class Protocol::HTTP::Headers

Headers are an array of key-value pairs. Some header keys represent multiple values.

def self.[] hash

def self.[] hash
	self.new(hash.to_a)
end

def == other

def == other
	case other
	when Hash
		to_h == other
	when Headers
		@fields == other.fields
	else
		@fields == other
	end
end

def [] key

def [] key
	to_h[key]
end

def []= key, value

Parameters:
  • value () -- The header value.
  • key (String) -- The header key.
def []= key, value
	if @indexed
		merge_into(@indexed, key.downcase, value)
	end
	
	@fields << [key, value]
end

def add(key, value)

def add(key, value)
	self[key] = value
end

def delete(key)

Delete all headers with the given key, and return the merged value.
def delete(key)
	deleted, @fields = @fields.partition do |field|
		field.first.downcase == key
	end
	
	if deleted.empty?
		return nil
	end
	
	if @indexed
		return @indexed.delete(key)
	elsif policy = MERGE_POLICY[key]
		(key, value), *tail = deleted
		merged = policy.new(value)
		
		tail.each{|k,v| merged << v}
		
		return merged
	else
		key, value = deleted.last
		return value
	end
end

def dup

def dup
	self.class.new(@fields, @indexed)
end

def each(&block)

def each(&block)
	@fields.each(&block)
end

def empty?

def empty?
	@fields.empty?
end

def extract(keys)

def extract(keys)
	deleted, @fields = @fields.partition do |field|
		keys.include?(field.first.downcase)
	end
	
	if @indexed
		keys.each do |key|
			@indexed.delete(key)
		end
	end
	
	return deleted
end

def freeze

def freeze
	return if frozen?
	
	# Ensure @indexed is generated:
	self.to_h
	
	@fields.freeze
	@indexed.freeze
	
	super
end

def include? key

def include? key
	self[key] != nil
end

def initialize(fields = nil, indexed = nil)

def initialize(fields = nil, indexed = nil)
	if fields
		@fields = fields.dup
	else
		@fields = []
	end
	
	if indexed
		@indexed = indexed.dup
	else
		@indexed = nil
	end
end

def inspect

def inspect
	"#<#{self.class} #{@fields.inspect}>"
end

def merge(headers)

def merge(headers)
	self.dup.merge!(headers)
end

def merge!(headers)

def merge!(headers)
	headers.each do |key, value|
		self[key] = value
	end
	
	return self
end

def merge_into(hash, key, value)

def merge_into(hash, key, value)
 = MERGE_POLICY[key]
nt_value = hash[key]
_value << value
y] = policy.new(value)
't merge these, we only expose the last one set.
] = value

def to_h

A hash table of `{key, policy[key].map(values)}`
def to_h
	@indexed ||= @fields.inject({}) do |hash, (key, value)|
		merge_into(hash, key.downcase, value)
		
		hash
	end
end