Module: Ronin::Network::HTTP

Defined in:
lib/ronin/network/http/http.rb,
lib/ronin/network/http/proxy.rb,
lib/ronin/network/http/exceptions/unknown_request.rb

Overview

Settings and helper methods for HTTP.

Defined Under Namespace

Classes: Proxy, UnknownRequest

Class Method Summary collapse

Class Method Details

.expand_options(options = {}) ⇒ Hash

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.

Expands the given HTTP options.

Parameters:

  • options (Hash) (defaults to: {})

    HTTP options.

Options Hash (options):

  • :url (String, URI::HTTP, URI::HTTPS)

    The URL to request.

  • :host (String)

    The host to connect to.

  • :port (String) — default: ::Net::HTTP.default_port

    The port to connect to.

  • :user (String)

    The user to authenticate as.

  • :password (String)

    The password to authenticate with.

  • :path (String) — default: '/'

    The path to request.

  • :proxy (String, Hash) — default: Ronin::Network::HTTP.proxy

    The Proxy information.

Returns:

  • (Hash)

    The expanded version of options.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ronin/network/http/http.rb', line 166

def HTTP.expand_options(options={})
  new_options = options.dup

  new_options[:port] ||= Net::HTTP.default_port
  new_options[:path] ||= '/'

  if new_options[:ssl] == true
    new_options[:ssl] = {}
  end

  if (url = new_options.delete(:url))
    new_options.merge!(HTTP.expand_url(url))
  end

  new_options[:proxy] = if new_options.has_key?(:proxy)
                          HTTP::Proxy.create(new_options[:proxy])
                        else
                          HTTP.proxy
                        end

  return new_options
end

.expand_url(url) ⇒ Hash{Symbol => Object}

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.

Expands the URL into options.

Parameters:

Returns:

  • (Hash{Symbol => Object})

    The options for the URL.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ronin/network/http/http.rb', line 104

def HTTP.expand_url(url)
  new_options = {}

  url = case url
        when URI
          url
        when Hash
          URI::HTTP.build(url)
        else
          URI(url.to_s)
        end

  new_options[:ssl] = {} if url.scheme == 'https'

  new_options[:host] = url.host
  new_options[:port] = url.port

  new_options[:user] = url.user if url.user
  new_options[:password] = url.password if url.password

  new_options[:path] = unless url.path.empty?
                         url.path
                       else
                         '/'
                       end
  new_options[:path] += "?#{url.query}" if url.query

  return new_options
end

.header_name(name) ⇒ 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.

Converts an underscored, dashed, lowercase or uppercase HTTP header name to the standard camel-case HTTP header name.

Parameters:

  • name (Symbol, String)

    The unformatted HTTP header name.

Returns:

  • (String)

    The camel-case HTTP header name.



201
202
203
204
205
206
# File 'lib/ronin/network/http/http.rb', line 201

def HTTP.header_name(name)
  words = name.to_s.split(/[\s+_-]/)

  words.each { |word| word.capitalize! }
  return words.join('-')
end

.headers(options = {}) ⇒ Hash

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.

Converts underscored, dashed, lowercase and uppercase HTTP headers to standard camel-cased HTTP headers.

Parameters:

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

    Ronin HTTP headers.

Returns:

  • (Hash)

    The camel-cased HTTP headers created from the given options.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ronin/network/http/http.rb', line 220

def HTTP.headers(options={})
  headers = {}

  if HTTP.user_agent
    headers['User-Agent'] = HTTP.user_agent
  end

  if options
    options.each do |name,value|
      headers[HTTP.header_name(name)] = value.to_s
    end
  end

  return headers
end

.proxyProxy

The Ronin HTTP proxy to use. Parses the value of the HTTP_PROXY environment variable if set.

Returns:

  • (Proxy)

    The Ronin HTTP proxy.

See Also:



42
43
44
45
46
47
48
# File 'lib/ronin/network/http/http.rb', line 42

def HTTP.proxy
  @proxy ||= if ENV['HTTP_PROXY']
               Proxy.parse(ENV['HTTP_PROXY'])
             else
               Proxy.new
             end
end

.proxy=(new_proxy) ⇒ Proxy

Sets the Ronin HTTP proxy to use.

Parameters:

Returns:

  • (Proxy)

    The new proxy.

Raises:

  • (ArgumentError)

    The given proxy information was not a Proxy, URI::HTTP, Hash or String.



65
66
67
# File 'lib/ronin/network/http/http.rb', line 65

def HTTP.proxy=(new_proxy)
  @proxy = Proxy.create(new_proxy)
end

.request(options = {}) ⇒ HTTP::Request

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 specific type of HTTP request object.

Parameters:

  • options (Hash) (defaults to: {})

    The HTTP options for the request.

Options Hash (options):

  • :method (Symbol, String)

    The HTTP method to use for the request.

  • :path (String) — default: '/'

    The path to request.

  • :body (String)

    The body of the request.

  • :form_data (Hash, String)

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

  • :user (String)

    The user to authenticate as.

  • :password (String)

    The password to authenticate with.

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

    Additional HTTP headers to use for the request.

Returns:

  • (HTTP::Request)

    The new HTTP Request object.

Raises:

  • (ArgumentError)

    The :method option must be specified.

  • (UnknownRequest)

    The :method option did not match a known Net::HTTP request class.

See Also:



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/ronin/network/http/http.rb', line 277

def HTTP.request(options={})
  unless options[:method]
    raise(ArgumentError,"the :method option must be specified")
  end

  name = options[:method].to_s.capitalize

  unless Net::HTTP.const_defined?(name)
    raise(UnknownRequest,"unknown HTTP request type #{name.dump}")
  end

  headers = HTTP.headers(options[:headers])
  path = (options[:path] || '/').to_s

  request = Net::HTTP.const_get(name).new(path,headers)

  if request.request_body_permitted?
    if options[:form_data]
      request.set_form_data(options[:form_data])
    elsif options[:body]
      request.body = options[:body]
    end
  end

  if (user = options.delete(:user))
    user = user.to_s

    if (password = options.delete(:password))
      password = password.to_s
    end

    request.basic_auth(user,password)
  end

  return request
end

.user_agentString?

The default Ronin HTTP User-Agent string.

Returns:

  • (String, nil)

    The default Ronin HTTP User-Agent.



77
78
79
# File 'lib/ronin/network/http/http.rb', line 77

def HTTP.user_agent
  @user_agent ||= nil
end

.user_agent=(agent) ⇒ Object

Sets the default Ronin HTTP User-Agent string.

Parameters:

  • agent (String)

    The new User-Agent string to use.



89
90
91
# File 'lib/ronin/network/http/http.rb', line 89

def HTTP.user_agent=(agent)
  @user_agent = agent
end