Module: FTW::HTTP::Message
Overview
HTTP Message, RFC2616 For specification, see RFC2616 section 4: <tools.ietf.org/html/rfc2616#section-4>
You probably won’t use this class much. Instead, check out Request and Response
Constant Summary collapse
- VALID_VERSIONS =
HTTP Versions that are valid.
[1.0, 1.1]
Constants included from CRLF
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
The HTTP headers - See Headers.
-
#version ⇒ Object
The HTTP version.
Instance Method Summary collapse
-
#body ⇒ Object
Get the body of this message.
-
#body=(message_body) ⇒ Object
Set the body of this message.
-
#body? ⇒ Boolean
Does this message have a body?.
-
#content? ⇒ Boolean
Should this message have a content?.
-
#initialize ⇒ Object
A new HTTP message.
-
#to_s ⇒ Object
Serialize this Request according to RFC2616 Note: There is NO trailing CRLF.
Instance Attribute Details
#headers ⇒ Object (readonly)
The HTTP headers - See Headers. RFC2616 5.3 - <tools.ietf.org/html/rfc2616#section-5.3>
14 15 16 |
# File 'lib/ftw/http/message.rb', line 14 def headers @headers end |
#version ⇒ Object
The HTTP version. See VALID_VERSIONS for valid versions. This will always be a Numeric object. Both Request and Responses have version, so put it in the parent class.
19 20 21 |
# File 'lib/ftw/http/message.rb', line 19 def version @version end |
Instance Method Details
#body ⇒ Object
Get the body of this message
Returns an Enumerable, IO-like object, or String, depending on how this message was built.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ftw/http/message.rb', line 73 def body # TODO(sissel): verification todos follow... # TODO(sissel): RFC2616 section 4.3 - if there is a message body # then one of "Transfer-Encoding" *or* "Content-Length" MUST be present. # otherwise, if neither header is present, no body is present. # TODO(sissel): Responses to HEAD requests or those with status 1xx, 204, # or 304 MUST NOT have a body. All other requests have a message body, # even if that body is of zero length. return @body end |
#body=(message_body) ⇒ Object
Set the body of this message
The ‘message_body’ can be an IO-like object, Enumerable, or String.
See RFC2616 section 4.3: <tools.ietf.org/html/rfc2616#section-4.3>
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ftw/http/message.rb', line 52 def body=() # TODO(sissel): if message_body is a string, set Content-Length header # TODO(sissel): if it's an IO object, set Transfer-Encoding to chunked # TODO(sissel): if it responds to each or appears to be Enumerable, then # set Transfer-Encoding to chunked. @body = # don't set any additional length/encoding headers if they are already set. return if headers.include?("Content-Length") or headers.include?("Transfer-Encoding") if (.respond_to?(:read) or .respond_to?(:each)) and headers["Transfer-Encoding"] = "chunked" else headers["Content-Length"] = .bytesize end end |
#body? ⇒ Boolean
Does this message have a body?
94 95 96 |
# File 'lib/ftw/http/message.rb', line 94 def body? return !@body.nil? end |
#content? ⇒ Boolean
Should this message have a content?
In HTTP 1.1, there is a body if response sets Content-Length or Transfer-Encoding, it has a body. Otherwise, there is no body.
88 89 90 91 |
# File 'lib/ftw/http/message.rb', line 88 def content? return (headers.include?("Content-Length") and headers["Content-Length"].to_i > 0) \ || headers.include?("Transfer-Encoding") end |
#initialize ⇒ Object
A new HTTP message.
27 28 29 30 |
# File 'lib/ftw/http/message.rb', line 27 def initialize @headers = FTW::HTTP::Headers.new @body = nil end |
#to_s ⇒ Object
Serialize this Request according to RFC2616 Note: There is NO trailing CRLF. This is intentional. The RFC defines:
generic-message = start-line
*(message-header CRLF)
CRLF
[ message-body ]
Thus, the CRLF between header and body is not part of the header.
118 119 120 |
# File 'lib/ftw/http/message.rb', line 118 def to_s return [start_line, @headers].join(CRLF) end |