class Mongrel2::HTTPResponse
The Mongrel2
HTTP Response class.
Constants
- DEFAULT_CONTENT_TYPE
The default content type
- EOL
A network End-Of-Line
- STATUS_LINE_FORMAT
The format for building valid HTTP responses
Attributes
The response headers (a Mongrel2::Table
)
The response headers (a Mongrel2::Table
)
The HTTP status code
Public Instance Methods
Returns true if the response status means the response shouldn't have a body.
# 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
Return the current response Content-Type.
# File lib/mongrel2/httpresponse.rb, line 135 def content_type return self.headers[ :content_type ] end
Set the current response Content-Type.
# File lib/mongrel2/httpresponse.rb, line 141 def content_type=( type ) return self.headers[ :content_type ] = type end
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.
# 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
Returns true if the response is ready to be sent to the client.
# File lib/mongrel2/httpresponse.rb, line 82 def handled? return ! @status.nil? end
Return the current response header as a valid HTTP string after normalizing them.
# File lib/mongrel2/httpresponse.rb, line 160 def header_data return self.normalized_headers.to_s end
Set the Connection header to allow pipelined HTTP.
# File lib/mongrel2/httpresponse.rb, line 200 def keepalive=( value ) self.headers[:connection] = value ? 'keep-alive' : 'close' end
Returns true
if the response has pipelining enabled.
# 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
Get a copy of the response headers table with any auto-generated or calulated headers set.
# 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
Clear any existing headers and body and restore them to their defaults
# File lib/mongrel2/httpresponse.rb, line 147 def reset @headers.clear @body.truncate( 0 ) @status = nil self.set_defaults return true end
Set up response default headers, etc.
# File lib/mongrel2/httpresponse.rb, line 59 def set_defaults @headers[:server] = Mongrel2.version_string( true ) end
Return the numeric category of the response's status code (1-5)
# File lib/mongrel2/httpresponse.rb, line 98 def status_category return 0 if self.status.nil? return (self.status / 100).ceil end
Return true if response is in the 4XX range
# File lib/mongrel2/httpresponse.rb, line 123 def status_is_clienterror? return self.status_category == 4 end
Return true if response is in the 1XX range
# File lib/mongrel2/httpresponse.rb, line 105 def status_is_informational? return self.status_category == 1 end
Return true if response is in the 3XX range
# File lib/mongrel2/httpresponse.rb, line 117 def status_is_redirect? return self.status_category == 3 end
Return true if response is in the 5XX range
# File lib/mongrel2/httpresponse.rb, line 129 def status_is_servererror? return self.status_category == 5 end
Return true if response is in the 2XX range
# File lib/mongrel2/httpresponse.rb, line 111 def status_is_successful? return self.status_category == 2 end
Send the response status to the client
# 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
Stringify the response
Mongrel2::Response#to_s
# File lib/mongrel2/httpresponse.rb, line 65 def to_s return [ self.status_line, self.header_data, self.bodiless? ? '' : super ].join( "\r\n" ) end
Protected Instance Methods
Return the numeric HTTP status code for the response bsaed on what has already been set
# File lib/mongrel2/httpresponse.rb, line 231 def derived_status_code # If there's a non-empty entity body, or the content length has been set explicitly # to something non-zero, assume the response is OK if self.body.size.nonzero? || (self.header.content_length && self.header.content_length.nonzero?) return HTTP::OK # otherwise set it to 204 else return HTTP::NO_CONTENT end end
Set up a few things specific to HTTP responses
# 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
Return the details to include in the contents of the inspected object.
# File lib/mongrel2/httpresponse.rb, line 220 def inspect_details return %Q{%s -- %d headers, %0.2fK body (%p)} % [ self.status_line, self.headers.length, (self.get_content_length / 1024.0), self.body, ] end