Methods

Unicorn::HttpResponse

Writes a Rack response to your client using the HTTP/1.1 specification. You use it by simply doing:

  status, headers, body = rack_app.call(env)
  HttpResponse.write(socket, [ status, headers, body ])

Most header correctness (including Content-Length and Content-Type) is the job of Rack, with the exception of the “Connection: close” and “Date” headers.

A design decision was made to force the client to not pipeline or keepalive 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 always 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.

Constants

CODES

Every standard HTTP code mapped to the appropriate message.

SKIP

Rack does not set/require a Date: header. We always override the Connection: and Date: headers no matter what (if anything) our Rack application sent us.

Public Class Methods

write(socket, rack_response, have_header = true) click to toggle source

writes the rack_response to socket as an HTTP response

    # File lib/unicorn/http_response.rb, line 35
35:   def self.write(socket, rack_response, have_header = true)
36:     status, headers, body = rack_response
37: 
38:     if have_header
39:       status = CODES[status.to_i] || status
40:       out = []
41: 
42:       # Don't bother enforcing duplicate supression, it's a Hash most of
43:       # the time anyways so just hope our app knows what it's doing
44:       headers.each do |key, value|
45:         next if SKIP.include?(key.downcase)
46:         if value =~ /\n/
47:           # avoiding blank, key-only cookies with /\n+/
48:           out.concat(value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" })
49:         else
50:           out << "#{key}: #{value}\r\n"
51:         end
52:       end
53: 
54:       # Rack should enforce Content-Length or chunked transfer encoding,
55:       # so don't worry or care about them.
56:       # Date is required by HTTP/1.1 as long as our clock can be trusted.
57:       # Some broken clients require a "Status" header so we accomodate them
58:       socket.write("HTTP/1.1 #{status}\r\n"                     "Date: #{Time.now.httpdate}\r\n"                     "Status: #{status}\r\n"                     "Connection: close\r\n"                     "#{out.join('')}\r\n")
59:     end
60: 
61:     body.each { |chunk| socket.write(chunk) }
62:     socket.close # flushes and uncorks the socket immediately
63:     ensure
64:       body.respond_to?(:close) and body.close
65:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.