Module: Ronin::Support::Network::HTTP::Request Private
- Defined in:
- lib/ronin/support/network/http/request.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Handles building HTTP request objects.
Constant Summary collapse
- METHODS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Request methods and
Net::HTTP
request classes. { copy: Net::HTTP::Copy, delete: Net::HTTP::Delete, get: Net::HTTP::Get, head: Net::HTTP::Head, lock: Net::HTTP::Lock, mkcol: Net::HTTP::Mkcol, move: Net::HTTP::Move, options: Net::HTTP::Options, patch: Net::HTTP::Patch, post: Net::HTTP::Post, propfind: Net::HTTP::Propfind, proppatch: Net::HTTP::Proppatch, put: Net::HTTP::Put, trace: Net::HTTP::Trace, unlock: Net::HTTP::Unlock }
- MIME_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Common MIME types.
{ text: 'text/plain', xml: 'text/xml', html: 'text/html', json: 'application/json' }
Class Method Summary collapse
-
.build(method, path, query: nil, query_params: nil, user: nil, password: nil, headers: nil, content_type: nil, accept: nil, user_agent: nil, cookie: nil, body: nil, form_data: nil, json: nil) ⇒ Net::HTTP::Copy, ...
private
Creates a new
Net::HTTP
request. -
.mime_type_for(mime_type) ⇒ String
private
Resolves the MIME type.
-
.request_uri(path, query: nil, query_params: nil) ⇒ String
private
Builds the [Request-URI], aka path + query string, for a HTTP request.
Class Method Details
.build(method, path, query: nil, query_params: nil, user: nil, password: nil, headers: nil, content_type: nil, accept: nil, user_agent: nil, cookie: nil, body: nil, form_data: nil, json: nil) ⇒ Net::HTTP::Copy, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new Net::HTTP
request.
The HTTP request method to use.
The `User-Agent` header value for the request.
The built HTTP request object.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/ronin/support/network/http/request.rb', line 212 def self.build(method,path, # query string keyword arguments query: nil, query_params: nil, # Basic-Auth keyword arguments user: nil, password: nil, # Header keyword arguments headers: nil, content_type: nil, accept: nil, user_agent: nil, cookie: nil, # request body keyword arguments body: nil, form_data: nil, json: nil) request_class = METHODS.fetch(method) do raise(ArgumentError,"unknown HTTP request method: #{method.inspect}") end request = request_class.new( request_uri(path, query: query, query_params: query_params), headers ) if user user = user.to_s password = password.to_s if password request.basic_auth(user,password) end if content_type request['Content-Type'] = mime_type_for(content_type) end if accept request['Accept'] = mime_type_for(accept) end if user_agent request['User-Agent'] = case user_agent when Symbol then UserAgents[user_agent] else user_agent end end if && !.empty? request['Cookie'] = case when Hash then HTTP::Cookie.new().to_s else .to_s end end if json request['Content-Type'] ||= 'application/json' request.body = json.to_json elsif form_data case form_data when String request['Content-Type'] ||= 'application/x-www-form-urlencoded' request.body = form_data else request.form_data = form_data end elsif body case body when IO, StringIO then request.body_stream = body else request.body = body end end return request end |
.mime_type_for(mime_type) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves the MIME type.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ronin/support/network/http/request.rb', line 118 def self.mime_type_for(mime_type) case mime_type when Symbol MIME_TYPES.fetch(mime_type) do raise(ArgumentError,"unsupported MIME type: #{mime_type.inspect}") end else mime_type end end |
.request_uri(path, query: nil, query_params: nil) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds the Request-URI, aka path + query string, for a HTTP request.
Additional query params for the request.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ronin/support/network/http/request.rb', line 55 def self.request_uri(path, query: nil, query_params: nil) if query_params query = URI::QueryParams.dump(query_params) end if query # append the query-string onto the path path += if path.include?('?') then "&#{query}" else "?#{query}" end end return path end |