class HTTP::Request

def connect_using_proxy(socket)

Setup tunnel through proxy for SSL request
def connect_using_proxy(socket)
  Request::Writer.new(socket, nil, proxy_connect_headers, proxy_connect_header).connect_through_proxy
end

def default_host_header_value

Returns:
  • (String) - Default host (with port if needed) header value.
def default_host_header_value
  PORTS[@scheme] != port ? "#{host}:#{port}" : host
end

def headline

Compute HTTP request header for direct or proxy request
def headline
  request_uri = using_proxy? ? uri : uri.omit(:scheme, :authority)
  "#{verb.to_s.upcase} #{request_uri.omit :fragment} HTTP/#{version}"
end

def include_proxy_authorization_header

Compute and add the Proxy-Authorization header
def include_proxy_authorization_header
  headers[Headers::PROXY_AUTHORIZATION] = proxy_authorization_header
end

def initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") # rubocop:disable ParameterLists

rubocop:disable ParameterLists
:nodoc:
def initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") # rubocop:disable ParameterLists
  @verb   = verb.to_s.downcase.to_sym
  @uri    = normalize_uri uri
  @scheme = @uri.scheme && @uri.scheme.to_s.downcase.to_sym
  fail(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb)
  fail(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme)
  @proxy   = proxy
  @body    = body
  @version = version
  @headers = HTTP::Headers.coerce(headers || {})
  @headers[Headers::HOST]        ||= default_host_header_value
  @headers[Headers::USER_AGENT]  ||= USER_AGENT
end

def normalize_uri(uri)

Returns:
  • (HTTP::URI) - URI with all componentes but query being normalized.
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

def port

Returns:
  • (Fixnum) -
def port
  @uri.port || @uri.default_port
end

def proxy_authorization_header

def proxy_authorization_header
  digest = Base64.strict_encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}")
  "Basic #{digest}"
end

def proxy_connect_header

Compute HTTP request header SSL proxy connection
def proxy_connect_header
  "CONNECT #{host}:#{port} HTTP/#{version}"
end

def proxy_connect_headers

Headers to send with proxy connect request
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
end

def redirect(uri, verb = @verb)

Returns new Request with updated uri
def redirect(uri, verb = @verb)
  req = self.class.new(verb, @uri.join(uri), headers, proxy, body, version)
  req[Headers::HOST] = req.uri.host
  req
end

def socket_host

Host for tcp socket
def socket_host
  using_proxy? ? proxy[:proxy_address] : host
end

def socket_port

Port for tcp socket
def socket_port
  using_proxy? ? proxy[:proxy_port] : port
end

def stream(socket)

Stream the request to a socket
def stream(socket)
  include_proxy_authorization_header if using_authenticated_proxy? && !@uri.https?
  Request::Writer.new(socket, body, headers, headline).stream
end

def using_authenticated_proxy?

Is this request using an authenticated proxy?
def using_authenticated_proxy?
  proxy && proxy.keys.size == 4
end

def using_proxy?

Is this request using a proxy?
def using_proxy?
  proxy && proxy.keys.size >= 2
end