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-04 14:30:40 -0800
committerEric Wong <normalperson@yhbt.net>2009-02-09 19:50:36 -0800
commit28d571b7cca709641d964e00e6004facb6bfcc7e (patch)
treeed260a408088db92adb190d2b9eddaa6be8ab580 /lib/unicorn/header_out.rb
parent66254b6f2b0ebb3899413b12d96614ac9318daae (diff)
downloadunicorn-28d571b7cca709641d964e00e6004facb6bfcc7e.tar.gz
Avoid conflicting with existing Mongrel libraries since
we'll be incompatible and break things w/o disrupting
Mongrel installations.
Diffstat (limited to 'lib/unicorn/header_out.rb')
-rw-r--r--lib/unicorn/header_out.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/unicorn/header_out.rb b/lib/unicorn/header_out.rb
new file mode 100644
index 0000000..a4d987c
--- /dev/null
+++ b/lib/unicorn/header_out.rb
@@ -0,0 +1,34 @@
+module Unicorn
+  # This class implements a simple way of constructing the HTTP headers dynamically
+  # via a Hash syntax.  Think of it as a write-only Hash.  Refer to HttpResponse for
+  # information on how this is used.
+  #
+  # One consequence of this write-only nature is that you can write multiple headers
+  # 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
+
+    def initialize(out = StringIO.new)
+      @sent = {}
+      @allowed_duplicates = {"Set-Cookie" => true, "Set-Cookie2" => true,
+        "Warning" => true, "WWW-Authenticate" => true}
+      @out = out
+    end
+
+    def merge!(hash)
+      hash.each do |key, value|
+        self[key] = value
+      end
+    end
+
+    # Simply writes "#{key}: #{value}" to an output buffer.
+    def[]=(key,value)
+      if not @sent.has_key?(key) or @allowed_duplicates.has_key?(key)
+        @sent[key] = true
+        @out.write(Const::HEADER_FORMAT % [key, value])
+      end
+    end
+  end
+end