Content-Types that are acceptable for the response.
The age the object has been in a proxy cache in seconds.
Authentication credentials for HTTP authentication.
Used to specify directives that must be obeyed by all caching mechanisms along the request-response chain.
Matches HTTP header names when in “Canonical-Http-Format”
Matches valid header field name according to RFC. @see tools.ietf.org/html/rfc7230#section-3.2
Control options for the current connection and list of hop-by-hop request fields.
Indicates what additional content codings have been applied to the entity-body.
The length of the request body in octets (8-bit bytes).
The MIME type of the body of the request (used with POST and PUT requests).
An HTTP cookie previously sent by the server with Set-Cookie.
The date and time that the message was sent (in “HTTP-date” format as defined by RFC 7231 Date/Time Formats).
An identifier for a specific version of a resource, often a message digest.
Gives the date/time after which the response is considered stale (in “HTTP-date” format as defined by RFC 7231).
The domain name of the server (for virtual hosting), and the TCP port number on which the server is listening. The port number may be omitted if the port is the standard port for the service requested.
Allows a 304 Not Modified to be returned if content is unchanged.
Allows a 304 Not Modified to be returned if content is unchanged.
The last modified date for the requested object (in “HTTP-date” format as defined by RFC 7231).
Used in redirection, or when a new resource has been created.
Authorization credentials for connecting to a proxy.
An HTTP cookie.
The form of encoding used to safely transfer the entity to the user. Currently defined methods are: chunked, compress, deflate, gzip, identity.
The user agent string of the user agent.
Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server.
Coerces given `object` into Headers.
@raise [Error] if object can't be coerced @param [#to_hash, to_h, to_a] object @return [Headers]
# File lib/http/headers.rb, line 177 def coerce(object) unless object.is_a? self object = case when object.respond_to?(:to_hash) then object.to_hash when object.respond_to?(:to_h) then object.to_h when object.respond_to?(:to_a) then object.to_a else raise Error, "Can't coerce #{object.inspect} to Headers" end end headers = new object.each { |k, v| headers.add k, v } headers end
Class constructor.
# File lib/http/headers.rb, line 22 def initialize @pile = [] end
Compares headers to another Headers or Array of key/value pairs
@return [Boolean]
# File lib/http/headers.rb, line 118 def ==(other) return false unless other.respond_to? :to_a @pile == other.to_a end
Smart version of {#get}.
@return [nil] if header was not set @return [String] if header has exactly one value @return [Array<String>] if header has more than one value
# File lib/http/headers.rb, line 68 def [](name) values = get(name) case values.count when 0 then nil when 1 then values.first else values end end
Appends header.
@param [#to_s] name header name @param [Array<#to_s>, to_s] value header value(s) to be appended @return [void]
# File lib/http/headers.rb, line 50 def add(name, value) name = normalize_header name.to_s Array(value).each { |v| @pile << [name, v.to_s] } end
Removes header.
@param [#to_s] name header name @return [void]
# File lib/http/headers.rb, line 40 def delete(name) name = normalize_header name.to_s @pile.delete_if { |k, _| k == name } end
Calls the given block once for each key/value pair in headers container.
@return [Enumerator] if no block given @return [Headers] self-reference
# File lib/http/headers.rb, line 127 def each return to_enum(__method__) unless block_given? @pile.each { |arr| yield(arr) } self end
Returns list of header values if any.
@return [Array<String>]
# File lib/http/headers.rb, line 58 def get(name) name = normalize_header name.to_s @pile.select { |k, _| k == name }.map { |_, v| v } end
Tells whenever header with given `name` is set or not.
@return [Boolean]
# File lib/http/headers.rb, line 81 def include?(name) name = normalize_header name.to_s @pile.any? { |k, _| k == name } end
Properly clones internal key/value storage.
@api private
# File lib/http/headers.rb, line 150 def initialize_copy(orig) super @pile = to_a end
Returns human-readable representation of `self` instance.
@return [String]
# File lib/http/headers.rb, line 104 def inspect "#<#{self.class} #{to_h.inspect}>" end
Returns list of header names.
@return [Array<String>]
# File lib/http/headers.rb, line 111 def keys @pile.map { |k, _| k }.uniq end
Returns new instance with `other` headers merged in.
@see merge! @return [Headers]
# File lib/http/headers.rb, line 167 def merge(other) dup.tap { |dupped| dupped.merge! other } end
Merges `other` headers into `self`.
@see merge @return [void]
# File lib/http/headers.rb, line 159 def merge!(other) self.class.coerce(other).to_h.each { |name, values| set name, values } end
Sets header.
@param (see add) @return [void]
# File lib/http/headers.rb, line 30 def set(name, value) delete(name) add(name, value) end
Returns headers key/value pairs.
@return [Array<[String, String]>]
# File lib/http/headers.rb, line 97 def to_a @pile.map { |pair| pair.map(&:dup) } end
Returns Rack-compatible headers Hash
@return [Hash]
# File lib/http/headers.rb, line 89 def to_h Hash[keys.map { |k| [k, self[k]] }] end
Transforms `name` to canonical HTTP header capitalization
@param [String] name @raise [InvalidHeaderNameError] if normalized name does not
match {HEADER_NAME_RE}
@return [String] canonical HTTP header name
# File lib/http/headers.rb, line 202 def normalize_header(name) return name if name =~ CANONICAL_NAME_RE normalized = name.split(/[\-_]/).each(&:capitalize!).join("-") return normalized if normalized =~ COMPLIANT_NAME_RE raise InvalidHeaderNameError, "Invalid HTTP header field name: #{name.inspect}" end