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-04-17 10:33:43 -0700
committerEric Wong <normalperson@yhbt.net>2010-04-19 01:22:35 -0700
commit77f1e141e75f27b81bef5c3bddd01948d6236ebf (patch)
tree7a00c312df1890434e321f5224d95ef2df611d11 /lib/rainbows/http_response.rb
parentb07004fc79867ba8f0cacde1508fb1e4fbf4c37d (diff)
downloadrainbows-77f1e141e75f27b81bef5c3bddd01948d6236ebf.tar.gz
This will make it easier to use body#to_path if possible since
some concurrency models like EventMachine have optimized code
paths for serving static files.
Diffstat (limited to 'lib/rainbows/http_response.rb')
-rw-r--r--lib/rainbows/http_response.rb40
1 files changed, 21 insertions, 19 deletions
diff --git a/lib/rainbows/http_response.rb b/lib/rainbows/http_response.rb
index 7806eb1..1933218 100644
--- a/lib/rainbows/http_response.rb
+++ b/lib/rainbows/http_response.rb
@@ -5,29 +5,31 @@ module Rainbows
 
   class HttpResponse < ::Unicorn::HttpResponse
 
-    def self.write(socket, rack_response, out = [])
-      status, headers, body = rack_response
-
-      if Array === out
-        status = CODES[status.to_i] || status
+    def self.header_string(status, headers, out)
+      status = CODES[status.to_i] || status
 
-        headers.each do |key, value|
-          next if %r{\AX-Rainbows-}i =~ key
-          next if SKIP.include?(key.downcase)
-          if value =~ /\n/
-            # avoiding blank, key-only cookies with /\n+/
-            out.concat(value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" })
-          else
-            out << "#{key}: #{value}\r\n"
-          end
+      headers.each do |key, value|
+        next if %r{\AX-Rainbows-}i =~ key
+        next if SKIP.include?(key.downcase)
+        if value =~ /\n/
+          # avoiding blank, key-only cookies with /\n+/
+          out.concat(value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" })
+        else
+          out << "#{key}: #{value}\r\n"
         end
-
-        socket.write("HTTP/1.1 #{status}\r\n" \
-                     "Date: #{Time.now.httpdate}\r\n" \
-                     "Status: #{status}\r\n" \
-                     "#{out.join('')}\r\n")
       end
 
+      "HTTP/1.1 #{status}\r\n" \
+      "Date: #{Time.now.httpdate}\r\n" \
+      "Status: #{status}\r\n" \
+      "#{out.join('')}\r\n"
+    end
+
+    def self.write(socket, rack_response, out = [])
+      status, headers, body = rack_response
+      out.instance_of?(Array) and
+        socket.write(header_string(status, headers, out))
+
       body.each { |chunk| socket.write(chunk) }
       ensure
         body.respond_to?(:close) and body.close