about summary refs log tree commit homepage
path: root/lib/rainbows/http_response.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-04 22:16:52 +0000
committerEric Wong <normalperson@yhbt.net>2010-07-04 22:34:09 +0000
commit39b178cdebe275cbc8ce19cf269bea7cd15ff4ca (patch)
treeb7628ed278895fcf70ea3206956be586ac9e1ac5 /lib/rainbows/http_response.rb
parent75f5aa9a0d6b37a94afbea3121fc2c16e70a2b1d (diff)
downloadrainbows-39b178cdebe275cbc8ce19cf269bea7cd15ff4ca.tar.gz
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.
Diffstat (limited to 'lib/rainbows/http_response.rb')
-rw-r--r--lib/rainbows/http_response.rb21
1 files changed, 14 insertions, 7 deletions
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: