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
}

Class Method Summary collapse

Class Method Details

.build(method, path, query: nil, query_params: nil, user: nil, password: nil, headers: nil, user_agent: nil, cookie: nil, body: nil, form_data: 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.

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

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ronin/support/network/http/request.rb', line 151

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,
                            user_agent: nil,
                            cookie:     nil,
                            # request body keyword arguments
                            body:      nil,
                            form_data: 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 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 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

.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