port test/unit/test_ccc.rb to Perl 5
[unicorn.git] / lib / unicorn / http_response.rb
blob36341654faf8104f4749b2fab3bc6cc9527983a7
1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
3 # :enddoc:
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'
20     {}
21   end
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"
27   end
29   def append_header(buf, key, value)
30     case value
31     when Array # Rack 3
32       value.each { |v| buf << "#{key}: #{v}\r\n" }
33     when /\n/ # Rack 2
34       # avoiding blank, key-only cookies with /\n+/
35       value.split(/\n+/).each { |v| buf << "#{key}: #{v}\r\n" }
36     else
37       buf << "#{key}: #{value}\r\n"
38     end
39   end
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)
44     hijack = nil
45     do_chunk = false
46     if headers
47       code = status.to_i
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|
55         case key
56         when %r{\A(?:Date|Connection)\z}i
57           next
58         when %r{\AContent-Length\z}i
59           append_header(buf, key, value)
60           term = true
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
64         when "rack.hijack"
65           # This should only be hit under Rack >= 1.5, as this was an illegal
66           # key in Rack < 1.5
67           hijack = value
68         else
69           append_header(buf, key, value)
70         end
71       end
72       if !hijack && !term && req.chunkable_response?
73         do_chunk = true
74         buf << "Transfer-Encoding: chunked\r\n".freeze
75       end
76       socket.write(buf << "\r\n".freeze)
77     end
79     if hijack
80       req.hijacked!
81       hijack.call(socket)
82     elsif do_chunk
83       begin
84         body.each do |b|
85           socket.write("#{b.bytesize.to_s(16)}\r\n", b, "\r\n".freeze)
86         end
87       ensure
88         socket.write("0\r\n\r\n".freeze)
89       end
90     else
91       body.each { |chunk| socket.write(chunk) }
92     end
93   end
94 end