class Mongrel2::Response

The Mongrel2 Response base class.

Constants

DEFAULT_CHUNKSIZE

The default number of bytes of the response body to send to the mongrel2 server at a time.

Attributes

body[R]

The body of the response as an IO (or IOish) object

chunksize[RW]

The number of bytes to write to Mongrel in a single “chunk”

conn_id[RW]

The response's connection ID; this corresponds to the identifier of the connection the response will be routed to by the mongrel2 server

extended_reply_data[R]

The Array of data to include with the extended reply

extended_reply_filter[R]

The name of the extended reply filter to use in the reply. If this is set the response will be send back to Mongrel as an extended reply.

request[RW]

The request that this response is for, if there is one

sender_id[RW]

The response's UUID; this corresponds to the mongrel2 server the response will be routed to by the Connection.

Public Class Methods

from_request( request ) click to toggle source

Create a response to the specified request and return it.

# File lib/mongrel2/response.rb, line 24
def self::from_request( request )
        self.log.debug "Creating a %p to request %p" % [ self, request ]
        response = new( request.sender_id, request.conn_id )
        response.request = request

        return response
end
new( sender_id, conn_id, body='' ) click to toggle source

Create a new Response object for the specified sender_id, conn_id, and body.

# File lib/mongrel2/response.rb, line 34
def initialize( sender_id, conn_id, body='' )
        body = StringIO.new( body.dup, 'a+' ) unless body.respond_to?( :read )

        @sender_id             = sender_id
        @conn_id               = conn_id
        @body                  = body
        @request               = nil
        @chunksize             = DEFAULT_CHUNKSIZE
        @extended_reply_filter = nil
        @extended_reply_data   = []
end

Public Instance Methods

<<( object ) click to toggle source

Append the given object to the response body. Returns the response for chaining.

# File lib/mongrel2/response.rb, line 86
def <<( object )
        self.body << object
        return self
end
body=( newbody ) click to toggle source

Set the response's entity body to newbody. If newbody is a String-ish object (i.e., it responds to to_str), it will be wrapped in a StringIO in 'a+' mode).

# File lib/mongrel2/response.rb, line 78
def body=( newbody )
        newbody = StringIO.new( newbody.dup, 'a+' ) if newbody.respond_to?( :to_str )
        @body = newbody
end
each_chunk() { |self| ... } click to toggle source

Yield chunks of the response to the caller's block. By default, just yields the result of calling to_s on the response.

# File lib/mongrel2/response.rb, line 125
def each_chunk
        if block_given?
                yield( self.to_s )
        else
                return [ self.to_s ].to_enum
        end
end
extend_reply_with( filter ) click to toggle source

Set up the response to send an extended reply to Mongrel2, invoking the given filter. The body of the response will be passed to the filter after being serialized to a tnetstring.

# File lib/mongrel2/response.rb, line 101
def extend_reply_with( filter )
        @extended_reply_filter = filter
end
Also aliased as: extended_reply_with
extended_reply?() click to toggle source

Returns true if the response has been set to use an extended reply.

# File lib/mongrel2/response.rb, line 108
def extended_reply?
        return @extended_reply_filter ? true : false
end
extended_reply_with( filter )
Alias for: extend_reply_with
inspect() click to toggle source

Returns a string containing a human-readable representation of the Response, suitable for debugging.

# File lib/mongrel2/response.rb, line 144
def inspect
        return "#<%p:0x%016x %s (%s)>" % [
                self.class,
                self.object_id * 2,
                self.inspect_details,
                self.socket_id
        ]
end
puts( *objects ) click to toggle source

Write the given objects to the response body, calling to_s on each one.

# File lib/mongrel2/response.rb, line 93
def puts( *objects )
        self.body.puts( *objects )
end
socket_id() click to toggle source

Returns a string containing the request's sender and connection IDs separated by a colon.

# File lib/mongrel2/response.rb, line 136
def socket_id
        return "%s:%d" % [ self.sender_id, self.conn_id ]
end
to_s() click to toggle source

Stringify the response, which just returns its body.

# File lib/mongrel2/response.rb, line 114
def to_s
        pos = self.body.pos
        self.body.pos = 0
        return self.body.read
ensure
        self.body.pos = pos
end

Protected Instance Methods

inspect_details() click to toggle source

Return the details to include in the contents of the inspected object. This method allows other request types to provide their own details while keeping the form somewhat consistent.

# File lib/mongrel2/response.rb, line 161
def inspect_details
        return "%p body" % [ self.body.class ]
end