From bf680b1a61ab17b6a0e17432fd4ab22358ec83bd Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 5 Feb 2009 14:11:09 -0800 Subject: Remove StringIO usage in HeaderOut It's more GC-friendly to just use an array than to repeatedly append short strings on top of each other. I also find StringIO confusing... --- lib/unicorn/const.rb | 1 - lib/unicorn/header_out.rb | 28 ++++++++++++++++++++-------- lib/unicorn/http_response.rb | 6 ++---- 3 files changed, 22 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/unicorn/const.rb b/lib/unicorn/const.rb index 56c3bb4..8cefd2b 100644 --- a/lib/unicorn/const.rb +++ b/lib/unicorn/const.rb @@ -101,7 +101,6 @@ module Unicorn HEAD="HEAD".freeze # ETag is based on the apache standard of hex mtime-size-inode (inode is 0 on win32) ETAG_FORMAT="\"%x-%x-%x\"".freeze - HEADER_FORMAT="%s: %s\r\n".freeze LINE_END="\r\n".freeze REMOTE_ADDR="REMOTE_ADDR".freeze HTTP_X_FORWARDED_FOR="HTTP_X_FORWARDED_FOR".freeze diff --git a/lib/unicorn/header_out.rb b/lib/unicorn/header_out.rb index a4d987c..b0f66b9 100644 --- a/lib/unicorn/header_out.rb +++ b/lib/unicorn/header_out.rb @@ -7,14 +7,21 @@ module Unicorn # by just doing them twice (which is sometimes needed in HTTP), but that the normal # semantics for Hash (where doing an insert replaces) is not there. class HeaderOut - attr_reader :out - attr_accessor :allowed_duplicates + ALLOWED_DUPLICATES = { + 'Set-Cookie' => true, + 'Set-Cookie2' => true, + 'Warning' => true, + 'WWW-Authenticate' => true, + }.freeze - def initialize(out = StringIO.new) + def initialize @sent = {} - @allowed_duplicates = {"Set-Cookie" => true, "Set-Cookie2" => true, - "Warning" => true, "WWW-Authenticate" => true} - @out = out + @out = [] + end + + def reset! + @sent.clear + @out.clear end def merge!(hash) @@ -25,10 +32,15 @@ module Unicorn # Simply writes "#{key}: #{value}" to an output buffer. def[]=(key,value) - if not @sent.has_key?(key) or @allowed_duplicates.has_key?(key) + if not @sent.has_key?(key) or ALLOWED_DUPLICATES.has_key?(key) @sent[key] = true - @out.write(Const::HEADER_FORMAT % [key, value]) + @out << "#{key}: #{value}\r\n" end end + + def to_s + @out.join + end + end end diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb index 5fbc990..c3094cf 100644 --- a/lib/unicorn/http_response.rb +++ b/lib/unicorn/http_response.rb @@ -77,8 +77,7 @@ module Unicorn raise "You have already sent the request headers." else # XXX Dubious ( http://mongrel.rubyforge.org/ticket/19 ) - @header.out.close - @header = HeaderOut.new(StringIO.new) + @header.reset! @body.close @body = StringIO.new @@ -95,8 +94,7 @@ module Unicorn def send_header if not @header_sent - @header.out.rewind - write(@header.out.read + Const::LINE_END) + write("#{@header.to_s}#{Const::LINE_END}") @header_sent = true end end -- cgit v1.2.3-24-ge0c7