fc0dfe360663735df01841f6f646e2814ee1fa4f
[rainbows.git] / lib / rainbows / event_machine / client.rb
blobfc0dfe360663735df01841f6f646e2814ee1fa4f
1 # -*- encoding: binary -*-
2 # :enddoc:
3 class Rainbows::EventMachine::Client < EM::Connection
4   include Rainbows::EvCore
5   Rainbows.config!(self, :keepalive_timeout)
7   def initialize(io)
8     @_io = io
9     @deferred = 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 @deferred
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 || Z) 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 input
35     set_comm_inactivity_timeout 0
36     @env[RACK_INPUT] = input
37     @env[REMOTE_ADDR] = @_io.kgio_addr
38     @env[ASYNC_CALLBACK] = method(:write_async_response)
39     @env[ASYNC_CLOSE] = EM::DefaultDeferrable.new
40     status, headers, body = catch(:async) {
41       APP.call(@env.merge!(RACK_DEFAULTS))
42     }
44     (nil == status || -1 == status) ? @deferred = true :
45       ev_write_response(status, headers, body, @hp.next?)
46   end
48   def deferred_errback(orig_body)
49     @deferred.errback do
50       orig_body.close if orig_body.respond_to?(:close)
51       quit
52     end
53   end
55   def deferred_callback(orig_body, alive)
56     @deferred.callback do
57       orig_body.close if orig_body.respond_to?(:close)
58       @deferred = nil
59       alive ? receive_data(nil) : quit
60     end
61   end
63   def ev_write_response(status, headers, body, alive)
64     @state = :headers if alive
65     if body.respond_to?(:errback) && body.respond_to?(:callback)
66       @deferred = body
67       write_headers(status, headers, alive)
68       write_body_each(body)
69       deferred_errback(body)
70       deferred_callback(body, alive)
71       return
72     elsif body.respond_to?(:to_path)
73       st = File.stat(path = body.to_path)
75       if st.file?
76         write_headers(status, headers, alive)
77         @deferred = stream_file_data(path)
78         deferred_errback(body)
79         deferred_callback(body, alive)
80         return
81       elsif st.socket? || st.pipe?
82         io = body_to_io(@deferred = body)
83         chunk = stream_response_headers(status, headers, alive)
84         m = chunk ? Rainbows::EventMachine::ResponseChunkPipe :
85                     Rainbows::EventMachine::ResponsePipe
86         return EM.watch(io, m, self).notify_readable = true
87       end
88       # char or block device... WTF? fall through to body.each
89     end
90     write_response(status, headers, body, alive)
91     if alive
92       if @deferred.nil?
93         if @buf.empty?
94           set_comm_inactivity_timeout(KEEPALIVE_TIMEOUT)
95         else
96           EM.next_tick { receive_data(nil) }
97         end
98       end
99     else
100       quit unless @deferred
101     end
102   end
104   def next!
105     @deferred.close if @deferred.respond_to?(:close)
106     @hp.keepalive? ? receive_data(@deferred = nil) : quit
107   end
109   def unbind
110     async_close = @env[ASYNC_CLOSE] and async_close.succeed
111     @deferred.respond_to?(:fail) and @deferred.fail
112     begin
113       @_io.close
114     rescue Errno::EBADF
115       # EventMachine's EventableDescriptor::Close() may close
116       # the underlying file descriptor without invalidating the
117       # associated IO object on errors, so @_io.closed? isn't
118       # sufficient.
119     end
120   end