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, #socket_id
Constructor Details
permalink #initialize(sender_id, conn_id, body = '', headers = {}) ⇒ HTTPResponse
Set up a few things specific to HTTP responses
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongrel2/httpresponse.rb', line 31 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
permalink #headers ⇒ Object (readonly) Also known as: header
The response headers (a Mongrel2::Table)
52 53 54 |
# File 'lib/mongrel2/httpresponse.rb', line 52 def headers @headers end |
permalink #status ⇒ Object
The HTTP status code
56 57 58 |
# File 'lib/mongrel2/httpresponse.rb', line 56 def status @status end |
Instance Method Details
permalink #bodiless? ⇒ Boolean
Returns true if the response status means the response shouldn’t have a body.
91 92 93 94 95 |
# File 'lib/mongrel2/httpresponse.rb', line 91 def bodiless? return self.extended_reply? || self.body.nil? || HTTP::BODILESS_HTTP_RESPONSE_CODES.include?( self.status ) end |
permalink #content_type ⇒ Object
Return the current response Content-Type.
136 137 138 |
# File 'lib/mongrel2/httpresponse.rb', line 136 def content_type return self.headers[ :content_type ] end |
permalink #content_type=(type) ⇒ Object
Set the current response Content-Type.
142 143 144 |
# File 'lib/mongrel2/httpresponse.rb', line 142 def content_type=( type ) return self.headers[ :content_type ] = type end |
permalink #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.
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mongrel2/httpresponse.rb', line 187 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 |
permalink #handled? ⇒ Boolean Also known as: is_handled?
Returns true if the response is ready to be sent to the client.
83 84 85 |
# File 'lib/mongrel2/httpresponse.rb', line 83 def handled? return ! @status.nil? end |
permalink #header_data ⇒ Object
Return the current response header as a valid HTTP string after normalizing them.
161 162 163 |
# File 'lib/mongrel2/httpresponse.rb', line 161 def header_data return self.normalized_headers.to_s end |
permalink #keepalive=(value) ⇒ Object Also known as: pipelining_enabled=
Set the Connection header to allow pipelined HTTP.
201 202 203 |
# File 'lib/mongrel2/httpresponse.rb', line 201 def keepalive=( value ) self.headers[:connection] = value ? 'keep-alive' : 'close' end |
permalink #keepalive? ⇒ Boolean Also known as: pipelining_enabled?
Returns true
if the response has pipelining enabled.
208 209 210 211 212 |
# File 'lib/mongrel2/httpresponse.rb', line 208 def keepalive? ka_header = self.headers[:connection] return !ka_header.nil? && ka_header =~ /keep-alive/i return false end |
permalink #normalized_headers ⇒ Object
Get a copy of the response headers table with any auto-generated or calulated headers set.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/mongrel2/httpresponse.rb', line 168 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 |
permalink #reset ⇒ Object
Clear any existing headers and body and restore them to their defaults
148 149 150 151 152 153 154 155 156 |
# File 'lib/mongrel2/httpresponse.rb', line 148 def reset @headers.clear @body.truncate( 0 ) @status = nil self.set_defaults return true end |
permalink #set_defaults ⇒ Object
Set up response default headers, etc.
60 61 62 |
# File 'lib/mongrel2/httpresponse.rb', line 60 def set_defaults @headers[:server] = Mongrel2.version_string( true ) end |
permalink #status_category ⇒ Object
Return the numeric category of the response’s status code (1-5)
99 100 101 102 |
# File 'lib/mongrel2/httpresponse.rb', line 99 def status_category return 0 if self.status.nil? return (self.status / 100).ceil end |
permalink #status_is_clienterror? ⇒ Boolean
Return true if response is in the 4XX range
124 125 126 |
# File 'lib/mongrel2/httpresponse.rb', line 124 def status_is_clienterror? return self.status_category == 4 end |
permalink #status_is_informational? ⇒ Boolean
Return true if response is in the 1XX range
106 107 108 |
# File 'lib/mongrel2/httpresponse.rb', line 106 def status_is_informational? return self.status_category == 1 end |
permalink #status_is_redirect? ⇒ Boolean
Return true if response is in the 3XX range
118 119 120 |
# File 'lib/mongrel2/httpresponse.rb', line 118 def status_is_redirect? return self.status_category == 3 end |
permalink #status_is_servererror? ⇒ Boolean
Return true if response is in the 5XX range
130 131 132 |
# File 'lib/mongrel2/httpresponse.rb', line 130 def status_is_servererror? return self.status_category == 5 end |
permalink #status_is_successful? ⇒ Boolean
Return true if response is in the 2XX range
112 113 114 |
# File 'lib/mongrel2/httpresponse.rb', line 112 def status_is_successful? return self.status_category == 2 end |
permalink #status_line ⇒ Object
Send the response status to the client
76 77 78 79 |
# File 'lib/mongrel2/httpresponse.rb', line 76 def status_line st = self.status || self.derived_status_code return STATUS_LINE_FORMAT % [ st, HTTP::STATUS_NAME[st] ] end |
permalink #to_s ⇒ Object
Stringify the response
66 67 68 69 70 71 72 |
# File 'lib/mongrel2/httpresponse.rb', line 66 def to_s return [ self.status_line, self.header_data, self.bodiless? ? '' : super ].join( "\r\n" ) end |