unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 3d7cd5054b43d4d58b65c528218e93ae4aa277bf 2300 bytes (raw)
$ git show v0.10.3r:lib/unicorn/http_response.rb	# shows this blob on the CLI

 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
 
require 'time'

module Unicorn
  # 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)
  #   HttpResponse.write(socket, [ status, headers, body ], keepalive)
  #
  # Most header correctness (including Content-Length and Content-Type)
  # is the job of Rack, with the exception of the "Connection"
  # and "Date" headers.
  class 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
    }

    CONN_CLOSE = "Connection: close\r\n"
    CONN_ALIVE = "Connection: keep-alive\r\n"

    # Rack does not set/require a Date: header.  We always override the
    # Connection: and Date: headers no matter what (if anything) our
    # Rack application sent us.
    SKIP = { 'connection' => true, 'date' => true, 'status' => true }.freeze

    # writes the rack_response to socket as an HTTP response
    def self.write(socket, rack_response, keepalive = false)
      status, headers, body = rack_response
      status = CODES[status.to_i] || status
      tmp = [ keepalive ? CONN_ALIVE : CONN_CLOSE ]

      # Don't bother enforcing duplicate supression, it's a Hash most of
      # the time anyways so just hope our app knows what it's doing
      headers.each do |key, value|
        next if SKIP.include?(key.downcase)
        if value =~ /\n/
          value.split(/\n/).each { |v| tmp << "#{key}: #{v}\r\n" }
        else
          tmp << "#{key}: #{value}\r\n"
        end
      end

      # Rack should enforce Content-Length or chunked transfer encoding,
      # so don't worry or care about them.
      # Date is required by HTTP/1.1 as long as our clock can be trusted.
      # Some broken clients require a "Status" header so we accomodate them
      socket.write("HTTP/1.1 #{status}\r\n" \
                   "Date: #{Time.now.httpdate}\r\n" \
                   "Status: #{status}\r\n" \
                   "#{tmp.join(Z)}\r\n")
      body.each { |chunk| socket.write(chunk) }
      keepalive or socket.close # flushes and uncorks the socket immediately
      ensure
        body.respond_to?(:close) and body.close rescue nil
    end

  end
end

git clone https://yhbt.net/unicorn.git