Class: Mongrel2::HTTPResponse
Overview
The Mongrel2 HTTP Response class.
Direct Known Subclasses
Constant Summary collapse
- STATUS_LINE_FORMAT =
The format for building valid HTTP responses
"HTTP/1.1 %03d %s".freeze
- EOL =
A network End-Of-Line
"\r\n".freeze
- DEFAULT_CONTENT_TYPE =
The default content type
'application/octet-stream'.freeze
Constants included from Constants
Constants::DATA_DIR, Constants::DEFAULT_CONFIG_SCRIPT, Constants::DEFAULT_CONFIG_URI, Constants::DEFAULT_CONTROL_SOCKET, Constants::MAX_BROADCAST_IDENTS
Constants inherited from Response
Instance Attribute Summary collapse
-
#headers ⇒ Object
(also: #header)
readonly
The response headers (a Mongrel2::Table).
-
#status ⇒ Object
The HTTP status code.
Attributes inherited from Response
#body, #chunksize, #conn_id, #extended_reply_data, #extended_reply_filter, #request, #sender_id
Instance Method Summary collapse
-
#bodiless? ⇒ Boolean
Returns true if the response status means the response shouldn’t have a body.
-
#content_type ⇒ Object
Return the current response Content-Type.
-
#content_type=(type) ⇒ Object
Set the current response Content-Type.
-
#get_content_length ⇒ Object
Get the length of the body IO.
-
#handled? ⇒ Boolean
(also: #is_handled?)
Returns true if the response is ready to be sent to the client.
-
#header_data ⇒ Object
Return the current response header as a valid HTTP string after normalizing them.
-
#initialize(sender_id, conn_id, body = '', headers = {}) ⇒ HTTPResponse
constructor
Set up a few things specific to HTTP responses.
-
#keepalive=(value) ⇒ Object
(also: #pipelining_enabled=)
Set the Connection header to allow pipelined HTTP.
-
#keepalive? ⇒ Boolean
(also: #pipelining_enabled?)
Returns
true
if the response has pipelining enabled. -
#normalized_headers ⇒ Object
Get a copy of the response headers table with any auto-generated or calulated headers set.
-
#reset ⇒ Object
Clear any existing headers and body and restore them to their defaults.
-
#set_defaults ⇒ Object
Set up response default headers, etc.
-
#status_category ⇒ Object
Return the numeric category of the response’s status code (1-5).
-
#status_is_clienterror? ⇒ Boolean
Return true if response is in the 4XX range.
-
#status_is_informational? ⇒ Boolean
Return true if response is in the 1XX range.
-
#status_is_redirect? ⇒ Boolean
Return true if response is in the 3XX range.
-
#status_is_servererror? ⇒ Boolean
Return true if response is in the 5XX range.
-
#status_is_successful? ⇒ Boolean
Return true if response is in the 2XX range.
-
#status_line ⇒ Object
Send the response status to the client.
-
#to_s ⇒ Object
Stringify the response.
Methods inherited from Response
#<<, #each_chunk, #extend_reply_with, #extended_reply?, from_request, #inspect, #puts
Constructor Details
#initialize(sender_id, conn_id, body = '', headers = {}) ⇒ HTTPResponse
Set up a few things specific to HTTP responses
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mongrel2/httpresponse.rb', line 30 def initialize( sender_id, conn_id, body='', headers={} ) # :notnew: if body.is_a?( Hash ) headers = body body = '' end super( sender_id, conn_id, body ) @headers = Mongrel2::Table.new @status = nil self.set_defaults @headers.merge!( headers ) end |
Instance Attribute Details
#headers ⇒ Object (readonly) Also known as: header
The response headers (a Mongrel2::Table)
51 52 53 |
# File 'lib/mongrel2/httpresponse.rb', line 51 def headers @headers end |
#status ⇒ Object
The HTTP status code
55 56 57 |
# File 'lib/mongrel2/httpresponse.rb', line 55 def status @status end |
Instance Method Details
#bodiless? ⇒ Boolean
Returns true if the response status means the response shouldn’t have a body.
90 91 92 93 94 |
# File 'lib/mongrel2/httpresponse.rb', line 90 def bodiless? return self.extended_reply? || self.body.nil? || HTTP::BODILESS_HTTP_RESPONSE_CODES.include?( self.status ) end |
#content_type ⇒ Object
Return the current response Content-Type.
135 136 137 |
# File 'lib/mongrel2/httpresponse.rb', line 135 def content_type return self.headers[ :content_type ] end |
#content_type=(type) ⇒ Object
Set the current response Content-Type.
141 142 143 |
# File 'lib/mongrel2/httpresponse.rb', line 141 def content_type=( type ) return self.headers[ :content_type ] = type end |
#get_content_length ⇒ Object
Get the length of the body IO. If the IO’s offset is somewhere other than the beginning or end, the size of the remainder is used.
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mongrel2/httpresponse.rb', line 186 def get_content_length return 0 if self.bodiless? if self.body.pos.nonzero? && !self.body.eof? self.log.info "Calculating content length based on an offset of %d" % [ self.body.pos ] return self.body.size - self.body.pos else self.log.debug "Calculating body size via %p" % [ self.body.method(:size) ] return self.body.size end end |
#handled? ⇒ Boolean Also known as: is_handled?
Returns true if the response is ready to be sent to the client.
82 83 84 |
# File 'lib/mongrel2/httpresponse.rb', line 82 def handled? return ! @status.nil? end |
#header_data ⇒ Object
Return the current response header as a valid HTTP string after normalizing them.
160 161 162 |
# File 'lib/mongrel2/httpresponse.rb', line 160 def header_data return self.normalized_headers.to_s end |
#keepalive=(value) ⇒ Object Also known as: pipelining_enabled=
Set the Connection header to allow pipelined HTTP.
200 201 202 |
# File 'lib/mongrel2/httpresponse.rb', line 200 def keepalive=( value ) self.headers[:connection] = value ? 'keep-alive' : 'close' end |
#keepalive? ⇒ Boolean Also known as: pipelining_enabled?
Returns true
if the response has pipelining enabled.
207 208 209 210 211 |
# File 'lib/mongrel2/httpresponse.rb', line 207 def keepalive? ka_header = self.headers[:connection] return !ka_header.nil? && ka_header =~ /keep-alive/i return false end |
#normalized_headers ⇒ Object
Get a copy of the response headers table with any auto-generated or calulated headers set.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mongrel2/httpresponse.rb', line 167 def normalized_headers headers = self.headers.dup headers[:date] ||= Time.now.httpdate if self.bodiless? && !self.extended_reply? headers.delete( :content_length ) headers.delete( :content_type ) else headers[:content_length] ||= self.get_content_length headers[:content_type] ||= DEFAULT_CONTENT_TYPE.dup end return headers end |
#reset ⇒ Object
Clear any existing headers and body and restore them to their defaults
147 148 149 150 151 152 153 154 155 |
# File 'lib/mongrel2/httpresponse.rb', line 147 def reset @headers.clear @body.truncate( 0 ) @status = nil self.set_defaults return true end |
#set_defaults ⇒ Object
Set up response default headers, etc.
59 60 61 |
# File 'lib/mongrel2/httpresponse.rb', line 59 def set_defaults @headers[:server] = Mongrel2.version_string( true ) end |
#status_category ⇒ Object
Return the numeric category of the response’s status code (1-5)
98 99 100 101 |
# File 'lib/mongrel2/httpresponse.rb', line 98 def status_category return 0 if self.status.nil? return (self.status / 100).ceil end |
#status_is_clienterror? ⇒ Boolean
Return true if response is in the 4XX range
123 124 125 |
# File 'lib/mongrel2/httpresponse.rb', line 123 def status_is_clienterror? return self.status_category == 4 end |
#status_is_informational? ⇒ Boolean
Return true if response is in the 1XX range
105 106 107 |
# File 'lib/mongrel2/httpresponse.rb', line 105 def status_is_informational? return self.status_category == 1 end |
#status_is_redirect? ⇒ Boolean
Return true if response is in the 3XX range
117 118 119 |
# File 'lib/mongrel2/httpresponse.rb', line 117 def status_is_redirect? return self.status_category == 3 end |
#status_is_servererror? ⇒ Boolean
Return true if response is in the 5XX range
129 130 131 |
# File 'lib/mongrel2/httpresponse.rb', line 129 def status_is_servererror? return self.status_category == 5 end |
#status_is_successful? ⇒ Boolean
Return true if response is in the 2XX range
111 112 113 |
# File 'lib/mongrel2/httpresponse.rb', line 111 def status_is_successful? return self.status_category == 2 end |
#status_line ⇒ Object
Send the response status to the client
75 76 77 78 |
# File 'lib/mongrel2/httpresponse.rb', line 75 def status_line st = self.status || self.derived_status_code return STATUS_LINE_FORMAT % [ st, HTTP::STATUS_NAME[st] ] end |
#to_s ⇒ Object
Stringify the response
65 66 67 68 69 70 71 |
# File 'lib/mongrel2/httpresponse.rb', line 65 def to_s return [ self.status_line, self.header_data, self.bodiless? ? '' : super ].join( "\r\n" ) end |