Class: HTTP::Request

Inherits:
Object
  • Object
show all
Includes:
Header
Defined in:
lib/http/request.rb

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

Header::CANONICAL_HEADER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Header

#canonicalize_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

#bodyObject (readonly)

Returns the value of attribute body.



36
37
38
# File 'lib/http/request.rb', line 36

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



36
37
38
# File 'lib/http/request.rb', line 36

def headers
  @headers
end

#methodObject (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

#proxyObject (readonly)

Returns the value of attribute proxy.



36
37
38
# File 'lib/http/request.rb', line 36

def proxy
  @proxy
end

#uriObject (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

#versionObject (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