class HTTP::Request

Constants

METHODS
PORTS

Default ports of supported schemes

SCHEMES

Allowed schemes

USER_AGENT

Default User-Agent header value

Attributes

body[R]
proxy[R]
scheme[R]

Scheme is normalized to be a lowercase symbol e.g. :http, :https

uri[R]

“Request URI” as per RFC 2616 www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

verb[R]

Method is given as a lowercase symbol e.g. :get, :post

version[R]

Public Class Methods

new(opts) click to toggle source

@option opts [String] :version @option opts [#to_s] :verb HTTP request method @option opts [HTTP::URI, to_s] :uri @option opts [Hash] :headers @option opts [Hash] :proxy @option opts [String] :body

# File lib/http/request.rb, line 75
def initialize(opts)
  @verb   = opts.fetch(:verb).to_s.downcase.to_sym
  @uri    = normalize_uri(opts.fetch(:uri))
  @scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme

  raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb)
  raise(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme)

  @proxy   = opts[:proxy] || {}
  @body    = opts[:body]
  @version = opts[:version] || "1.1"
  @headers = HTTP::Headers.coerce(opts[:headers] || {})

  @headers[Headers::HOST]        ||= default_host_header_value
  @headers[Headers::USER_AGENT]  ||= USER_AGENT
end

Public Instance Methods

connect_using_proxy(socket) click to toggle source

Setup tunnel through proxy for SSL request

# File lib/http/request.rb, line 139
def connect_using_proxy(socket)
  Request::Writer.new(socket, nil, proxy_connect_headers, proxy_connect_header).connect_through_proxy
end
headline() click to toggle source

Compute HTTP request header for direct or proxy request

# File lib/http/request.rb, line 144
def headline
  request_uri = (using_proxy? && !uri.https?) ? uri : uri.omit(:scheme, :authority)
  "#{verb.to_s.upcase} #{request_uri.omit :fragment} HTTP/#{version}"
end
include_proxy_authorization_header() click to toggle source

Compute and add the Proxy-Authorization header

# File lib/http/request.rb, line 129
def include_proxy_authorization_header
  headers[Headers::PROXY_AUTHORIZATION] = proxy_authorization_header
end
include_proxy_headers() click to toggle source
# File lib/http/request.rb, line 123
def include_proxy_headers
  headers.merge!(proxy[:proxy_headers]) if proxy.key?(:proxy_headers)
  include_proxy_authorization_header if using_authenticated_proxy?
end
proxy_authorization_header() click to toggle source
# File lib/http/request.rb, line 133
def proxy_authorization_header
  digest = Base64.strict_encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}")
  "Basic #{digest}"
end
proxy_connect_header() click to toggle source

Compute HTTP request header SSL proxy connection

# File lib/http/request.rb, line 150
def proxy_connect_header
  "CONNECT #{host}:#{port} HTTP/#{version}"
end
proxy_connect_headers() click to toggle source

Headers to send with proxy connect request

# File lib/http/request.rb, line 155
def proxy_connect_headers
  connect_headers = HTTP::Headers.coerce(
    Headers::HOST        => headers[Headers::HOST],
    Headers::USER_AGENT  => headers[Headers::USER_AGENT]
  )

  connect_headers[Headers::PROXY_AUTHORIZATION] = proxy_authorization_header if using_authenticated_proxy?
  connect_headers.merge!(proxy[:proxy_headers]) if proxy.key?(:proxy_headers)
  connect_headers
end
redirect(uri, verb = @verb) click to toggle source

Returns new Request with updated uri

# File lib/http/request.rb, line 93
def redirect(uri, verb = @verb)
  req = self.class.new(
    :verb    => verb,
    :uri     => @uri.join(uri),
    :headers => headers,
    :proxy   => proxy,
    :body    => body,
    :version => version
  )

  req[Headers::HOST] = req.uri.host
  req
end
socket_host() click to toggle source

Host for tcp socket

# File lib/http/request.rb, line 167
def socket_host
  using_proxy? ? proxy[:proxy_address] : host
end
socket_port() click to toggle source

Port for tcp socket

# File lib/http/request.rb, line 172
def socket_port
  using_proxy? ? proxy[:proxy_port] : port
end
stream(socket) click to toggle source

Stream the request to a socket

# File lib/http/request.rb, line 108
def stream(socket)
  include_proxy_headers if using_proxy? && !@uri.https?
  Request::Writer.new(socket, body, headers, headline).stream
end
using_authenticated_proxy?() click to toggle source

Is this request using an authenticated proxy?

# File lib/http/request.rb, line 119
def using_authenticated_proxy?
  proxy && proxy.keys.size >= 4
end
using_proxy?() click to toggle source

Is this request using a proxy?

# File lib/http/request.rb, line 114
def using_proxy?
  proxy && proxy.keys.size >= 2
end

Private Instance Methods

default_host_header_value() click to toggle source

@return [String] Default host (with port if needed) header value.

# File lib/http/request.rb, line 189
def default_host_header_value
  PORTS[@scheme] != port ? "#{host}:#{port}" : host
end
normalize_uri(uri) click to toggle source

@return [HTTP::URI] URI with all componentes but query being normalized.

# File lib/http/request.rb, line 194
def normalize_uri(uri)
  uri = HTTP::URI.parse uri

  HTTP::URI.new(
    :scheme     => uri.normalized_scheme,
    :authority  => uri.normalized_authority,
    :path       => uri.normalized_path,
    :query      => uri.query,
    :fragment   => uri.normalized_fragment
  )
end
port() click to toggle source

@!attribute [r] port

@return [Fixnum]
# File lib/http/request.rb, line 184
def port
  @uri.port || @uri.default_port
end