Protocol::HTTPSourceProtocolHTTPHeaders

class Headers

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

Nested

Definitions

def self.[] headers

Construct an instance from a headers Array or Hash. No-op if already an instance of Headers. If the underlying array is frozen, it will be duped.

Implementation

def self.[] headers
	if headers.nil?
		return self.new
	end
	
	if headers.is_a?(self)
		if headers.frozen?
			return headers.dup
		else
			return headers
		end
	end
	
	fields = headers.to_a
	
	if fields.frozen?
		fields = fields.dup
	end
	
	return self.new(fields)
end

def flatten!

Flatten trailer into the headers.

Implementation

def flatten!
	if @tail
		self.delete(TRAILER)
		@tail = nil
	end
	
	return self
end

attr :fields

An array of [key, value] pairs.

def trailer?

Implementation

def trailer?
	@tail != nil
end

def trailer!(&block)

Record the current headers, and prepare to add trailers.

This method is typically used after headers are sent to capture any additional headers which should then be sent as trailers.

A sender that intends to generate one or more trailer fields in a message should generate a trailer header field in the header section of that message to indicate which fields might be present in the trailers.

Signature

parameter names Array

The trailer header names which will be added later.

Implementation

def trailer!(&block)
	@tail ||= @fields.size
	
	return trailer(&block)
end

def trailer(&block)

Enumerate all headers in the trailer, if there are any.

Implementation

def trailer(&block)
	return to_enum(:trailer) unless block_given?
	
	if @tail
		@fields.drop(@tail).each(&block)
	end
end

def add(key, value)

Add the specified header key value pair.

Implementation

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

def set(key, value)

Set the specified header key to the specified value, replacing any existing header keys with the same name.

Implementation

def set(key, value)
	# TODO This could be a bit more efficient:
	self.delete(key)
	self.add(key, value)
end

def []= key, value

Append the value to the given key. Some values can be appended multiple times, others can only be set once.

Implementation

def []= key, value
	if @indexed
		merge_into(@indexed, key.downcase, value)
	end
	
	@fields << [key, value]
end

def delete(key)

Delete all headers with the given key, and return the merged value.

Implementation

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 = 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 to_h

A hash table of {key, policy[key].map(values)}

Implementation

def to_h
	@indexed ||= @fields.inject({}) do |hash, (key, value)|
		merge_into(hash, key.downcase, value)
		
		hash
	end
end