class HTTP::Headers
Constants
- ACCEPT
-
Content-Types that are acceptable for the response.
- ACCEPT_ENCODING
-
Content-codings that are acceptable in the response.
- AGE
-
The age the object has been in a proxy cache in seconds.
- AUTHORIZATION
-
Authentication credentials for
HTTP
authentication. - CACHE_CONTROL
-
Used to specify directives that must be obeyed by all caching mechanisms along the request-response chain.
- CONNECTION
-
Control options for the current connection and list of hop-by-hop request fields.
- CONTENT_ENCODING
-
Indicates what additional content codings have been applied to the entity-body.
- CONTENT_LENGTH
-
The length of the request body in octets (8-bit bytes).
- CONTENT_TYPE
-
The MIME type of the body of the request (used with POST and PUT requests).
- COOKIE
-
An
HTTP
cookie previously sent by the server with Set-Cookie. - DATE
-
The date and time that the message was sent (in “HTTP-date” format as defined by RFC 7231 Date/Time Formats).
- ETAG
-
An identifier for a specific version of a resource, often a message digest.
- EXPIRES
-
Gives the date/time after which the response is considered stale (in “HTTP-date” format as defined by RFC 7231).
- HOST
-
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.
- IF_MODIFIED_SINCE
-
Allows a 304 Not Modified to be returned if content is unchanged.
- IF_NONE_MATCH
-
Allows a 304 Not Modified to be returned if content is unchanged.
- LAST_MODIFIED
-
The last modified date for the requested object (in “HTTP-date” format as defined by RFC 7231).
- LOCATION
-
Used in redirection, or when a new resource has been created.
- PROXY_AUTHORIZATION
-
Authorization credentials for connecting to a proxy.
- SET_COOKIE
-
An
HTTP
cookie. - TRANSFER_ENCODING
-
The form of encoding used to safely transfer the entity to the user. Currently defined methods are: chunked, compress, deflate, gzip, identity.
- USER_AGENT
-
The user agent string of the user agent.
- VARY
-
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.
Public Class Methods
Source
# File lib/http/headers.rb, line 22 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
Coerces given ‘object` into Headers
.
@raise [Error] if object can’t be coerced @param [#to_hash, to_h
, to_a
] object @return [Headers]
Source
# File lib/http/headers.rb, line 43 def initialize # The @pile stores each header value using a three element array: # 0 - the normalized header key, used for lookup # 1 - the header key as it will be sent with a request # 2 - the value @pile = [] end
Class constructor.
Source
# File lib/http/headers.rb, line 37 def normalizer @normalizer ||= Headers::Normalizer.new end
Public Instance Methods
Source
# File lib/http/headers.rb, line 162 def ==(other) return false unless other.respond_to? :to_a to_a == other.to_a end
Compares headers to another Headers
or Array of key/value pairs
@return [Boolean]
Source
# File lib/http/headers.rb, line 112 def [](name) values = get(name) case values.count when 0 then nil when 1 then values.first else values end 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
Source
# File lib/http/headers.rb, line 80 def add(name, value) lookup_name = normalize_header(name.to_s) wire_name = case name when String name when Symbol lookup_name else raise HTTP::HeaderError, "HTTP header must be a String or Symbol: #{name.inspect}" end Array(value).each do |v| @pile << [ lookup_name, wire_name, validate_value(v) ] end end
Appends header.
@param [String, Symbol] name header name. When specified as a string, the
name is sent as-is. When specified as a symbol, the name is converted to a string of capitalized words separated by a dash. Word boundaries are determined by an underscore (`_`) or a dash (`-`). Ex: `:content_type` is sent as `"Content-Type"`, and `"auth_key"` (string) is sent as `"auth_key"`.
@param [Array<#to_s>, to_s] value header value(s) to be appended @return [void]
Source
# File lib/http/headers.rb, line 65 def delete(name) name = normalize_header name.to_s @pile.delete_if { |k, _| k == name } end
Removes header.
@param [#to_s] name header name @return [void]
Source
# File lib/http/headers.rb, line 172 def each return to_enum(__method__) unless block_given? @pile.each { |item| yield(item[1..2]) } self end
Calls the given block once for each key/value pair in headers container.
@return [Enumerator] if no block given @return [Headers] self-reference
Source
# File lib/http/headers.rb, line 102 def get(name) name = normalize_header name.to_s @pile.select { |k, _| k == name }.map { |_, _, v| v } end
Returns list of header values if any.
@return [Array<String>]
Source
# File lib/http/headers.rb, line 125 def include?(name) name = normalize_header name.to_s @pile.any? { |k, _| k == name } end
Tells whenever header with given ‘name` is set or not.
@return [Boolean]
Source
# File lib/http/headers.rb, line 196 def initialize_copy(orig) super @pile = @pile.map(&:dup) end
Properly clones internal key/value storage.
@api private
Source
# File lib/http/headers.rb, line 148 def inspect "#<#{self.class} #{to_h.inspect}>" end
Returns human-readable representation of ‘self` instance.
@return [String]
Source
# File lib/http/headers.rb, line 155 def keys @pile.map { |_, k, _| k }.uniq end
Returns list of header names.
@return [Array<String>]
Source
# File lib/http/headers.rb, line 213 def merge(other) dup.tap { |dupped| dupped.merge! other } end
Returns new instance with ‘other` headers merged in.
@see merge!
@return [Headers]
Source
# File lib/http/headers.rb, line 205 def merge!(other) self.class.coerce(other).to_h.each { |name, values| set name, values } end
Merges ‘other` headers into `self`.
@see merge
@return [void]
Source
# File lib/http/headers.rb, line 55 def set(name, value) delete(name) add(name, value) end
Sets header.
@param (see add
) @return [void]
Source
# File lib/http/headers.rb, line 141 def to_a @pile.map { |item| item[1..2] } end
Returns headers key/value pairs.
@return [Array<[String, String]>]
Source
# File lib/http/headers.rb, line 133 def to_h keys.to_h { |k| [k, self[k]] } end
Returns Rack-compatible headers Hash
@return [Hash]
Private Instance Methods
Source
# File lib/http/headers.rb, line 220 def normalize_header(name) self.class.normalizer.call(name) end
Transforms ‘name` to canonical HTTP
header capitalization
Source
# File lib/http/headers.rb, line 229 def validate_value(value) v = value.to_s return v unless v.include?("\n") raise HeaderError, "Invalid HTTP header field value: #{v.inspect}" end
Ensures there is no new line character in the header value
@param [String] value @raise [HeaderError] if value includes new line character @return [String] stringified header value