Skip to content

Commit a2107b1

Browse files
committed
Land rapid7#6037, YARD for request plugin
2 parents a773627 + 35ca8ad commit a2107b1

File tree

1 file changed

+97
-5
lines changed

1 file changed

+97
-5
lines changed

plugins/request.rb

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ def commands
1919
}
2020
end
2121

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.
2226
def types
23-
# dynamically figure out what types are supported based on parse_args_*
2427
parse_methods = self.public_methods.select {|m| m.to_s =~ /^parse_args_/}
2528
parse_methods.collect {|m| m.to_s.split('_').slice(2..-1).join('_')}
2629
end
2730

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]
2835
def cmd_request(*args)
2936
# short circuit the whole deal if they need help
3037
return help if args.length == 0
@@ -68,6 +75,14 @@ def cmd_request(*args)
6875
end
6976
end
7077

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.
7186
def parse_args(args, type = 'http')
7287
type.downcase!
7388
parse_method = "parse_args_#{type}".to_sym
@@ -78,13 +93,25 @@ def parse_args(args, type = 'http')
7893
end
7994
end
8095

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.
82103
def parse_args_https(args = [], type = 'https')
83104
# just let http do it
84105
parse_args_http(args, type)
85106
end
86107

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.
88115
def parse_args_http(args = [], type = 'http')
89116
opt_parser = Rex::Parser::Arguments.new(
90117
'-0' => [ false, 'Use HTTP 1.0' ],
@@ -171,13 +198,63 @@ def parse_args_http(args = [], type = 'http')
171198
[options, opt_parser]
172199
end
173200

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]
175227
def handle_request_https(opts, opt_parser)
176228
# let http do it
177229
handle_request_http(opts, opt_parser)
178230
end
179231

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]
181258
def handle_request_http(opts, opt_parser)
182259
uri = opts[:uri]
183260
http_client = Rex::Proto::Http::Client.new(
@@ -238,6 +315,14 @@ def handle_request_http(opts, opt_parser)
238315
end
239316
end
240317

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]
241326
def output_line(opts, line)
242327
if opts[:output_file].nil?
243328
if line[-2..-1] == "\r\n"
@@ -252,6 +337,13 @@ def output_line(opts, line)
252337
end
253338
end
254339

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]
255347
def help(opt_parser = nil, msg = 'Usage: request [options] uri')
256348
print_line(msg)
257349
if opt_parser

0 commit comments

Comments
 (0)