HTTPResponse

class
Superclass
Mongrel2::Response
Included Modules
Mongrel2::Constants
Extended With
Loggability

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

header[R]

The response headers (a Mongrel2::Table)

headers[R]

The response headers (a Mongrel2::Table)

status[RW]

The HTTP status code

Public Instance Methods

anchor
bodiless?()

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
anchor
content_type()

Return the current response Content-Type.

# File lib/mongrel2/httpresponse.rb, line 135
def content_type
        return self.headers[ :content_type ]
end
anchor
content_type=( type )

Set the current response Content-Type.

# File lib/mongrel2/httpresponse.rb, line 141
def content_type=( type )
        return self.headers[ :content_type ] = type
end
anchor
get_content_length()

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
anchor
handled?()

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
Also aliased as: is_handled?
anchor
header_data()

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
anchor
is_handled?()
Alias for: handled?
anchor
keepalive=( value )

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
Also aliased as: pipelining_enabled=
anchor
keepalive?()

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
Also aliased as: pipelining_enabled?
anchor
normalized_headers()

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
anchor
pipelining_enabled=( value )
Alias for: keepalive=
anchor
pipelining_enabled?()
Alias for: keepalive?
anchor
reset()

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
anchor
set_defaults()

Set up response default headers, etc.

# File lib/mongrel2/httpresponse.rb, line 59
def set_defaults
        @headers[:server] = Mongrel2.version_string( true )
end
anchor
status_category()

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
anchor
status_is_clienterror?()

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
anchor
status_is_informational?()

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
anchor
status_is_redirect?()

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
anchor
status_is_servererror?()

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
anchor
status_is_successful?()

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
anchor
status_line()

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
anchor
to_s()

Stringify the response

# 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

anchor
derived_status_code()

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
anchor
initialize( sender_id, conn_id, body='', headers={} )

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
anchor
inspect_details()

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