Class: HTTP::Request
- Inherits:
-
Object
- Object
- HTTP::Request
- 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
-
#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 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
-
#__method__ ⇒ Object
The following alias may be removed in three minor versions (0.8.0) or one major version (1.0.0).
-
#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:.
-
#method ⇒ Object
The following method may be removed in two minor versions (0.7.0) or one major version (1.0.0).
-
#redirect(uri, verb = @verb) ⇒ Object
Returns new Request with updated uri.
-
#request_header ⇒ Object
Compute HTTP request header for direct or proxy request.
-
#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?.
Constructor Details
#initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = '1.1') ⇒ Request
:nodoc:
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/http/request.rb', line 73 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.to_s.downcase.to_sym if @uri.scheme 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
#body ⇒ Object (readonly)
Returns the value of attribute body.
70 71 72 |
# File 'lib/http/request.rb', line 70 def body @body end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
70 71 72 |
# File 'lib/http/request.rb', line 70 def proxy @proxy end |
#scheme ⇒ Object (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 |
#uri ⇒ Object (readonly)
“Request URI” as per RFC 2616 www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
69 70 71 |
# File 'lib/http/request.rb', line 69 def uri @uri end |
#verb ⇒ Object (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 |
#version ⇒ Object (readonly)
Returns the value of attribute version.
70 71 72 |
# File 'lib/http/request.rb', line 70 def version @version end |
Instance Method Details
#__method__ ⇒ Object
The following alias may be removed in three minor versions (0.8.0) or one major version (1.0.0)
58 |
# File 'lib/http/request.rb', line 58 alias_method :__method__, :method |
#include_proxy_authorization_header ⇒ Object
Compute and add the Proxy-Authorization header
114 115 116 117 |
# File 'lib/http/request.rb', line 114 def digest = Base64.encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}").chomp headers['Proxy-Authorization'] = "Basic #{digest}" end |
#method ⇒ Object
The following method may be removed in two minor versions (0.7.0) or one major version (1.0.0)
62 63 64 65 |
# File 'lib/http/request.rb', line 62 def method(*) warn "#{Kernel.caller.first}: [DEPRECATION] HTTP::Request#method is deprecated. Use #verb instead. For Object#method, use #__method__." @verb end |
#redirect(uri, verb = @verb) ⇒ Object
Returns new Request with updated uri
90 91 92 93 94 95 |
# File 'lib/http/request.rb', line 90 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_header ⇒ Object
Compute HTTP request header for direct or proxy request
120 121 122 123 124 125 126 127 128 |
# File 'lib/http/request.rb', line 120 def request_header if using_proxy? "#{verb.to_s.upcase} #{uri} HTTP/#{version}" else path = uri.query && !uri.query.empty? ? "#{uri.path}?#{uri.query}" : uri.path path = '/' if path.empty? "#{verb.to_s.upcase} #{path} HTTP/#{version}" end end |
#socket_host ⇒ Object
Host for tcp socket
131 132 133 |
# File 'lib/http/request.rb', line 131 def socket_host using_proxy? ? proxy[:proxy_address] : uri.host end |
#socket_port ⇒ Object
Port for tcp socket
136 137 138 |
# File 'lib/http/request.rb', line 136 def socket_port using_proxy? ? proxy[:proxy_port] : uri.port end |
#stream(socket) ⇒ Object
Stream the request to a socket
98 99 100 101 |
# File 'lib/http/request.rb', line 98 def stream(socket) 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?
109 110 111 |
# File 'lib/http/request.rb', line 109 def using_authenticated_proxy? proxy && proxy.keys.size == 4 end |
#using_proxy? ⇒ Boolean
Is this request using a proxy?
104 105 106 |
# File 'lib/http/request.rb', line 104 def using_proxy? proxy && proxy.keys.size >= 2 end |