From 77f1e141e75f27b81bef5c3bddd01948d6236ebf Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 17 Apr 2010 10:33:43 -0700 Subject: http_response: split out header stringification code 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. --- lib/rainbows/http_response.rb | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'lib/rainbows/http_response.rb') 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 -- cgit v1.2.3-24-ge0c7