1 # -*- encoding: binary -*-
3 # Writes a Rack response to your client using the HTTP/1.1 specification.
4 # You use it by simply doing:
6 # status, headers, body = rack_app.call(env)
7 # http_response_write(socket, status, headers, body)
9 # Most header correctness (including Content-Length and Content-Type)
10 # is the job of Rack, with the exception of the "Date" and "Status" header.
11 module Unicorn::HttpResponse
13 STATUS_CODES = defined?(Rack::Utils::HTTP_STATUS_CODES) ?
14 Rack::Utils::HTTP_STATUS_CODES : {}
15 STATUS_WITH_NO_ENTITY_BODY = defined?(
16 Rack::Utils::STATUS_WITH_NO_ENTITY_BODY) ?
17 Rack::Utils::STATUS_WITH_NO_ENTITY_BODY : begin
18 warn 'Rack::Utils::STATUS_WITH_NO_ENTITY_BODY missing'
22 # internal API, code will always be common-enough-for-even-old-Rack
23 def err_response(code, response_start_sent)
24 "#{response_start_sent ? '' : 'HTTP/1.1 '}" \
25 "#{code} #{STATUS_CODES[code]}\r\n\r\n"
28 def append_header(buf, key, value)
31 value.each { |v| buf << "#{key}: #{v}\r\n" }
33 # avoiding blank, key-only cookies with /\n+/
34 value.split(/\n+/).each { |v| buf << "#{key}: #{v}\r\n" }
36 buf << "#{key}: #{value}\r\n"
40 # writes the rack_response to socket as an HTTP response
41 def http_response_write(socket, status, headers, body,
42 req = Unicorn::HttpRequest.new)
47 msg = STATUS_CODES[code]
48 start = req.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
49 term = STATUS_WITH_NO_ENTITY_BODY.include?(code) || false
50 buf = "#{start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
51 "Date: #{httpdate}\r\n" \
52 "Connection: close\r\n"
53 headers.each do |key, value|
55 when %r{\A(?:Date|Connection)\z}i
57 when %r{\AContent-Length\z}i
58 append_header(buf, key, value)
60 when %r{\ATransfer-Encoding\z}i
61 append_header(buf, key, value)
62 term = true if /\bchunked\b/i === value # value may be Array :x
64 # This should only be hit under Rack >= 1.5, as this was an illegal
68 append_header(buf, key, value)
71 if !hijack && !term && req.chunkable_response?
73 buf << "Transfer-Encoding: chunked\r\n".freeze
75 socket.write(buf << "\r\n".freeze)
84 socket.write("#{b.bytesize.to_s(16)}\r\n", b, "\r\n".freeze)
87 socket.write("0\r\n\r\n".freeze)
90 body.each { |chunk| socket.write(chunk) }