1 # -*- encoding: binary -*-
6 # TODO: remove redundant names
7 Unicorn.const_set(:HttpRequest, Unicorn::HttpParser)
8 class Unicorn::HttpParser
10 # default parameters we merge into the request env for Rack handlers
12 "rack.errors" => $stderr,
13 "rack.multiprocess" => true,
14 "rack.multithread" => false,
15 "rack.run_once" => false,
16 "rack.version" => [1, 1],
19 # this is not in the Rack spec, but some apps may rely on it
20 "SERVER_SOFTWARE" => "Unicorn #{Unicorn::Const::UNICORN_VERSION}"
23 NULL_IO = StringIO.new("")
25 attr_accessor :response_start_sent
28 # A frozen format for this is about 15% faster
29 REMOTE_ADDR = 'REMOTE_ADDR'.freeze
30 RACK_INPUT = 'rack.input'.freeze
31 @@input_class = Unicorn::TeeInput
32 @@check_client_connection = false
38 def self.input_class=(klass)
42 def self.check_client_connection
43 @@check_client_connection
46 def self.check_client_connection=(bool)
47 @@check_client_connection = bool
52 # Does the majority of the IO processing. It has been written in
53 # Ruby using about 8 different IO processing strategies.
55 # It is currently carefully constructed to make sure that it gets
56 # the best possible performance for the common case: GET requests
57 # that are fully complete after a single read(2)
59 # Anyone who thinks they can make it faster is more than welcome to
62 # returns an environment hash suitable for Rack if successful
63 # This does minimal exception trapping and it is up to the caller
64 # to handle any socket errors (e.g. user aborted upload).
69 # From http://www.ietf.org/rfc/rfc3875:
70 # "Script authors should be aware that the REMOTE_ADDR and
71 # REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
72 # may not identify the ultimate source of the request. They
73 # identify the client for the immediate request to the server;
74 # that client may be a proxy, gateway, or other intermediary
75 # acting on behalf of the actual source client."
76 e[REMOTE_ADDR] = socket.kgio_addr
78 # short circuit the common case with small GET requests first
79 socket.kgio_read!(16384, buf)
81 # Parser is not done, queue up more data to read and continue parsing
82 # an Exception thrown from the parser will throw us out of the loop
83 false until add_parse(socket.kgio_read!(16384))
86 # detect if the socket is valid by writing a partial response:
87 if @@check_client_connection && headers?
88 @response_start_sent = true
89 Unicorn::Const::HTTP_RESPONSE_START.each { |c| socket.write(c) }
92 e[RACK_INPUT] = 0 == content_length ?
93 NULL_IO : @@input_class.new(socket, self)
94 hijack_setup(e, socket)
98 # Rack 1.5.0 (protocol version 1.2) adds hijack request support
99 if ((Rack::VERSION[0] << 8) | Rack::VERSION[1]) >= 0x0102
100 DEFAULTS["rack.hijack?"] = true
101 DEFAULTS["rack.version"] = [1, 2]
103 RACK_HIJACK = "rack.hijack".freeze
104 RACK_HIJACK_IO = "rack.hijack_io".freeze
107 env.include?(RACK_HIJACK_IO)
110 def hijack_setup(e, socket)
111 e[RACK_HIJACK] = proc { e[RACK_HIJACK_IO] ||= socket }
114 # old Rack, do nothing.
115 def hijack_setup(e, _)