Class: Thin::Headers
- Inherits:
-
Object
- Object
- Thin::Headers
- 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
-
#[]=(key, value) ⇒ Object
Add
key: valuepair to the headers. - #has_key?(key) ⇒ Boolean
-
#initialize ⇒ Headers
constructor
A new instance of Headers.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Headers
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
39 40 41 |
# File 'lib/thin/headers.rb', line 39 def has_key?(key) @sent[key.downcase] end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/thin/headers.rb', line 43 def to_s @out.join end |