Class: Thin::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/thin/headers.rb

Overview

Store HTTP header name-value pairs direcly to a string and allow duplicated entries on some names.

Constant Summary collapse

HEADER_FORMAT =
"%s: %s\r\n".freeze
ALLOWED_DUPLICATES =
%w(set-cookie set-cookie2 warning www-authenticate).freeze
CR_OR_LF =
/[\r\n]/.freeze

Instance Method Summary collapse

Constructor Details

#initializeHeaders

Returns a new instance of Headers.



13
14
15
16
# File 'lib/thin/headers.rb', line 13

def initialize
  @sent = {}
  @out = []
end

Instance Method Details

#[]=(key, value) ⇒ Object

Add key: value pair to the headers. Ignore if already sent and no duplicates are allowed for this key.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thin/headers.rb', line 21

def []=(key, value)
  downcase_key = key.downcase
  if !@sent.has_key?(downcase_key) || ALLOWED_DUPLICATES.include?(downcase_key)
    @sent[downcase_key] = true
    value = case value
            when Time
              value.httpdate
            when NilClass
              return
            when CR_OR_LF
              raise InvalidHeader, "Header contains CR or LF"
            else
              value.to_s
            end
    @out << HEADER_FORMAT % [key, value]
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/thin/headers.rb', line 39

def has_key?(key)
  @sent[key.downcase]
end

#to_sObject



43
44
45
# File 'lib/thin/headers.rb', line 43

def to_s
  @out.join
end