unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
blob 342dd0b9cb67b0f99f67dcbf654d317f23bc2b5d 2725 bytes (raw)
name: lib/unicorn/http_response.rb 	 # note: path name is non-authoritative(*)

 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
# -*- 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

  STATUS_CODES = defined?(Rack::Utils::HTTP_STATUS_CODES) ?
                 Rack::Utils::HTTP_STATUS_CODES : {}

  # internal API, code will always be common-enough-for-even-old-Rack
  def err_response(code, response_start_sent)
    "#{response_start_sent ? '' : 'HTTP/1.1 '}" \
      "#{code} #{STATUS_CODES[code]}\r\n\r\n"
  end

  def append_header(buf, key, value)
    case value
    when Array # Rack 3
      value.each { |v| buf << "#{key}: #{v}\r\n" }
    when /\n/ # Rack 2
      # avoiding blank, key-only cookies with /\n+/
      value.split(/\n+/).each { |v| buf << "#{key}: #{v}\r\n" }
    else
      buf << "#{key}: #{value}\r\n"
    end
  end

  # writes the rack_response to socket as an HTTP response
  def http_response_write(socket, status, headers, body,
                          req = Unicorn::HttpRequest.new)
    hijack = nil
    do_chunk = false
    if headers
      code = status.to_i
      msg = STATUS_CODES[code]
      start = req.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
      term = false
      buf = "#{start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
            "Date: #{httpdate}\r\n" \
            "Connection: close\r\n"
      headers.each do |key, value|
        case key
        when %r{\A(?:Date|Connection)\z}i
          next
        when %r{\AContent-Length\z}i
          append_header(buf, key, value)
          term = true
        when %r{\ATransfer-Encoding\z}i
          append_header(buf, key, value)
          term = true if /\bchunked\b/i === value # value may be Array :x
        when "rack.hijack"
          # This should only be hit under Rack >= 1.5, as this was an illegal
          # key in Rack < 1.5
          hijack = value
        else
          append_header(buf, key, value)
        end
      end
      if !hijack && !term && req.chunkable_response?
        do_chunk = true
        buf << "Transfer-Encoding: chunked\r\n".freeze
      end
      socket.write(buf << "\r\n".freeze)
    end

    if hijack
      req.hijacked!
      hijack.call(socket)
    elsif do_chunk
      begin
        body.each do |b|
          socket.write("#{b.bytesize.to_s(16)}\r\n", b, "\r\n".freeze)
        end
      ensure
        socket.write("0\r\n\r\n".freeze)
      end
    else
      body.each { |chunk| socket.write(chunk) }
    end
  end
end

debug log:

solving 342dd0b ...
found 342dd0b in https://yhbt.net/unicorn-public/20230602000027.M794217@dcvr/
found 19469b4 in https://yhbt.net/unicorn.git/
preparing index
index prepared:
100644 19469b47963fd8d0a170a7be4bde93fab4519011	lib/unicorn/http_response.rb

applying [1/1] https://yhbt.net/unicorn-public/20230602000027.M794217@dcvr/
diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb
index 19469b4..342dd0b 100644

Checking patch lib/unicorn/http_response.rb...
Applied patch lib/unicorn/http_response.rb cleanly.

index at:
100644 342dd0b9cb67b0f99f67dcbf654d317f23bc2b5d	lib/unicorn/http_response.rb

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).