Class: HTTP::Headers
- Inherits:
-
Object
- Object
- HTTP::Headers
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/http/headers.rb,
lib/http/headers/known.rb,
lib/http/headers/mixin.rb,
lib/http/headers/normalizer.rb
Overview
HTTP Headers container.
Defined Under Namespace
Modules: Mixin Classes: Normalizer
Constant Summary collapse
- ACCEPT =
Content-Types that are acceptable for the response.
"Accept"
- ACCEPT_ENCODING =
Content-codings that are acceptable in the response.
"Accept-Encoding"
- AGE =
The age the object has been in a proxy cache in seconds.
"Age"
- AUTHORIZATION =
Authentication credentials for HTTP authentication.
"Authorization"
- CACHE_CONTROL =
Used to specify directives that must be obeyed by all caching mechanisms along the request-response chain.
"Cache-Control"
- COOKIE =
An HTTP cookie previously sent by the server with Set-Cookie.
"Cookie"
- CONNECTION =
Control options for the current connection and list of hop-by-hop request fields.
"Connection"
- CONTENT_LENGTH =
The length of the request body in octets (8-bit bytes).
"Content-Length"
- CONTENT_TYPE =
The MIME type of the body of the request (used with POST and PUT requests).
"Content-Type"
- DATE =
The date and time that the message was sent (in "HTTP-date" format as defined by RFC 7231 Date/Time Formats).
"Date"
- ETAG =
An identifier for a specific version of a resource, often a message digest.
"ETag"
- EXPIRES =
Gives the date/time after which the response is considered stale (in "HTTP-date" format as defined by RFC 7231).
"Expires"
- 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.
"Host"
- IF_MODIFIED_SINCE =
Allows a 304 Not Modified to be returned if content is unchanged.
"If-Modified-Since"
- IF_NONE_MATCH =
Allows a 304 Not Modified to be returned if content is unchanged.
"If-None-Match"
- LAST_MODIFIED =
The last modified date for the requested object (in "HTTP-date" format as defined by RFC 7231).
"Last-Modified"
- LOCATION =
Used in redirection, or when a new resource has been created.
"Location"
- PROXY_AUTHORIZATION =
Authorization credentials for connecting to a proxy.
"Proxy-Authorization"
- SET_COOKIE =
An HTTP cookie.
"Set-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.
"Transfer-Encoding"
- CONTENT_ENCODING =
Indicates what additional content codings have been applied to the entity-body.
"Content-Encoding"
- USER_AGENT =
The user agent string of the user agent.
"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.
"Vary"
Class Method Summary collapse
-
.coerce(object) ⇒ Headers
(also: [])
Coerces given
object
into Headers. - .normalizer ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares headers to another Headers or Array of key/value pairs.
-
#[](name) ⇒ nil, ...
Smart version of #get.
-
#add(name, value) ⇒ void
Appends header.
-
#delete(name) ⇒ void
Removes header.
-
#each ⇒ Enumerator, Headers
Calls the given block once for each key/value pair in headers container.
-
#empty? ⇒ Boolean
Returns
true
ifself
has no key/value pairs. -
#get(name) ⇒ Array<String>
Returns list of header values if any.
-
#hash ⇒ Fixnum
Compute a hash-code for this headers container.
-
#include?(name) ⇒ Boolean
Tells whenever header with given
name
is set or not. -
#initialize ⇒ Headers
constructor
Class constructor.
-
#initialize_copy(orig) ⇒ Object
private
Properly clones internal key/value storage.
-
#inspect ⇒ String
Returns human-readable representation of
self
instance. -
#keys ⇒ Array<String>
Returns list of header names.
-
#merge(other) ⇒ Headers
Returns new instance with
other
headers merged in. -
#merge!(other) ⇒ void
Merges
other
headers intoself
. -
#set(name, value) ⇒ void
(also: #[]=)
Sets header.
-
#to_a ⇒ Array<[String, String]>
Returns headers key/value pairs.
-
#to_h ⇒ Hash
(also: #to_hash)
Returns Rack-compatible headers Hash.
Constructor Details
#initialize ⇒ Headers
Class constructor.
44 45 46 47 48 49 50 |
# File 'lib/http/headers.rb', line 44 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 Method Details
.coerce(object) ⇒ Headers Also known as: []
Coerces given object
into Headers.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 |
.normalizer ⇒ Object
38 39 40 |
# File 'lib/http/headers.rb', line 38 def normalizer @normalizer ||= Headers::Normalizer.new end |
Instance Method Details
#==(other) ⇒ Boolean
Compares headers to another Headers or Array of key/value pairs
163 164 165 166 167 |
# File 'lib/http/headers.rb', line 163 def ==(other) return false unless other.respond_to? :to_a to_a == other.to_a end |
#[](name) ⇒ nil, ...
Smart version of #get.
113 114 115 116 117 118 119 120 121 |
# File 'lib/http/headers.rb', line 113 def [](name) values = get(name) case values.count when 0 then nil when 1 then values.first else values end end |
#add(name, value) ⇒ void
This method returns an undefined value.
Appends header.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/http/headers.rb', line 81 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 |
#delete(name) ⇒ void
This method returns an undefined value.
Removes header.
66 67 68 69 |
# File 'lib/http/headers.rb', line 66 def delete(name) name = normalize_header name.to_s @pile.delete_if { |k, _| k == name } end |
#each ⇒ Enumerator, Headers
Calls the given block once for each key/value pair in headers container.
173 174 175 176 177 178 |
# File 'lib/http/headers.rb', line 173 def each return to_enum(__method__) unless block_given? @pile.each { |item| yield(item[1..2]) } self end |
#empty? ⇒ Boolean
Returns true
if self
has no key/value pairs
184 |
# File 'lib/http/headers.rb', line 184 def_delegator :@pile, :empty? |
#get(name) ⇒ Array<String>
Returns list of header values if any.
103 104 105 106 |
# File 'lib/http/headers.rb', line 103 def get(name) name = normalize_header name.to_s @pile.select { |k, _| k == name }.map { |_, _, v| v } end |
#hash ⇒ Fixnum
Compute a hash-code for this headers container. Two containers with the same content will have the same hash code.
192 |
# File 'lib/http/headers.rb', line 192 def_delegator :@pile, :hash |
#include?(name) ⇒ Boolean
Tells whenever header with given name
is set or not.
126 127 128 129 |
# File 'lib/http/headers.rb', line 126 def include?(name) name = normalize_header name.to_s @pile.any? { |k, _| k == name } end |
#initialize_copy(orig) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Properly clones internal key/value storage.
197 198 199 200 |
# File 'lib/http/headers.rb', line 197 def initialize_copy(orig) super @pile = @pile.map(&:dup) end |
#inspect ⇒ String
Returns human-readable representation of self
instance.
149 150 151 |
# File 'lib/http/headers.rb', line 149 def inspect "#<#{self.class} #{to_h.inspect}>" end |
#keys ⇒ Array<String>
Returns list of header names.
156 157 158 |
# File 'lib/http/headers.rb', line 156 def keys @pile.map { |_, k, _| k }.uniq end |
#merge(other) ⇒ Headers
Returns new instance with other
headers merged in.
214 215 216 |
# File 'lib/http/headers.rb', line 214 def merge(other) dup.tap { |dupped| dupped.merge! other } end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merges other
headers into self
.
206 207 208 |
# File 'lib/http/headers.rb', line 206 def merge!(other) self.class.coerce(other).to_h.each { |name, values| set name, values } end |
#set(name, value) ⇒ void Also known as: []=
This method returns an undefined value.
Sets header.
56 57 58 59 |
# File 'lib/http/headers.rb', line 56 def set(name, value) delete(name) add(name, value) end |
#to_a ⇒ Array<[String, String]>
Returns headers key/value pairs.
142 143 144 |
# File 'lib/http/headers.rb', line 142 def to_a @pile.map { |item| item[1..2] } end |
#to_h ⇒ Hash Also known as: to_hash
Returns Rack-compatible headers Hash
134 135 136 |
# File 'lib/http/headers.rb', line 134 def to_h keys.to_h { |k| [k, self[k]] } end |