From 95fa512342b4495cf9d56bbf663aa2114a85a4e1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 16 Apr 2009 22:28:06 -0700 Subject: Small garbage reduction in HttpResponse Avoid creating new string objects and then discarding them right away by stuffing non-constant but always-present headers into the initial output. --- lib/unicorn/http_response.rb | 11 ++++++----- test/unit/test_response.rb | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb index c0b1081..b355ad4 100644 --- a/lib/unicorn/http_response.rb +++ b/lib/unicorn/http_response.rb @@ -25,15 +25,13 @@ module Unicorn # Connection: and Date: headers no matter what (if anything) our # Rack application sent us. SKIP = { 'connection' => true, 'date' => true, 'status' => true }.freeze + HEADER_OUT = [ "Connection: close" ] # :nodoc # writes the rack_response to socket as an HTTP response def self.write(socket, rack_response) status, headers, body = rack_response status = "#{status} #{HTTP_STATUS_CODES[status]}" - - # Date is required by HTTP/1.1 as long as our clock can be trusted. - # Some broken clients require a "Status" header so we accomodate them - out = [ "Date: #{Time.now.httpdate}", "Status: #{status}" ] + out = HEADER_OUT.dup # shallow copy # Don't bother enforcing duplicate supression, it's a Hash most of # the time anyways so just hope our app knows what it's doing @@ -48,9 +46,12 @@ module Unicorn # Rack should enforce Content-Length or chunked transfer encoding, # so don't worry or care about them. + # Date is required by HTTP/1.1 as long as our clock can be trusted. + # Some broken clients require a "Status" header so we accomodate them socket_write(socket, "HTTP/1.1 #{status}\r\n" \ - "Connection: close\r\n" \ + "Date: #{Time.now.httpdate}\r\n" \ + "Status: #{status}\r\n" \ "#{out.join("\r\n")}\r\n\r\n") body.each { |chunk| socket_write(socket, chunk) } socket.close # uncorks the socket immediately diff --git a/test/unit/test_response.rb b/test/unit/test_response.rb index 8abc86c..1d47c57 100644 --- a/test/unit/test_response.rb +++ b/test/unit/test_response.rb @@ -60,7 +60,7 @@ class ResponseTest < Test::Unit::TestCase out = StringIO.new HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, []]) assert out.closed? - assert_match(/^Status: 200 OK\r\nX-Whatever: stuff\r\n/, out.string) + assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size end # we always favor the code returned by the application, since "Status" @@ -71,7 +71,7 @@ class ResponseTest < Test::Unit::TestCase header_hash = {"X-Whatever" => "stuff", 'StaTus' => "666" } HttpResponse.write(out,[200, header_hash, []]) assert out.closed? - assert_match(/^Status: 200 OK\r\nX-Whatever: stuff\r\n/, out.string) + assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size assert_equal 1, out.string.split(/\r\n/).grep(/^Status:/i).size end -- cgit v1.2.3-24-ge0c7