unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 008bff82215b67c86c749ff92c2f2019b8bb21a3 1115 bytes (raw)
$ git show v0.0.0:lib/mongrel/header_out.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
module Mongrel
  # 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

git clone https://yhbt.net/unicorn.git