process_client: fix pipeline_ready arity
[rainbows.git] / lib / rainbows / process_client.rb
blob3f23825f9a6f0a908f61e47fce49d6624905c666
1 # -*- encoding: binary -*-
2 # :enddoc:
3 module Rainbows::ProcessClient
4   include Rainbows::Response
5   include Rainbows::Const
7   NULL_IO = Unicorn::HttpRequest::NULL_IO
8   RACK_INPUT = Unicorn::HttpRequest::RACK_INPUT
9   IC = Unicorn::HttpRequest.input_class
10   HBUFSIZ = Rainbows.client_header_buffer_size
12   def process_loop
13     @hp = hp = Rainbows::HttpParser.new
14     kgio_read!(HBUFSIZ, buf = hp.buf) or return
16     begin # loop
17       until env = hp.parse
18         timed_read(buf2 ||= "") or return
19         buf << buf2
20       end
22       set_input(env, hp)
23       env[REMOTE_ADDR] = kgio_addr
24       status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
26       if 100 == status.to_i
27         write(EXPECT_100_RESPONSE)
28         env.delete(HTTP_EXPECT)
29         status, headers, body = APP.call(env)
30       end
31       write_response(status, headers, body, alive = @hp.next?)
32     end while alive
33   # if we get any error, try to write something back to the client
34   # assuming we haven't closed the socket, but don't get hung up
35   # if the socket is already closed or broken.  We'll always ensure
36   # the socket is closed at the end of this function
37   rescue => e
38     handle_error(e)
39   ensure
40     close unless closed?
41   end
43   def handle_error(e)
44     Rainbows::Error.write(self, e)
45   end
47   def set_input(env, hp)
48     env[RACK_INPUT] = 0 == hp.content_length ? NULL_IO : IC.new(self, hp)
49   end
51   def process_pipeline(env, hp)
52     begin
53       set_input(env, hp)
54       env[REMOTE_ADDR] = kgio_addr
55       status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
56       if 100 == status.to_i
57         write(EXPECT_100_RESPONSE)
58         env.delete(HTTP_EXPECT)
59         status, headers, body = APP.call(env)
60       end
61       write_response(status, headers, body, alive = hp.next?)
62     end while alive && pipeline_ready(hp)
63     alive or close
64     rescue => e
65       handle_error(e)
66   end
68   # override this in subclass/module
69   def pipeline_ready(hp)
70   end
71 end