Class: HTTP::Request
- Inherits:
-
Object
- Object
- HTTP::Request
- Extended by:
- Forwardable
- Includes:
- Headers::Mixin
- Defined in:
- lib/http/request.rb,
lib/http/request/writer.rb,
lib/http/request/caching.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Caching, 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#scheme ⇒ Object
readonly
Scheme is normalized to be a lowercase symbol e.g.
-
#uri ⇒ Object
readonly
"Request URI" as per RFC 2616 http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html.
-
#verb ⇒ Object
readonly
Method is given as a lowercase symbol e.g.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Attributes included from Headers::Mixin
Instance Method Summary collapse
- #caching ⇒ HTTP::Request::Caching
-
#connect_using_proxy(socket) ⇒ Object
Setup tunnel through proxy for SSL request.
-
#headline ⇒ Object
(also: #request_header)
Compute HTTP request header for direct or proxy request.
-
#include_proxy_authorization_header ⇒ Object
Compute and add the Proxy-Authorization header.
-
#initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") ⇒ Request
constructor
:nodoc:.
- #proxy_authorization_header ⇒ Object
-
#proxy_connect_header ⇒ Object
Compute HTTP request header SSL proxy connection.
-
#proxy_connect_headers ⇒ Object
Headers to send with proxy connect request.
-
#redirect(uri, verb = @verb) ⇒ Object
Returns new Request with updated uri.
-
#socket_host ⇒ Object
Host for tcp socket.
-
#socket_port ⇒ Object
Port for tcp socket.
-
#stream(socket) ⇒ Object
Stream the request to a socket.
-
#using_authenticated_proxy? ⇒ Boolean
Is this request using an authenticated proxy?.
-
#using_proxy? ⇒ Boolean
Is this request using a proxy?.
Methods included from Headers::Mixin
Constructor Details
#initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") ⇒ Request
:nodoc:
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/http/request.rb', line 68 def initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") # rubocop:disable ParameterLists @verb = verb.to_s.downcase.to_sym @uri = HTTP::URI.parse(uri).normalize @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_header_value @headers["User-Agent"] ||= USER_AGENT end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
65 66 67 |
# File 'lib/http/request.rb', line 65 def body @body end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
65 66 67 |
# File 'lib/http/request.rb', line 65 def proxy @proxy end |
#scheme ⇒ Object (readonly)
Scheme is normalized to be a lowercase symbol e.g. :http, :https
60 61 62 |
# File 'lib/http/request.rb', line 60 def scheme @scheme end |
#uri ⇒ Object (readonly)
"Request URI" as per RFC 2616 http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
64 65 66 |
# File 'lib/http/request.rb', line 64 def uri @uri end |
#verb ⇒ Object (readonly)
Method is given as a lowercase symbol e.g. :get, :post
57 58 59 |
# File 'lib/http/request.rb', line 57 def verb @verb end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
65 66 67 |
# File 'lib/http/request.rb', line 65 def version @version end |
Instance Method Details
#caching ⇒ HTTP::Request::Caching
157 158 159 |
# File 'lib/http/request.rb', line 157 def caching Caching.new self end |
#connect_using_proxy(socket) ⇒ Object
Setup tunnel through proxy for SSL request
118 119 120 |
# File 'lib/http/request.rb', line 118 def connect_using_proxy(socket) Request::Writer.new(socket, nil, proxy_connect_headers, proxy_connect_header).connect_through_proxy end |
#headline ⇒ Object Also known as: request_header
Compute HTTP request header for direct or proxy request
123 124 125 126 |
# File 'lib/http/request.rb', line 123 def headline request_uri = using_proxy? ? uri : uri.omit(:scheme, :authority) "#{verb.to_s.upcase} #{request_uri} HTTP/#{version}" end |
#include_proxy_authorization_header ⇒ Object
Compute and add the Proxy-Authorization header
108 109 110 |
# File 'lib/http/request.rb', line 108 def headers["Proxy-Authorization"] = end |
#proxy_authorization_header ⇒ Object
112 113 114 115 |
# File 'lib/http/request.rb', line 112 def digest = Base64.strict_encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}") "Basic #{digest}" end |
#proxy_connect_header ⇒ Object
Compute HTTP request header SSL proxy connection
132 133 134 |
# File 'lib/http/request.rb', line 132 def proxy_connect_header "CONNECT #{@uri.host}:#{@uri.port} HTTP/#{version}" end |
#proxy_connect_headers ⇒ Object
Headers to send with proxy connect request
137 138 139 140 141 142 143 144 |
# File 'lib/http/request.rb', line 137 def proxy_connect_headers connect_headers = HTTP::Headers.coerce( "Host" => headers["Host"], "User-Agent" => headers["User-Agent"] ) connect_headers["Proxy-Authorization"] = if using_authenticated_proxy? connect_headers end |
#redirect(uri, verb = @verb) ⇒ Object
Returns new Request with updated uri
85 86 87 88 89 |
# File 'lib/http/request.rb', line 85 def redirect(uri, verb = @verb) req = self.class.new(verb, @uri.join(uri), headers, proxy, body, version) req["Host"] = req.uri.host req end |
#socket_host ⇒ Object
Host for tcp socket
147 148 149 |
# File 'lib/http/request.rb', line 147 def socket_host using_proxy? ? proxy[:proxy_address] : host end |
#socket_port ⇒ Object
Port for tcp socket
152 153 154 |
# File 'lib/http/request.rb', line 152 def socket_port using_proxy? ? proxy[:proxy_port] : port end |
#stream(socket) ⇒ Object
Stream the request to a socket
92 93 94 95 |
# File 'lib/http/request.rb', line 92 def stream(socket) 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?
103 104 105 |
# File 'lib/http/request.rb', line 103 def using_authenticated_proxy? proxy && proxy.keys.size == 4 end |
#using_proxy? ⇒ Boolean
Is this request using a proxy?
98 99 100 |
# File 'lib/http/request.rb', line 98 def using_proxy? proxy && proxy.keys.size >= 2 end |