1 # -*- encoding: binary -*-
5 # TODO: remove redundant names
6 Unicorn.const_set(:HttpRequest, Unicorn::HttpParser)
7 class Unicorn::HttpParser
9 # default parameters we merge into the request env for Rack handlers
11 "rack.errors" => $stderr,
12 "rack.multiprocess" => true,
13 "rack.multithread" => false,
14 "rack.run_once" => false,
15 "rack.version" => [1, 1],
18 # this is not in the Rack spec, but some apps may rely on it
19 "SERVER_SOFTWARE" => "Unicorn #{Unicorn::Const::UNICORN_VERSION}"
22 NULL_IO = StringIO.new("")
25 # A frozen format for this is about 15% faster
26 REMOTE_ADDR = 'REMOTE_ADDR'.freeze
27 RACK_INPUT = 'rack.input'.freeze
28 @@input_class = Unicorn::TeeInput
34 def self.input_class=(klass)
39 # Does the majority of the IO processing. It has been written in
40 # Ruby using about 8 different IO processing strategies.
42 # It is currently carefully constructed to make sure that it gets
43 # the best possible performance for the common case: GET requests
44 # that are fully complete after a single read(2)
46 # Anyone who thinks they can make it faster is more than welcome to
49 # returns an environment hash suitable for Rack if successful
50 # This does minimal exception trapping and it is up to the caller
51 # to handle any socket errors (e.g. user aborted upload).
56 # From http://www.ietf.org/rfc/rfc3875:
57 # "Script authors should be aware that the REMOTE_ADDR and
58 # REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
59 # may not identify the ultimate source of the request. They
60 # identify the client for the immediate request to the server;
61 # that client may be a proxy, gateway, or other intermediary
62 # acting on behalf of the actual source client."
63 e[REMOTE_ADDR] = socket.kgio_addr
65 # short circuit the common case with small GET requests first
66 socket.kgio_read!(16384, buf)
68 # Parser is not done, queue up more data to read and continue parsing
69 # an Exception thrown from the parser will throw us out of the loop
71 buf << socket.kgio_read!(16384)
74 e[RACK_INPUT] = 0 == content_length ?
75 NULL_IO : @@input_class.new(socket, self)