1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
4 # Writes a Rack response to your client using the HTTP/1.1 specification.
5 # You use it by simply doing:
7 # status, headers, body = rack_app.call(env)
8 # http_response_write(socket, status, headers, body)
10 # Most header correctness (including Content-Length and Content-Type)
11 # is the job of Rack, with the exception of the "Date" and "Status" header.
12 module Unicorn::HttpResponse
14 STATUS_CODES = defined?(Rack::Utils::HTTP_STATUS_CODES) ?
15 Rack::Utils::HTTP_STATUS_CODES : {}
16 STATUS_WITH_NO_ENTITY_BODY = defined?(
17 Rack::Utils::STATUS_WITH_NO_ENTITY_BODY) ?
18 Rack::Utils::STATUS_WITH_NO_ENTITY_BODY : begin
19 warn 'Rack::Utils::STATUS_WITH_NO_ENTITY_BODY missing'
23 # internal API, code will always be common-enough-for-even-old-Rack
24 def err_response(code, response_start_sent)
25 "#{response_start_sent ? '' : 'HTTP/1.1 '}" \
26 "#{code} #{STATUS_CODES[code]}\r\n\r\n"
29 def append_header(buf, key, value)
32 value.each { |v| buf << "#{key}: #{v}\r\n" }
34 # avoiding blank, key-only cookies with /\n+/
35 value.split(/\n+/).each { |v| buf << "#{key}: #{v}\r\n" }
37 buf << "#{key}: #{value}\r\n"
41 # writes the rack_response to socket as an HTTP response
42 def http_response_write(socket, status, headers, body,
43 req = Unicorn::HttpRequest.new)
48 msg = STATUS_CODES[code]
49 start = req.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
50 term = STATUS_WITH_NO_ENTITY_BODY.include?(code) || false
51 buf = "#{start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
52 "Date: #{httpdate}\r\n" \
53 "Connection: close\r\n"
54 headers.each do |key, value|
56 when %r{\A(?:Date|Connection)\z}i
58 when %r{\AContent-Length\z}i
59 append_header(buf, key, value)
61 when %r{\ATransfer-Encoding\z}i
62 append_header(buf, key, value)
63 term = true if /\bchunked\b/i === value # value may be Array :x
65 # This should only be hit under Rack >= 1.5, as this was an illegal
69 append_header(buf, key, value)
72 if !hijack && !term && req.chunkable_response?
74 buf << "Transfer-Encoding: chunked\r\n".freeze
76 socket.write(buf << "\r\n".freeze)
85 socket.write("#{b.bytesize.to_s(16)}\r\n", b, "\r\n".freeze)
88 socket.write("0\r\n\r\n".freeze)
91 body.each { |chunk| socket.write(chunk) }