Class | Mongrel::HttpResponse
In: | lib/mongrel/http_response.rb
Parent: | Object

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.

### Methods

done done= finished new reset send_body send_file send_header send_status socket_error start write

### Attributes

body | [W] |
body | [R] |
body_sent | [R] |
header | [R] |
header_sent | [R] |
socket | [R] |
status | [W] |
status | [R] |
status_sent | [R] |

### Public Class methods

new(socket)

### Public Instance methods

done()
done=(val)

Used during error conditions to mark the response as "done" so there isn‘t any more processing sent to the client.

finished()

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.

reset()

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.

send_body()
send_file(path, small_file = false)

Appends the contents of `path` to the response stream. The file is opened for binary reading and written in chunks to the socket.

Sendfile API support has been removed in 0.3.13.4 due to stability problems.

send_header()
send_status(content_length=@body.length)
socket_error(details)
start(status=200, finalize=false, reason=nil) {|@header, @body| ...}

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.

write(data)

[Validate]