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.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
-
#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 ⇒ Object
-
#start(status = 200) {|@header, @body| ... } ⇒ Object
Receives a block passing it the header and body for you to work with.
Constructor Details
#initialize(socket) ⇒ HttpResponse
Returns a new instance of HttpResponse.
259 260 261 262 263 264 |
# File 'lib/mongrel.rb', line 259 def initialize(socket) @socket = socket @body = StringIO.new @status = 404 @header = HeaderOut.new(StringIO.new) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
254 255 256 |
# File 'lib/mongrel.rb', line 254 def body @body end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
255 256 257 |
# File 'lib/mongrel.rb', line 255 def header @header end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
253 254 255 |
# File 'lib/mongrel.rb', line 253 def socket @socket end |
#status ⇒ Object
Returns the value of attribute status.
256 257 258 |
# File 'lib/mongrel.rb', line 256 def status @status end |
Instance Method Details
#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.
302 303 304 305 306 |
# File 'lib/mongrel.rb', line 302 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.
278 279 280 281 |
# File 'lib/mongrel.rb', line 278 def reset @header.out.rewind @body.rewind end |
#send_body ⇒ Object
294 295 296 297 298 |
# File 'lib/mongrel.rb', line 294 def send_body @body.rewind # connection: close is also added to ensure that the client does not pipeline. @socket.write(@body.read) end |
#send_header ⇒ Object
288 289 290 291 292 |
# File 'lib/mongrel.rb', line 288 def send_header @header.out.rewind @socket.write(@header.out.read) @socket.write("\r\n") end |
#send_status ⇒ Object
283 284 285 286 |
# File 'lib/mongrel.rb', line 283 def send_status status = "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status]}\r\nContent-Length: #{@body.length}\r\nConnection: close\r\n" @socket.write(status) end |
#start(status = 200) {|@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.
270 271 272 273 274 |
# File 'lib/mongrel.rb', line 270 def start(status=200) @status = status.to_i yield @header, @body finished end |