Class: Mongrel::HttpResponse
- Inherits:
-
Object
- Object
- Mongrel::HttpResponse
- Defined in:
- lib/mongrel.rb
Overview
Writes and controls your response to the client using the HTTP/1.1 specification. You use it by simply doing:
response.start(200) do |head,out|
head['Content-Type'] = 'text/plain'
out.write("hello\n")
end
The parameter to start is the response code–which Mongrel will translate for you based on HTTP_STATUS_CODES. The head parameter is how you write custom headers. The out parameter is where you write your body. The default status code for HttpResponse.start is 200 so the above example is redundant.
As you can see, it’s just like using a Hash and as you do this it writes the proper header to the output on the fly. You can even intermix specifying headers and writing content. The HttpResponse class with write the things in the proper order once the HttpResponse.block is ended.
You may also work the HttpResponse object directly using the various attributes available for the raw socket, body, header, and status codes. If you do this you’re on your own. A design decision was made to force the client to not pipeline requests. HTTP/1.1 pipelining really kills the performance due to how it has to be handled and how unclear the standard is. To fix this the HttpResponse gives a “Connection: close” header which forces the client to close right away. The bonus for this is that it gives a pretty nice speed boost to most clients since they can close their connection immediately.
One additional caveat is that you don’t have to specify the Content-length header as the HttpResponse will write this for you based on the out length.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#body_sent ⇒ Object
readonly
Returns the value of attribute body_sent.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#header_sent ⇒ Object
readonly
Returns the value of attribute header_sent.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#status ⇒ Object
Returns the value of attribute status.
-
#status_sent ⇒ Object
readonly
Returns the value of attribute status_sent.
Instance Method Summary collapse
- #done ⇒ Object
-
#finished ⇒ Object
This takes whatever has been done to header and body and then writes it in the proper format to make an HTTP/1.1 response.
-
#initialize(socket) ⇒ HttpResponse
constructor
A new instance of HttpResponse.
-
#reset ⇒ Object
Primarily used in exception handling to reset the response output in order to write an alternative response.
- #send_body ⇒ Object
- #send_header ⇒ Object
- #send_status(content_length = nil) ⇒ Object
-
#start(status = 200, finalize = false) {|@header, @body| ... } ⇒ Object
Receives a block passing it the header and body for you to work with.
- #write(data) ⇒ Object
Constructor Details
#initialize(socket) ⇒ HttpResponse
Returns a new instance of HttpResponse.
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/mongrel.rb', line 263 def initialize(socket) @socket = socket @body = StringIO.new @status = 404 @header = HeaderOut.new(StringIO.new) @header[Const::DATE] = Time.now.httpdate @body_sent = false @header_sent = false @status_sent = false end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
255 256 257 |
# File 'lib/mongrel.rb', line 255 def body @body end |
#body_sent ⇒ Object (readonly)
Returns the value of attribute body_sent.
259 260 261 |
# File 'lib/mongrel.rb', line 259 def body_sent @body_sent end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
256 257 258 |
# File 'lib/mongrel.rb', line 256 def header @header end |
#header_sent ⇒ Object (readonly)
Returns the value of attribute header_sent.
260 261 262 |
# File 'lib/mongrel.rb', line 260 def header_sent @header_sent end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
254 255 256 |
# File 'lib/mongrel.rb', line 254 def socket @socket end |
#status ⇒ Object
Returns the value of attribute status.
257 258 259 |
# File 'lib/mongrel.rb', line 257 def status @status end |
#status_sent ⇒ Object (readonly)
Returns the value of attribute status_sent.
261 262 263 |
# File 'lib/mongrel.rb', line 261 def status_sent @status_sent end |
Instance Method Details
#done ⇒ Object
339 340 341 |
# File 'lib/mongrel.rb', line 339 def done (@status_sent and @header_sent and @body_sent) end |
#finished ⇒ Object
This takes whatever has been done to header and body and then writes it in the proper format to make an HTTP/1.1 response.
333 334 335 336 337 |
# File 'lib/mongrel.rb', line 333 def finished send_status send_header send_body end |
#reset ⇒ Object
Primarily used in exception handling to reset the response output in order to write an alternative response. It will abort with an exception if you have already sent the header or the body. This is pretty catastrophic actually.
291 292 293 294 295 296 297 298 299 300 |
# File 'lib/mongrel.rb', line 291 def reset if @body_sent raise "You have already sent the request body." elsif @header_sent raise "You have already sent the request headers." else @header.out.rewind @body.rewind end end |
#send_body ⇒ Object
318 319 320 321 322 323 324 325 |
# File 'lib/mongrel.rb', line 318 def send_body if not @body_sent @body.rewind # connection: close is also added to ensure that the client does not pipeline. @socket.write(@body.read) @body_sent = true end end |
#send_header ⇒ Object
310 311 312 313 314 315 316 |
# File 'lib/mongrel.rb', line 310 def send_header if not @header_sent @header.out.rewind @socket.write(@header.out.read + Const::LINE_END) @header_sent = true end end |
#send_status(content_length = nil) ⇒ Object
302 303 304 305 306 307 308 |
# File 'lib/mongrel.rb', line 302 def send_status(content_length=nil) if not @status_sent content_length ||= @body.length @socket.write(Const::STATUS_FORMAT % [status, HTTP_STATUS_CODES[@status], content_length]) @status_sent = true end end |
#start(status = 200, finalize = false) {|@header, @body| ... } ⇒ Object
Receives a block passing it the header and body for you to work with. When the block is finished it writes everything you’ve done to the socket in the proper order. This lets you intermix header and body content as needed. Handlers are able to modify pretty much any part of the request in the chain, and can stop further processing by simple passing “finalize=true” to the start method. By default all handlers run and then mongrel finalizes the request when they’re all done.
282 283 284 285 286 |
# File 'lib/mongrel.rb', line 282 def start(status=200, finalize=false) @status = status.to_i yield @header, @body finished if finalize end |
#write(data) ⇒ Object
327 328 329 |
# File 'lib/mongrel.rb', line 327 def write(data) @socket.write(data) end |