1 # -*- encoding: binary -*-
3 require 'rainbows/rack_input'
4 module Rainbows::ProcessClient
6 include Rainbows::Response
7 HttpParser = Unicorn::HttpParser
8 include Rainbows::RackInput
9 include Rainbows::Const
11 # once a client is accepted, it is processed in its entirety here
12 # in 3 easy steps: read request, call app, write app response
13 # this is used by synchronous concurrency models
14 # Base, ThreadSpawn, ThreadPool
15 def process_client(client) # :nodoc:
17 client.kgio_read!(16384, buf = hp.buf)
18 remote_addr = client.kgio_addr
23 client.timed_read(buf2 ||= "") or return
27 set_input(env, hp, client)
28 env[REMOTE_ADDR] = remote_addr
29 status, headers, body = APP.call(env.update(RACK_DEFAULTS))
32 client.write(EXPECT_100_RESPONSE)
33 env.delete(HTTP_EXPECT)
34 status, headers, body = APP.call(env)
38 headers = HH.new(headers)
39 range = make_range!(env, status, headers) and status = range.shift
40 alive = hp.next? && G.alive
41 headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE
42 client.write(response_header(status, headers))
44 write_body(client, body, range)
46 # if we get any error, try to write something back to the client
47 # assuming we haven't closed the socket, but don't get hung up
48 # if the socket is already closed or broken. We'll always ensure
49 # the socket is closed at the end of this function
51 Rainbows::Error.write(client, e)
53 client.close unless client.closed?