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