Class: HTTP::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Headers::Mixin
Defined in:
lib/http/request.rb,
lib/http/request/writer.rb

Defined Under Namespace

Classes: UnsupportedMethodError, UnsupportedSchemeError, Writer

Constant Summary collapse

USER_AGENT =

Default User-Agent header value

"http.rb/#{HTTP::VERSION}".freeze
METHODS =

RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1

[:options, :get, :head, :post, :put, :delete, :trace, :connect]
SCHEMES =

Allowed schemes

[:http, :https, :ws, :wss]
PORTS =

Default ports of supported schemes

{
  :http   => 80,
  :https  => 443,
  :ws     => 80,
  :wss    => 443
}

Instance Attribute Summary collapse

Attributes included from Headers::Mixin

#headers

Instance Method Summary collapse

Methods included from Headers::Mixin

#[], #[]=

Constructor Details

#initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") ⇒ Request

:nodoc:


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/http/request.rb', line 67

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


64
65
66
# File 'lib/http/request.rb', line 64

def body
  @body
end

#proxyObject (readonly)

Returns the value of attribute proxy


64
65
66
# File 'lib/http/request.rb', line 64

def proxy
  @proxy
end

#schemeObject (readonly)

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


59
60
61
# File 'lib/http/request.rb', line 59

def scheme
  @scheme
end

#uriObject (readonly)


63
64
65
# File 'lib/http/request.rb', line 63

def uri
  @uri
end

#verbObject (readonly)

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


56
57
58
# File 'lib/http/request.rb', line 56

def verb
  @verb
end

#versionObject (readonly)

Returns the value of attribute version


64
65
66
# File 'lib/http/request.rb', line 64

def version
  @version
end

Instance Method Details

#connect_using_proxy(socket) ⇒ Object

Setup tunnel through proxy for SSL request


119
120
121
# File 'lib/http/request.rb', line 119

def connect_using_proxy(socket)
  Request::Writer.new(socket, nil, proxy_connect_headers, proxy_connect_header).connect_through_proxy
end

#headlineObject Also known as: request_header

Compute HTTP request header for direct or proxy request


124
125
126
127
# File 'lib/http/request.rb', line 124

def headline
  request_uri = using_proxy? ? uri : uri.omit(:scheme, :authority)
  "#{verb.to_s.upcase} #{request_uri.omit :fragment} HTTP/#{version}"
end

#include_proxy_authorization_headerObject

Compute and add the Proxy-Authorization header


109
110
111
# File 'lib/http/request.rb', line 109

def include_proxy_authorization_header
  headers[Headers::PROXY_AUTHORIZATION] = proxy_authorization_header
end

#proxy_authorization_headerObject


113
114
115
116
# File 'lib/http/request.rb', line 113

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

#proxy_connect_headerObject

Compute HTTP request header SSL proxy connection


133
134
135
# File 'lib/http/request.rb', line 133

def proxy_connect_header
  "CONNECT #{host}:#{port} HTTP/#{version}"
end

#proxy_connect_headersObject

Headers to send with proxy connect request


138
139
140
141
142
143
144
145
146
147
# File 'lib/http/request.rb', line 138

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

#redirect(uri, verb = @verb) ⇒ Object

Returns new Request with updated uri


86
87
88
89
90
# File 'lib/http/request.rb', line 86

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

#socket_hostObject

Host for tcp socket


150
151
152
# File 'lib/http/request.rb', line 150

def socket_host
  using_proxy? ? proxy[:proxy_address] : host
end

#socket_portObject

Port for tcp socket


155
156
157
# File 'lib/http/request.rb', line 155

def socket_port
  using_proxy? ? proxy[:proxy_port] : port
end

#stream(socket) ⇒ Object

Stream the request to a socket


93
94
95
96
# File 'lib/http/request.rb', line 93

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

#using_authenticated_proxy?Boolean

Is this request using an authenticated proxy?

Returns:

  • (Boolean)

104
105
106
# File 'lib/http/request.rb', line 104

def using_authenticated_proxy?
  proxy && proxy.keys.size == 4
end

#using_proxy?Boolean

Is this request using a proxy?

Returns:

  • (Boolean)

99
100
101
# File 'lib/http/request.rb', line 99

def using_proxy?
  proxy && proxy.keys.size >= 2
end