Class: HTTP::Request
Defined Under Namespace
Classes: UnsupportedMethodError
Constant Summary collapse
- METHODS =
RFC 2616: Hypertext Transfer Protocol – HTTP/1.1
[:options, :get, :head, :post, :put, :delete, :trace, :connect]
Constants included from Header
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Method is given as a lowercase symbol e.g.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#uri ⇒ Object
readonly
“Request URI” as per RFC 2616 www.w3.org/Protocols/rfc2616/rfc2616-sec5.html.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#[](header) ⇒ Object
Obtain the given header.
-
#initialize(method, uri, headers = {}, proxy = {}, body = nil, version = "1.1") ⇒ Request
constructor
:nodoc:.
-
#stream(socket) ⇒ Object
Stream the request to a socket.
Methods included from Header
Constructor Details
#initialize(method, uri, headers = {}, proxy = {}, body = nil, version = "1.1") ⇒ Request
:nodoc:
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/http/request.rb', line 39 def initialize(method, uri, headers = {}, proxy = {}, body = nil, version = "1.1") @method = method.to_s.downcase.to_sym raise UnsupportedMethodError, "unknown method: #{method}" unless METHODS.include? @method @uri = uri.is_a?(URI) ? uri : URI(uri.to_s) @headers = {} headers.each do |name, value| name = name.to_s key = name[CANONICAL_HEADER] key ||= canonicalize_header(name) @headers[key] = value end @headers["Host"] ||= @uri.host @proxy, @body, @version = proxy, body, version end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
36 37 38 |
# File 'lib/http/request.rb', line 36 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
36 37 38 |
# File 'lib/http/request.rb', line 36 def headers @headers end |
#method ⇒ Object (readonly)
Method is given as a lowercase symbol e.g. :get, :post
31 32 33 |
# File 'lib/http/request.rb', line 31 def method @method end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
36 37 38 |
# File 'lib/http/request.rb', line 36 def proxy @proxy end |
#uri ⇒ Object (readonly)
“Request URI” as per RFC 2616 www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
35 36 37 |
# File 'lib/http/request.rb', line 35 def uri @uri end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
36 37 38 |
# File 'lib/http/request.rb', line 36 def version @version end |
Instance Method Details
#[](header) ⇒ Object
Obtain the given header
58 59 60 |
# File 'lib/http/request.rb', line 58 def [](header) @headers[canonicalize_header(header)] end |
#stream(socket) ⇒ Object
Stream the request to a socket
63 64 65 66 67 68 69 |
# File 'lib/http/request.rb', line 63 def stream(socket) path = uri.query ? "#{uri.path}?#{uri.query}" : uri.path path = "/" if path.empty? request_header = "#{method.to_s.upcase} #{path} HTTP/#{version}" rs = HTTP::RequestStream.new socket, body, @headers, request_header rs.stream end |