util: allow relative paths to be rotated
[unicorn.git] / lib / unicorn / http_request.rb
blob2c52631bb2823696320c3c4765b437901de88330
1 # -*- encoding: binary -*-
3 require 'unicorn_http'
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
10   DEFAULTS = {
11     "rack.errors" => $stderr,
12     "rack.multiprocess" => true,
13     "rack.multithread" => false,
14     "rack.run_once" => false,
15     "rack.version" => [1, 1],
16     "SCRIPT_NAME" => "",
18     # this is not in the Rack spec, but some apps may rely on it
19     "SERVER_SOFTWARE" => "Unicorn #{Unicorn::Const::UNICORN_VERSION}"
20   }
22   NULL_IO = StringIO.new("")
24   # :stopdoc:
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
30   def self.input_class
31     @@input_class
32   end
34   def self.input_class=(klass)
35     @@input_class = klass
36   end
37   # :startdoc:
39   # Does the majority of the IO processing.  It has been written in
40   # Ruby using about 8 different IO processing strategies.
41   #
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)
45   #
46   # Anyone who thinks they can make it faster is more than welcome to
47   # take a crack at it.
48   #
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).
52   def read(socket)
53     clear
54     e = env
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)
67     if parse.nil?
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
70       begin
71         buf << socket.kgio_read!(16384)
72       end while parse.nil?
73     end
74     e[RACK_INPUT] = 0 == content_length ?
75                     NULL_IO : @@input_class.new(socket, self)
76     e.merge!(DEFAULTS)
77   end
78 end