@@ -19,12 +19,19 @@ def commands
19
19
}
20
20
end
21
21
22
+ # Dynamically determine the types of requests that are supported based on
23
+ # methods prefixed with "parse_args".
24
+ #
25
+ # @return [Array<String>] The supported request types.
22
26
def types
23
- # dynamically figure out what types are supported based on parse_args_*
24
27
parse_methods = self . public_methods . select { |m | m . to_s =~ /^parse_args_/ }
25
28
parse_methods . collect { |m | m . to_s . split ( '_' ) . slice ( 2 ..-1 ) . join ( '_' ) }
26
29
end
27
30
31
+ # The main handler for the request command.
32
+ #
33
+ # @param args [Array<String>] The array of arguments provided by the user.
34
+ # @return [nil]
28
35
def cmd_request ( *args )
29
36
# short circuit the whole deal if they need help
30
37
return help if args . length == 0
@@ -68,6 +75,14 @@ def cmd_request(*args)
68
75
end
69
76
end
70
77
78
+ # Parse the provided arguments by dispatching to the correct method based
79
+ # on the specified type.
80
+ #
81
+ # @param args [Array<String>] The command line arguments to parse.
82
+ # @param type [String] The protocol type that the request is for such as
83
+ # HTTP.
84
+ # @return [Array<Hash, Rex::Parser::Arguments>] An array with the options
85
+ # hash and the argument parser.
71
86
def parse_args ( args , type = 'http' )
72
87
type . downcase!
73
88
parse_method = "parse_args_#{ type } " . to_sym
@@ -78,13 +93,25 @@ def parse_args(args, type = 'http')
78
93
end
79
94
end
80
95
81
- # arg parsing for requests of type 'http'
96
+ # Parse the provided arguments for making HTTPS requests. The argument flags
97
+ # are intended to be similar to the curl utility.
98
+ #
99
+ # @param args [Array<String>] The command line arguments to parse.
100
+ # @param type [String] The protocol type that the request is for.
101
+ # @return [Array<Hash, Rex::Parser::Arguments>] An array with the options
102
+ # hash and the argument parser.
82
103
def parse_args_https ( args = [ ] , type = 'https' )
83
104
# just let http do it
84
105
parse_args_http ( args , type )
85
106
end
86
107
87
- # arg parsing for requests of type 'http'
108
+ # Parse the provided arguments for making HTTP requests. The argument flags
109
+ # are intended to be similar to the curl utility.
110
+ #
111
+ # @param args [Array<String>] The command line arguments to parse.
112
+ # @param type [String] The protocol type that the request is for.
113
+ # @return [Array<Hash>, Rex::Parser::Arguments>] An array with the options
114
+ # hash and the argument parser.
88
115
def parse_args_http ( args = [ ] , type = 'http' )
89
116
opt_parser = Rex ::Parser ::Arguments . new (
90
117
'-0' => [ false , 'Use HTTP 1.0' ] ,
@@ -171,13 +198,63 @@ def parse_args_http(args = [], type = 'http')
171
198
[ options , opt_parser ]
172
199
end
173
200
174
- # handling for requests of type 'https'
201
+ # Perform an HTTPS request based on the user specifed options.
202
+ #
203
+ # @param opts [Hash] The options to use for making the HTTPS request.
204
+ # @option opts [String] :auth_username An optional username to use with
205
+ # basic authentication.
206
+ # @option opts [String] :auth_password An optional password to use with
207
+ # basic authentication. This is only used when :auth_username is
208
+ # specified.
209
+ # @option opts [String] :data Any data to include within the body of the
210
+ # request. Often used with the POST HTTP method.
211
+ # @option opts [Hash] :headers A hash of additional headers to include in
212
+ # the request.
213
+ # @option opts [String] :method The HTTP method to use in the request.
214
+ # @option opts [#write] :output_file A file to write the response data to.
215
+ # @option opts [Boolean] :print_body Whether or not to print the body of the
216
+ # response.
217
+ # @option opts [Boolean] :print_headers Whether or not to print the headers
218
+ # of the response.
219
+ # @options opts [String] :ssl_version The version of SSL to use if the
220
+ # request scheme is HTTPS.
221
+ # @option opts [String] :uri The target uri to request.
222
+ # @option opts [String] :user_agent The value to use in the User-Agent
223
+ # header of the request.
224
+ # @param opt_parser [Rex::Parser::Arguments] the argument parser for the
225
+ # request type.
226
+ # @return [nil]
175
227
def handle_request_https ( opts , opt_parser )
176
228
# let http do it
177
229
handle_request_http ( opts , opt_parser )
178
230
end
179
231
180
- # handling for requests of type 'http'
232
+ # Perform an HTTP request based on the user specifed options.
233
+ #
234
+ # @param opts [Hash] The options to use for making the HTTP request.
235
+ # @option opts [String] :auth_username An optional username to use with
236
+ # basic authentication.
237
+ # @option opts [String] :auth_password An optional password to use with
238
+ # basic authentication. This is only used when :auth_username is
239
+ # specified.
240
+ # @option opts [String] :data Any data to include within the body of the
241
+ # request. Often used with the POST HTTP method.
242
+ # @option opts [Hash] :headers A hash of additional headers to include in
243
+ # the request.
244
+ # @option opts [String] :method The HTTP method to use in the request.
245
+ # @option opts [#write] :output_file A file to write the response data to.
246
+ # @option opts [Boolean] :print_body Whether or not to print the body of the
247
+ # response.
248
+ # @option opts [Boolean] :print_headers Whether or not to print the headers
249
+ # of the response.
250
+ # @options opts [String] :ssl_version The version of SSL to use if the
251
+ # request scheme is HTTPS.
252
+ # @option opts [String] :uri The target uri to request.
253
+ # @option opts [String] :user_agent The value to use in the User-Agent
254
+ # header of the request.
255
+ # @param opt_parser [Rex::Parser::Arguments] the argument parser for the
256
+ # request type.
257
+ # @return [nil]
181
258
def handle_request_http ( opts , opt_parser )
182
259
uri = opts [ :uri ]
183
260
http_client = Rex ::Proto ::Http ::Client . new (
@@ -238,6 +315,14 @@ def handle_request_http(opts, opt_parser)
238
315
end
239
316
end
240
317
318
+ # Output lines based on the provided options. Data is either printed to the
319
+ # console or written to a file. Trailing new lines are removed.
320
+ #
321
+ # @param opts [Hash] The options as parsed from parse_args.
322
+ # @option opts [#write, nil] :output_file An optional file to write the
323
+ # output to.
324
+ # @param line [String] The string to output.
325
+ # @return [nil]
241
326
def output_line ( opts , line )
242
327
if opts [ :output_file ] . nil?
243
328
if line [ -2 ..-1 ] == "\r \n "
@@ -252,6 +337,13 @@ def output_line(opts, line)
252
337
end
253
338
end
254
339
340
+ # Print the appropriate help text depending on an optional option parser.
341
+ #
342
+ # @param opt_parser [Rex::Parser::Arguments] the argument parser for the
343
+ # request type.
344
+ # @param msg [String] the first line of the help text to display to the
345
+ # user.
346
+ # @return [nil]
255
347
def help ( opt_parser = nil , msg = 'Usage: request [options] uri' )
256
348
print_line ( msg )
257
349
if opt_parser
0 commit comments