about summary refs log tree commit homepage
path: root/lib/unicorn/header_out.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-02-05 14:11:09 -0800
committerEric Wong <normalperson@yhbt.net>2009-02-09 19:50:40 -0800
commitbf680b1a61ab17b6a0e17432fd4ab22358ec83bd (patch)
treef6e42b3c3514eeb71fe0332b948d95f2b498dc5e /lib/unicorn/header_out.rb
parentce5868855c7bbd0aa82dcd60b2e27a3fd3d5f9fb (diff)
downloadunicorn-bf680b1a61ab17b6a0e17432fd4ab22358ec83bd.tar.gz
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...
Diffstat (limited to 'lib/unicorn/header_out.rb')
-rw-r--r--lib/unicorn/header_out.rb28
1 files changed, 20 insertions, 8 deletions
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