Class: Mongrel2::Response

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Defined in:
lib/mongrel2/response.rb

Overview

The Mongrel2 Response base class.

Direct Known Subclasses

HTTPResponse, WebSocket::Response

Constant Summary collapse

DEFAULT_CHUNKSIZE =

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

1024 * 512

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender_id, conn_id, body = '') ⇒ Response

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mongrel2/response.rb', line 35

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

Instance Attribute Details

#bodyObject

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



61
62
63
# File 'lib/mongrel2/response.rb', line 61

def body
  @body
end

#chunksizeObject

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



67
68
69
# File 'lib/mongrel2/response.rb', line 67

def chunksize
  @chunksize
end

#conn_idObject

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



58
59
60
# File 'lib/mongrel2/response.rb', line 58

def conn_id
  @conn_id
end

#extended_reply_dataObject (readonly)

The Array of data to include with the extended reply



74
75
76
# File 'lib/mongrel2/response.rb', line 74

def extended_reply_data
  @extended_reply_data
end

#extended_reply_filterObject (readonly)

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.



71
72
73
# File 'lib/mongrel2/response.rb', line 71

def extended_reply_filter
  @extended_reply_filter
end

#requestObject

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



64
65
66
# File 'lib/mongrel2/response.rb', line 64

def request
  @request
end

#sender_idObject

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



54
55
56
# File 'lib/mongrel2/response.rb', line 54

def sender_id
  @sender_id
end

Class Method Details

.from_request(request) ⇒ Object

Create a response to the specified request and return it.



25
26
27
28
29
30
31
# File 'lib/mongrel2/response.rb', line 25

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

Instance Method Details

#<<(object) ⇒ Object

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



87
88
89
90
# File 'lib/mongrel2/response.rb', line 87

def <<( object )
	self.body << object
	return self
end

#each_chunkObject

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



126
127
128
129
130
131
132
# File 'lib/mongrel2/response.rb', line 126

def each_chunk
	if block_given?
		yield( self.to_s )
	else
		return [ self.to_s ].to_enum
	end
end

#extend_reply_with(filter) ⇒ Object Also known as: extended_reply_with

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.



102
103
104
# File 'lib/mongrel2/response.rb', line 102

def extend_reply_with( filter )
	@extended_reply_filter = filter
end

#extended_reply?Boolean

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/mongrel2/response.rb', line 109

def extended_reply?
	return @extended_reply_filter ? true : false
end

#inspectObject

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



145
146
147
148
149
150
151
152
# File 'lib/mongrel2/response.rb', line 145

def inspect
	return "#<%p:0x%016x %s (%s)>" % [
		self.class,
		self.object_id * 2,
		self.inspect_details,
		self.socket_id
	]
end

#puts(*objects) ⇒ Object

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



94
95
96
# File 'lib/mongrel2/response.rb', line 94

def puts( *objects )
	self.body.puts( *objects )
end

#socket_idObject

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



137
138
139
# File 'lib/mongrel2/response.rb', line 137

def socket_id
	return "%s:%d" % [ self.sender_id, self.conn_id ]
end

#to_sObject

Stringify the response, which just returns its body.



115
116
117
118
119
120
121
# File 'lib/mongrel2/response.rb', line 115

def to_s
	pos = self.body.pos
	self.body.pos = 0
	return self.body.read
ensure
	self.body.pos = pos
end