class Mongrel::HttpResponse

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.