summary refs log tree commit homepage
path: root/lib/unicorn/http_response.rb
blob: cc027c52f25943a83ae6cc9cd078601fc678a916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- encoding: binary -*-
# :enddoc:
# 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.
module Unicorn::HttpResponse

  # Every standard HTTP code mapped to the appropriate message.
  CODES = Rack::Utils::HTTP_STATUS_CODES.inject({}) { |hash,(code,msg)|
    hash[code] = "#{code} #{msg}"
    hash
  }
  CRLF = "\r\n"

  def err_response(code, response_start_sent)
    "#{response_start_sent ? '' : 'HTTP/1.1 '}#{CODES[code]}\r\n\r\n"
  end

  # writes the rack_response to socket as an HTTP response
  def http_response_write(socket, status, headers, body,
                          response_start_sent=false)
    hijack = nil

    http_response_start = response_start_sent ? '' : 'HTTP/1.1 '
    if headers
      buf = "#{http_response_start}#{CODES[status.to_i] || status}\r\n" \
            "Date: #{httpdate}\r\n" \
            "Connection: close\r\n"
      headers.each do |key, value|
        case key
        when %r{\A(?:Date\z|Connection\z)}i
          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

  # Rack 1.5.0 (protocol version 1.2) adds response hijacking support
  if ((Rack::VERSION[0] << 8) | Rack::VERSION[1]) >= 0x0102
    def hijack_prepare(value)
      value
    end
  else
    def hijack_prepare(_)
    end
  end
end