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.

Since:

  • 1.0.0

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.

Since:

  • 1.0.0

{
  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.

Since:

  • 1.1.0

{
  text: 'text/plain',
  xml:  'text/xml',
  html: 'text/html',
  json: 'application/json'
}

Class Method Summary collapse

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.

Parameters:

  • method (:copy, :delete, :get, :head, :lock, :mkcol, :move, :options, :patch, :post, :propfind, :proppatch, :put, :trace, :unlock)
  • path (String)
  • query (String, nil) (defaults to: nil)

    The query-string to append to the request path.

  • query_params (Hash, nil) (defaults to: nil)

    The query-params to append to the request path.

  • user (String, nil) (defaults to: nil)

    The user to authenticate as.

  • password (String, nil) (defaults to: nil)

    The password to authenticate with.

  • headers (Hash{Symbol => String}, Hash{String => String}, nil) (defaults to: nil)

    Additional HTTP header names and values to add to the request.

  • content_type (String, :text, :xml, :html, :json, nil) (defaults to: nil)

    The Content-Type header value for the request. If a Symbol is given it will be resolved to a common MIME type:

    • :text - text/plain
    • :xml - text/xml
    • :html - text/html
    • :json - application/json
  • accept (String, :text, :xml, :html, :json, nil) (defaults to: nil)

    The Accept header value for the request. If a Symbol is given it will be resolved to a common MIME type:

    • :text - text/plain
    • :xml - text/xml
    • :html - text/html
    • :json - application/json
  • user_agent (String, :random, :chrome, :chrome_linux, :chrome_macos, :chrome_windows, :chrome_iphone, :chrome_ipad, :chrome_android, :firefox, :firefox_linux, :firefox_macos, :firefox_windows, :firefox_iphone, :firefox_ipad, :firefox_android, :safari, :safari_macos, :safari_iphone, :safari_ipad, :edge, :linux, :macos, :windows, :iphone, :ipad, :android, nil) (defaults to: nil)
  • cookie (String, Hash{String => String}, Cookie, nil) (defaults to: nil)

    Additional Cookie header. If a Hash is given, it will be converted to a String using Cookie. If the cookie value is empty, the Cookie header will not be set.

  • body (String, nil) (defaults to: nil)

    The body of the request.

  • form_data (Hash, String, nil) (defaults to: nil)

    The form data that may be sent in the body of the request.

  • json (#to_json, nil) (defaults to: nil)

    The JSON data that will be sent in the body of the request. Will also default the Content-Type header to application/json, unless already set.

Returns:

  • (Net::HTTP::Copy, Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head, Net::HTTP::Lock, Net::HTTP::Mkcol, Net::HTTP::Move, Net::HTTP::Options, Net::HTTP::Patch, Net::HTTP::Post, Net::HTTP::Propfind, Net::HTTP::Proppatch, Net::HTTP::Put, Net::HTTP::Trace, Net::HTTP::Unlock)

Since:

  • 1.0.0



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 cookie && !cookie.empty?
    request['Cookie'] = case cookie
                        when Hash then HTTP::Cookie.new(cookie).to_s
                        else           cookie.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.

Parameters:

  • mime_type (String, :text, :xml, :html, :json, nil)

    The MIME type String or Symbol. If a Symbol is given it will be resolved to a common MIME type:

    • :text - text/plain
    • :xml - text/xml
    • :html - text/html
    • :json - application/json

Returns:

  • (String)

    The resolved MIME type String.

Raises:

  • (ArgumentError)

    An unknown Symbol is given.

Since:

  • 1.1.0



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.

Parameters:

  • path (String)

    The path of the request.

  • query (String, nil) (defaults to: nil)

    The optional query string for the request.

  • query_params (Hash{Symbol => Object}, Hash{String => Object}, nil) (defaults to: nil)

Returns:

Since:

  • 1.0.0



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