1 # -*- encoding: binary -*-
3 module Rainbows::ProcessClient
4 include Rainbows::Response
5 include Rainbows::Const
7 NULL_IO = Unicorn::HttpRequest::NULL_IO
8 IC = Unicorn::HttpRequest.input_class
9 Rainbows.config!(self, :client_header_buffer_size, :keepalive_timeout)
12 Rainbows.now + KEEPALIVE_TIMEOUT
15 # used for reading headers (respecting keepalive_timeout)
19 case rv = kgio_tryread(CLIENT_HEADER_BUFFER_SIZE, buf)
21 return if expire && expire < Rainbows.now
22 expire ||= read_expire
23 kgio_wait_readable(KEEPALIVE_TIMEOUT)
31 @hp = hp = Rainbows::HttpParser.new
32 kgio_read!(CLIENT_HEADER_BUFFER_SIZE, buf = hp.buf) or return
36 timed_read(buf2 ||= "") or return
41 env['REMOTE_ADDR'] = kgio_addr
42 hp.hijack_setup(to_io)
43 status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
46 write("HTTP/1.1 100 Continue\r\n\r\n".freeze)
47 env.delete('HTTP_EXPECT'.freeze)
48 status, headers, body = APP.call(env)
50 return if hp.hijacked?
51 write_response(status, headers, body, alive = hp.next?) or return
53 # if we get any error, try to write something back to the client
54 # assuming we haven't closed the socket, but don't get hung up
55 # if the socket is already closed or broken. We'll always ensure
56 # the socket is closed at the end of this function
60 close unless closed? || hp.hijacked?
64 Rainbows::Error.write(self, e)
67 def set_input(env, hp)
68 env['rack.input'] = 0 == hp.content_length ? NULL_IO : IC.new(self, hp)
71 def process_pipeline(env, hp)
74 env['REMOTE_ADDR'] = kgio_addr
75 hp.hijack_setup(to_io)
76 status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
78 write("HTTP/1.1 100 Continue\r\n\r\n".freeze)
79 env.delete('HTTP_EXPECT'.freeze)
80 status, headers, body = APP.call(env)
82 return if hp.hijacked?
83 write_response(status, headers, body, alive = hp.next?) or return
84 end while alive && pipeline_ready(hp)
90 # override this in subclass/module
91 def pipeline_ready(hp)