class HTTPX::Headers
def ==(other)
def ==(other) other == to_hash end
def [](field)
identified by +field+, or nil otherwise.
returns the comma-separated values of the header field
def [](field) a = @headers[downcased(field)] || return a.join(", ") end
def []=(field, value)
sets +value+ (if not nil) as single value for the +field+ header.
def []=(field, value) return unless value @headers[downcased(field)] = array_value(value) end
def add(field, value)
adds additional +value+ to the existing, for header +field+.
def add(field, value) (@headers[downcased(field)] ||= []) << String(value) end
def array_value(value)
def array_value(value) case value when Array value.map { |val| String(val).strip } else [String(value).strip] end end
def delete(field)
deletes all values associated with +field+ header.
def delete(field) canonical = downcased(field) @headers.delete(canonical) if @headers.key?(canonical) end
def downcased(field)
def downcased(field) String(field).downcase end
def each(extra_headers = nil)
the comma-separated string format
returns the enumerable headers store in pairs of header field + the values in
def each(extra_headers = nil) return enum_for(__method__, extra_headers) { @headers.size } unless block_given? @headers.each do |field, value| yield(field, value.join(", ")) unless value.empty? end extra_headers.each do |field, value| yield(field, value) unless value.empty? end if extra_headers end
def freeze
def freeze @headers.freeze super end
def get(field)
to "correct" the user input, i.e. it doesn't downcase the key.
This method is more internal, and for this reason doesn't try
returns the values for the +field+ header in array format.
def get(field) @headers[field] || EMPTY end
def initialize(headers = nil)
def initialize(headers = nil) @headers = {} return unless headers headers.each do |field, value| array_value(value).each do |v| add(downcased(field), v) end end end
def initialize_clone(orig)
def initialize_clone(orig) super @headers = orig.instance_variable_get(:@headers).clone end
def initialize_dup(orig)
def initialize_dup(orig) super @headers = orig.instance_variable_get(:@headers).dup end
def inspect
def inspect to_hash.inspect end
def key?(downcased_key)
Please do not use this outside of core!
guarantees, like downcasing strings.
this is internal API and doesn't abide to other public API
def key?(downcased_key) @headers.key?(downcased_key) end
def merge(other)
ignore what the +other+ headers has. Otherwise, set
the merge rule is, if the header already exists,
merges headers with another header-quack.
def merge(other) headers = dup other.each do |field, value| headers[downcased(field)] = value end headers end
def new(headers = nil)
def new(headers = nil) return headers if headers.is_a?(self) super end
def same_headers?(headers)
def same_headers?(headers) @headers.empty? || begin headers.each do |k, v| next unless key?(k) return false unless v == self[k] end true end end
def to_a
def to_a Array(each) end
def to_hash
def to_hash Hash[to_a] end
def to_s
def to_s @headers.to_s end