module 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)
http_response_write(socket, status, headers, body)

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

Constants

CODES

Every standard HTTP code mapped to the appropriate message.

CRLF

Public Instance Methods

err_response(code, response_start_sent) click to toggle source
# File lib/unicorn/http_response.rb, line 19
def err_response(code, response_start_sent)
  "#{response_start_sent ? '' : 'HTTP/1.1 '}#{CODES[code]}\r\n\r\n"
end
hijack_prepare(value) click to toggle source
# File lib/unicorn/http_response.rb, line 67
def hijack_prepare(value)
  value
end
http_response_write(socket, status, headers, body, response_start_sent=false) click to toggle source

writes the rack_response to socket as an HTTP response

# File lib/unicorn/http_response.rb, line 24
def http_response_write(socket, status, headers, body,
                        response_start_sent=false)
  status = CODES[status.to_i] || status
  hijack = nil

  http_response_start = response_start_sent ? '' : 'HTTP/1.1 '
  if headers
    buf = "#{http_response_start}#{status}\r\n"              "Date: #{httpdate}\r\n"              "Status: #{status}\r\n"              "Connection: close\r\n"
    headers.each do |key, value|
      case key
      when %r{\A(?:Date\z|Connection\z)}
        next
      when "rack.hijack"
        # this was an illegal key in Rack < 1.5, so it should be
        # OK to silently discard it for those older versions
        hijack = hijack_prepare(value)
      else
        if value =~ /\n/
          # avoiding blank, key-only cookies with /\n+/
          buf << value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }.join
        else
          buf << "#{key}: #{value}\r\n"
        end
      end
    end
    socket.write(buf << CRLF)
  end

  if hijack
    body = nil # ensure we do not close body
    hijack.call(socket)
  else
    body.each { |chunk| socket.write(chunk) }
  end
ensure
  body.respond_to?(:close) and body.close
end