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

Instance Method Summary collapse

Constructor Details

#initializeHeaders

Returns a new instance of Headers.



8
9
10
11
# File 'lib/thin/headers.rb', line 8

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.



16
17
18
19
20
21
# File 'lib/thin/headers.rb', line 16

def []=(key, value)
  if !@sent.has_key?(key) || ALLOWED_DUPLICATES.include?(key)
    @sent[key] = true
    @out << HEADER_FORMAT % [key, value]
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/thin/headers.rb', line 23

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

#to_sObject



27
28
29
# File 'lib/thin/headers.rb', line 27

def to_s
  @out.join
end