prefer Hash#merge! to Hash#update for consistency
[rainbows.git] / lib / rainbows / event_machine / client.rb
blob3312f5d3aeacbd5d92ccf1accf4c29b8f0244fb6
1 # -*- encoding: binary -*-
2 # :enddoc:
3 class Rainbows::EventMachine::Client < EM::Connection
4   attr_writer :body
5   include Rainbows::EvCore
7   def initialize(io)
8     @_io = io
9     @body = nil
10   end
12   alias write send_data
14   def receive_data(data)
15     # To avoid clobbering the current streaming response
16     # (often a static file), we do not attempt to process another
17     # request on the same connection until the first is complete
18     if @body
19       if data
20         @buf << data
21         @_io.shutdown(Socket::SHUT_RD) if @buf.size > 0x1c000
22       end
23       EM.next_tick { receive_data(nil) } unless @buf.empty?
24     else
25       on_read(data || "") if (@buf.size > 0) || data
26     end
27   end
29   def quit
30     super
31     close_connection_after_writing
32   end
34   def app_call
35     set_comm_inactivity_timeout 0
36     @env[RACK_INPUT] = @input
37     @env[REMOTE_ADDR] = @_io.kgio_addr
38     @env[ASYNC_CALLBACK] = method(:em_write_response)
39     @env[ASYNC_CLOSE] = EM::DefaultDeferrable.new
41     response = catch(:async) { APP.call(@env.merge!(RACK_DEFAULTS)) }
43     # too tricky to support pipelining with :async since the
44     # second (pipelined) request could be a stuck behind a
45     # long-running async response
46     (response.nil? || -1 == response[0]) and return @state = :close
48     if @hp.next?
49       @state = :headers
50       em_write_response(response, true)
51       if @buf.empty?
52         set_comm_inactivity_timeout(G.kato)
53       elsif @body.nil?
54         EM.next_tick { receive_data(nil) }
55       end
56     else
57       em_write_response(response, false)
58     end
59   end
61   # don't change this method signature, "async.callback" relies on it
62   def em_write_response(response, alive = false)
63     status, headers, body = response
65     if body.respond_to?(:errback) && body.respond_to?(:callback)
66       @body = body
67       body.callback { quit }
68       body.errback { quit }
69       alive = true
70     elsif body.respond_to?(:to_path)
71       st = File.stat(path = body.to_path)
73       if st.file?
74         write_headers(status, headers, alive)
75         @body = stream_file_data(path)
76         @body.errback do
77           body.close if body.respond_to?(:close)
78           quit
79         end
80         @body.callback do
81           body.close if body.respond_to?(:close)
82           @body = nil
83           alive ? receive_data(nil) : quit
84         end
85         return
86       elsif st.socket? || st.pipe?
87         io = body_to_io(@body = body)
88         chunk = stream_response_headers(status, headers, alive)
89         m = chunk ? Rainbows::EventMachine::ResponseChunkPipe :
90                     Rainbows::EventMachine::ResponsePipe
91         return EM.watch(io, m, self).notify_readable = true
92       end
93       # char or block device... WTF? fall through to body.each
94     end
95     write_response(status, headers, body, alive)
96     quit unless alive
97   end
99   def next!
100     @body.close if @body.respond_to?(:close)
101     @hp.keepalive? ? receive_data(@body = nil) : quit
102   end
104   def unbind
105     async_close = @env[ASYNC_CLOSE] and async_close.succeed
106     @body.respond_to?(:fail) and @body.fail
107     begin
108       @_io.close
109     rescue Errno::EBADF
110       # EventMachine's EventableDescriptor::Close() may close
111       # the underlying file descriptor without invalidating the
112       # associated IO object on errors, so @_io.closed? isn't
113       # sufficient.
114     end
115   end