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/http_response.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'lib/rainbows/http_response.rb') diff --git a/lib/rainbows/http_response.rb b/lib/rainbows/http_response.rb index 811a793..677b5a7 100644 --- a/lib/rainbows/http_response.rb +++ b/lib/rainbows/http_response.rb @@ -6,7 +6,8 @@ module Rainbows::HttpResponse CODES = Unicorn::HttpResponse::CODES - def self.header_string(status, headers, out) + def response_header(response, out) + status, headers = response status = CODES[status.to_i] || status headers.each do |key, value| @@ -25,13 +26,19 @@ module Rainbows::HttpResponse "#{out.join('')}\r\n" end - def self.write(socket, rack_response, out = []) - status, headers, body = rack_response - out and socket.write(header_string(status, headers, out)) + def write_header(socket, response, out) + out and socket.write(response_header(response, out)) + end + + def write_response(socket, response, out) + write_header(socket, response, out) + write_body(socket, response[2]) + end - body.each { |chunk| socket.write(chunk) } - ensure - body.respond_to?(:close) and body.close + # called after forking + def self.setup(klass) + require('rainbows/http_response/body') and + klass.__send__(:include, Rainbows::HttpResponse::Body) end end # :startdoc: -- cgit v1.2.3-24-ge0c7