const: remove unused constants
[unicorn.git] / lib / unicorn / http_request.rb
blob1358cccbb5eacd563056fb85e8532acd0362e86f
1 # coding:binary
2 require 'stringio'
3 require 'unicorn_http'
5 module Unicorn
6   class HttpRequest
8     # default parameters we merge into the request env for Rack handlers
9     DEFAULTS = {
10       "rack.errors" => $stderr,
11       "rack.multiprocess" => true,
12       "rack.multithread" => false,
13       "rack.run_once" => false,
14       "rack.version" => [1, 0].freeze,
15       "SCRIPT_NAME" => "".freeze,
17       # this is not in the Rack spec, but some apps may rely on it
18       "SERVER_SOFTWARE" => "Unicorn #{Const::UNICORN_VERSION}".freeze
19     }
21     NULL_IO = StringIO.new(Z)
22     LOCALHOST = '127.0.0.1'.freeze
24     # Being explicitly single-threaded, we have certain advantages in
25     # not having to worry about variables being clobbered :)
26     BUF = ' ' * Const::CHUNK_SIZE # initial size, may grow
27     PARSER = HttpParser.new
28     REQ = {}
30     # Does the majority of the IO processing.  It has been written in
31     # Ruby using about 8 different IO processing strategies.
32     #
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)
36     #
37     # Anyone who thinks they can make it faster is more than welcome to
38     # take a crack at it.
39     #
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).
43     def read(socket)
44       REQ.clear
45       PARSER.reset
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.last : LOCALHOST
57       # short circuit the common case with small GET requests first
58       PARSER.headers(REQ, socket.readpartial(Const::CHUNK_SIZE, BUF)) and
59           return handle_body(socket)
61       data = BUF.dup # socket.readpartial will clobber data
63       # Parser is not done, queue up more data to read and continue parsing
64       # an Exception thrown from the PARSER will throw us out of the loop
65       begin
66         BUF << socket.readpartial(Const::CHUNK_SIZE, data)
67         PARSER.headers(REQ, BUF) and return handle_body(socket)
68       end while true
69     end
71     private
73     # Handles dealing with the rest of the request
74     # returns a # Rack environment if successful
75     def handle_body(socket)
76       REQ[Const::RACK_INPUT] = 0 == PARSER.content_length ?
77                    NULL_IO : Unicorn::TeeInput.new(socket, REQ, PARSER, BUF)
78       REQ.update(DEFAULTS)
79     end
81   end
82 end