From 39b178cdebe275cbc8ce19cf269bea7cd15ff4ca Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 4 Jul 2010 22:16:52 +0000 Subject: refactor response body handling for sendfile(2) This hopefully allows the "sendfile" gem to be required anywhere in the Rainbows!/Unicorn config file, and not have to be required via RUBYOPT or the '-r' command-line switch. We also modularize HttpResponse and avoids singleton methods in the response path. This (hopefully) makes it easier for individual concurrency models to share code and override individual methods. --- lib/rainbows/event_machine.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'lib/rainbows/event_machine.rb') diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb index 6ba536b..0ad604e 100644 --- a/lib/rainbows/event_machine.rb +++ b/lib/rainbows/event_machine.rb @@ -50,6 +50,7 @@ module Rainbows class Client < EM::Connection include Rainbows::EvCore + include Rainbows::HttpResponse G = Rainbows::G def initialize(io) @@ -103,23 +104,23 @@ module Rainbows if body.respond_to?(:errback) && body.respond_to?(:callback) body.callback { quit } body.errback { quit } - HttpResponse.write(self, response, out) + write_header(self, response, out) + write_body_each(self, body) return elsif ! body.respond_to?(:to_path) - HttpResponse.write(self, response, out) + write_response(self, response, out) quit unless alive return end headers = Rack::Utils::HeaderHash.new(response[1]) - io = Rainbows.body_to_io(body) + io = body_to_io(body) st = io.stat if st.file? headers.delete('Transfer-Encoding') headers['Content-Length'] ||= st.size.to_s - response = [ response[0], headers, [] ] - HttpResponse.write(self, response, out) + write_header(self, [ response[0], headers ], out) stream = stream_file_data(body.to_path) stream.callback { quit } unless alive elsif st.socket? || st.pipe? @@ -130,15 +131,14 @@ module Rainbows else out[0] = CONN_CLOSE end - response = [ response[0], headers, [] ] - HttpResponse.write(self, response, out) + write_header(self, [ response[0], headers ], out) if do_chunk EM.watch(io, ResponseChunkPipe, self).notify_readable = true else EM.enable_proxy(EM.attach(io, ResponsePipe, self), self, 16384) end else - HttpResponse.write(self, response, out) + write_response(self, response, out) end end @@ -226,6 +226,11 @@ module Rainbows end end + def init_worker_process(worker) + Rainbows::HttpResponse.setup(Rainbows::EventMachine::Client) + super + end + # runs inside each forked worker, this sits around and waits # for connections and doesn't die until the parent dies (or is # given a INT, QUIT, or TERM signal) -- cgit v1.2.3-24-ge0c7