7 # default parameters we merge into the request env for Rack handlers
9 "rack.errors" => $stderr,
10 "rack.multiprocess" => true,
11 "rack.multithread" => false,
12 "rack.run_once" => false,
13 "rack.version" => [1, 0].freeze,
14 "SCRIPT_NAME" => "".freeze,
16 # this is not in the Rack spec, but some apps may rely on it
17 "SERVER_SOFTWARE" => "Unicorn #{Const::UNICORN_VERSION}".freeze
20 LOCALHOST = '127.0.0.1'.freeze
22 # Being explicitly single-threaded, we have certain advantages in
23 # not having to worry about variables being clobbered :)
24 BUF = ' ' * Const::CHUNK_SIZE # initial size, may grow
25 PARSER = HttpParser.new
28 # Does the majority of the IO processing. It has been written in
29 # Ruby using about 8 different IO processing strategies.
31 # It is currently carefully constructed to make sure that it gets
32 # the best possible performance for the common case: GET requests
33 # that are fully complete after a single read(2)
35 # Anyone who thinks they can make it faster is more than welcome to
38 # returns an environment hash suitable for Rack if successful
39 # This does minimal exception trapping and it is up to the caller
40 # to handle any socket errors (e.g. user aborted upload).
45 # From http://www.ietf.org/rfc/rfc3875:
46 # "Script authors should be aware that the REMOTE_ADDR and
47 # REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
48 # may not identify the ultimate source of the request. They
49 # identify the client for the immediate request to the server;
50 # that client may be a proxy, gateway, or other intermediary
51 # acting on behalf of the actual source client."
52 REQ[Const::REMOTE_ADDR] =
53 TCPSocket === socket ? socket.peeraddr.last : LOCALHOST
55 # short circuit the common case with small GET requests first
56 PARSER.headers(REQ, socket.readpartial(Const::CHUNK_SIZE, BUF)) and
57 return handle_body(socket)
59 data = BUF.dup # socket.readpartial will clobber data
61 # Parser is not done, queue up more data to read and continue parsing
62 # an Exception thrown from the PARSER will throw us out of the loop
64 BUF << socket.readpartial(Const::CHUNK_SIZE, data)
65 PARSER.headers(REQ, BUF) and return handle_body(socket)
71 # Handles dealing with the rest of the request
72 # returns a # Rack environment if successful
73 def handle_body(socket)
74 REQ[Const::RACK_INPUT] = Unicorn::TeeInput.new(socket)