http_request: drop conditional assignment for hijack
[unicorn.git] / lib / unicorn / http_request.rb
blob6b204319cf33a36ab76493914ee5d203a6f461a6
1 # -*- encoding: binary -*-
2 # :enddoc:
3 # no stable API here
4 require 'unicorn_http'
6 # TODO: remove redundant names
7 Unicorn.const_set(:HttpRequest, Unicorn::HttpParser)
8 class Unicorn::HttpParser
10   # default parameters we merge into the request env for Rack handlers
11   DEFAULTS = {
12     "rack.errors" => $stderr,
13     "rack.multiprocess" => true,
14     "rack.multithread" => false,
15     "rack.run_once" => false,
16     "rack.version" => [1, 1],
17     "SCRIPT_NAME" => "",
19     # this is not in the Rack spec, but some apps may rely on it
20     "SERVER_SOFTWARE" => "Unicorn #{Unicorn::Const::UNICORN_VERSION}"
21   }
23   NULL_IO = StringIO.new("")
25   attr_accessor :response_start_sent
27   # :stopdoc:
28   # A frozen format for this is about 15% faster
29   REMOTE_ADDR = 'REMOTE_ADDR'.freeze
30   RACK_INPUT = 'rack.input'.freeze
31   @@input_class = Unicorn::TeeInput
32   @@check_client_connection = false
34   def self.input_class
35     @@input_class
36   end
38   def self.input_class=(klass)
39     @@input_class = klass
40   end
42   def self.check_client_connection
43     @@check_client_connection
44   end
46   def self.check_client_connection=(bool)
47     @@check_client_connection = bool
48   end
50   # :startdoc:
52   # Does the majority of the IO processing.  It has been written in
53   # Ruby using about 8 different IO processing strategies.
54   #
55   # It is currently carefully constructed to make sure that it gets
56   # the best possible performance for the common case: GET requests
57   # that are fully complete after a single read(2)
58   #
59   # Anyone who thinks they can make it faster is more than welcome to
60   # take a crack at it.
61   #
62   # returns an environment hash suitable for Rack if successful
63   # This does minimal exception trapping and it is up to the caller
64   # to handle any socket errors (e.g. user aborted upload).
65   def read(socket)
66     clear
67     e = env
69     # From http://www.ietf.org/rfc/rfc3875:
70     # "Script authors should be aware that the REMOTE_ADDR and
71     #  REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
72     #  may not identify the ultimate source of the request.  They
73     #  identify the client for the immediate request to the server;
74     #  that client may be a proxy, gateway, or other intermediary
75     #  acting on behalf of the actual source client."
76     e[REMOTE_ADDR] = socket.kgio_addr
78     # short circuit the common case with small GET requests first
79     socket.kgio_read!(16384, buf)
80     if parse.nil?
81       # Parser is not done, queue up more data to read and continue parsing
82       # an Exception thrown from the parser will throw us out of the loop
83       false until add_parse(socket.kgio_read!(16384))
84     end
86     # detect if the socket is valid by writing a partial response:
87     if @@check_client_connection && headers?
88       @response_start_sent = true
89       Unicorn::Const::HTTP_RESPONSE_START.each { |c| socket.write(c) }
90     end
92     e[RACK_INPUT] = 0 == content_length ?
93                     NULL_IO : @@input_class.new(socket, self)
94     hijack_setup(e, socket)
95     e.merge!(DEFAULTS)
96   end
98   # Rack 1.5.0 (protocol version 1.2) adds hijack request support
99   if ((Rack::VERSION[0] << 8) | Rack::VERSION[1]) >= 0x0102
100     DEFAULTS["rack.hijack?"] = true
101     DEFAULTS["rack.version"] = [1, 2]
103     RACK_HIJACK = "rack.hijack".freeze
104     RACK_HIJACK_IO = "rack.hijack_io".freeze
106     def hijacked?
107       env.include?(RACK_HIJACK_IO)
108     end
110     def hijack_setup(e, socket)
111       e[RACK_HIJACK] = proc { e[RACK_HIJACK_IO] = socket }
112     end
113   else
114     # old Rack, do nothing.
115     def hijack_setup(e, _)
116     end
118     def hijacked?
119       false
120     end
121   end