Class: HTTP::Request

Inherits:
Object
  • Object
show all
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

"RubyHTTPGem/#{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

Constructor Details

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

:nodoc:


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

def initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = '1.1') # rubocop:disable ParameterLists
  @verb   = verb.to_s.downcase.to_sym
  @uri    = uri.is_a?(URI) ? uri : URI(uri.to_s)
  @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, @body, @version = proxy, body, version

  @headers = HTTP::Headers.coerce(headers || {})

  @headers['Host']        ||= default_host
  @headers['User-Agent']  ||= USER_AGENT
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


66
67
68
# File 'lib/http/request.rb', line 66

def body
  @body
end

#proxyObject (readonly)

Returns the value of attribute proxy


66
67
68
# File 'lib/http/request.rb', line 66

def proxy
  @proxy
end

#schemeObject (readonly)

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


54
55
56
# File 'lib/http/request.rb', line 54

def scheme
  @scheme
end

#uriObject (readonly)


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

def uri
  @uri
end

#verbObject (readonly)

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


51
52
53
# File 'lib/http/request.rb', line 51

def verb
  @verb
end

#versionObject (readonly)

Returns the value of attribute version


66
67
68
# File 'lib/http/request.rb', line 66

def version
  @version
end

Instance Method Details

#__method__(*args) ⇒ Object

The following alias may be removed in two minor versions (0.8.0) or one major version (1.0.0)


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

def __method__(*args)
  warn "#{Kernel.caller.first}: [DEPRECATION] HTTP::Request#__method__ is deprecated. Use #method instead."
  method(*args)
end

#include_proxy_authorization_headerObject

Compute and add the Proxy-Authorization header


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

def include_proxy_authorization_header
  digest = Base64.encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}").chomp
  headers['Proxy-Authorization'] = "Basic #{digest}"
end

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

Returns new Request with updated uri


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

def redirect(uri, verb = @verb)
  uri = @uri.merge uri.to_s
  req = self.class.new(verb, uri, headers, proxy, body, version)
  req['Host'] = req.uri.host
  req
end

#request_headerObject

Compute HTTP request header for direct or proxy request


116
117
118
# File 'lib/http/request.rb', line 116

def request_header
  "#{verb.to_s.upcase} #{path_for_request_header} HTTP/#{version}"
end

#socket_hostObject

Host for tcp socket


121
122
123
# File 'lib/http/request.rb', line 121

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

#socket_portObject

Port for tcp socket


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

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

#stream(socket) ⇒ Object

Stream the request to a socket


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

def stream(socket)
  include_proxy_authorization_header if using_authenticated_proxy?
  Request::Writer.new(socket, body, headers, request_header).stream
end

#using_authenticated_proxy?Boolean

Is this request using an authenticated proxy?

Returns:

  • (Boolean)

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

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

#using_proxy?Boolean

Is this request using a proxy?

Returns:

  • (Boolean)

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

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