globally refactor Range handling for responses
[rainbows.git] / lib / rainbows / process_client.rb
blobd8407784b05dfe4770d871815419c0932e55f60d
1 # -*- encoding: binary -*-
2 module Rainbows::ProcessClient
3   include Rainbows::Response
4   include Rainbows::RackInput
5   include Rainbows::Const
7   def process_loop
8     @hp = hp = Rainbows::HttpParser.new
9     kgio_read!(16384, buf = hp.buf) or return
11     begin # loop
12       until env = hp.parse
13         timed_read(buf2 ||= "") or return
14         buf << buf2
15       end
17       set_input(env, hp)
18       env[REMOTE_ADDR] = kgio_addr
19       status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
21       if 100 == status.to_i
22         write(EXPECT_100_RESPONSE)
23         env.delete(HTTP_EXPECT)
24         status, headers, body = APP.call(env)
25       end
26       write_response(status, headers, body, alive = @hp.next?)
27     end while alive
28   # if we get any error, try to write something back to the client
29   # assuming we haven't closed the socket, but don't get hung up
30   # if the socket is already closed or broken.  We'll always ensure
31   # the socket is closed at the end of this function
32   rescue => e
33     handle_error(e)
34   ensure
35     close unless closed?
36   end
38   def handle_error(e)
39     Rainbows::Error.write(self, e)
40   end
41 end