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.
220 221 222 223 224 225 |
# File 'lib/mongrel.rb', line 220 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.
215 216 217 |
# File 'lib/mongrel.rb', line 215 def body @body end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
216 217 218 |
# File 'lib/mongrel.rb', line 216 def header @header end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
214 215 216 |
# File 'lib/mongrel.rb', line 214 def socket @socket end |
#status ⇒ Object
Returns the value of attribute status.
217 218 219 |
# File 'lib/mongrel.rb', line 217 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.
263 264 265 266 267 |
# File 'lib/mongrel.rb', line 263 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.
239 240 241 242 |
# File 'lib/mongrel.rb', line 239 def reset @header.out.rewind @body.rewind end |
#send_body ⇒ Object
254 255 256 257 258 259 |
# File 'lib/mongrel.rb', line 254 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
248 249 250 251 252 |
# File 'lib/mongrel.rb', line 248 def send_header @header.out.rewind @socket.write(@header.out.read) @socket.write("\r\n") end |
#send_status ⇒ Object
244 245 246 |
# File 'lib/mongrel.rb', line 244 def send_status @socket.write("HTTP/1.1 #{@status.to_i} #{HTTP_STATUS_CODES[@status.to_i]}\r\nContent-Length:#{body.length}\r\nConnection: close\r\n") 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.
231 232 233 234 235 |
# File 'lib/mongrel.rb', line 231 def start(status=200) @status = status.to_i yield @header, @body finished end |