Module: Ronin::Network::HTTP

Public Visibility

Public Class Method Summary

expand_options(options = {})

Expands the given HTTP options.

Returns: Hash

headers(options = {})

Converts underscored, dashed, lowercase and uppercase HTTP headers.

Returns: Hash

proxy

Returns: Proxy

request(options = {})

Creates an HTTP request object with the specified type and.

Returns: HTTP::Request

user_agent

Returns: String

user_agent=(agent)

Sets the default Ronin HTTP User-Agent.

Public Class Method Details

expand_options

public Hash expand_options(options = {})

Expands the given HTTP options.

Meta Tags

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:url N/A [String, URI::HTTP, URI::HTTPS]

The URL to request.

:host N/A [String]

The host to connect to.

:port ::Net::HTTP.default_port [String]

The port to connect to.

:user N/A [String]

The user to authenticate as.

:password N/A [String]

The password to authenticate with.

:path '/' [String]

The path to request.

:proxy Ronin::Network::HTTP.proxy [String, Hash]

The Proxy information.

Returns:

[Hash]

The expanded version of options.

[View source]


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ronin/network/http/http.rb', line 84

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

  if new_options[:url]
    url = URI(new_options.delete(:url).to_s)

    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

    unless url.path.empty?
      new_options[:path] = url.path
    else
      new_options[:path] = '/'
    end

    new_options[:path] << "?#{url.query}" if url.query
  else
    new_options[:port] ||= ::Net::HTTP.default_port
    new_options[:path] ||= '/'
  end

  if (proxy = new_options[:proxy])
    unless proxy.kind_of?(Hash)
      new_options[:proxy] = Ronin::Network::HTTP::Proxy.parse(proxy)
    end
  else
    new_options[:proxy] = Ronin::Network::HTTP.proxy
  end

  return new_options
end

headers

public Hash headers(options = {})

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

Meta Tags

Parameters:

[Hash{Symbol,String =>String}] options

Ronin HTTP headers.

Returns:

[Hash]

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

[View source]


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ronin/network/http/http.rb', line 129

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

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

  if options
    options.each do |name,value|
      header_name = name.to_s.split(/[\s+_-]/).map { |word|
        word.capitalize
      }.join('-')

      headers[header_name] = value.to_s
    end
  end

  return headers
end

proxy

public Proxy proxy

Meta Tags

Returns:

[Proxy]

The Ronin HTTP proxy hash.

[View source]


32
33
34
# File 'lib/ronin/network/http/http.rb', line 32

def HTTP.proxy
  @@http_proxy ||= Proxy.new
end

request

public HTTP::Request request(options = {})

Creates an HTTP request object with the specified type and given options. If type does not represent the name of an Net:HTTP Request Class an UnknownRequest exception will be raised.

Meta Tags

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:method N/A [Symbol, String]

The HTTP method to use for the request.

:path '/' [String]

The path to request.

:user N/A [String]

The user to authenticate as.

:password N/A [String]

The password to authenticate with.

:headers N/A [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.

[View source]


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
211
# File 'lib/ronin/network/http/http.rb', line 184

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

  name = options[:method].to_s.capitalize

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

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

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

  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_agent

public String user_agent

Meta Tags

Returns:

[String, nil]

The default Ronin HTTP User-Agent.

[View source]


40
41
42
# File 'lib/ronin/network/http/http.rb', line 40

def HTTP.user_agent
  @@http_user_agent ||= nil
end

user_agent=

public user_agent=(agent)

Sets the default Ronin HTTP User-Agent.

Meta Tags

Parameters:

[String] agent

The new User-Agent string to use.

[View source]


50
51
52
# File 'lib/ronin/network/http/http.rb', line 50

def HTTP.user_agent=(agent)
  @@http_user_agent = agent
end
Generated on Friday, September 25 2009 at 02:57:13 PM by YARD 0.2.3.5 (ruby-1.8.6).