Sha256: 70f971e7aaf43165e573a828c94a027e58efcf4c5d6cee9441391482fac749ed

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module Thin
  # Raised when an header is not valid
  # and the server can not process it.
  class InvalidHeader < StandardError; end

  # Store HTTP header name-value pairs direcly to a string
  # and allow duplicated entries on some names.
  class Headers
    HEADER_FORMAT      = "%s: %s\r\n".freeze
    ALLOWED_DUPLICATES = %w(set-cookie set-cookie2 warning www-authenticate).freeze
    CR_OR_LF           = /[\r\n]/.freeze
    
    def initialize
      @sent = {}
      @out = []
    end
    
    # Add <tt>key: value</tt> pair to the headers.
    # Ignore if already sent and no duplicates are allowed
    # for this +key+.
    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
    
    def has_key?(key)
      @sent[key.downcase]
    end
    
    def to_s
      @out.join
    end
  end
end

Version data entries

4 entries across 3 versions & 2 rubygems

Version Path
thin-1.8.2 lib/thin/headers.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/thin-1.8.1/lib/thin/headers.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/thin-1.8.1/lib/thin/headers.rb
thin-1.8.1 lib/thin/headers.rb