Class: Protocol::HTTP::Headers
- Inherits:
-
Object
- Object
- Protocol::HTTP::Headers
- Defined in:
- lib/protocol/http/headers.rb
Overview
Headers are an array of key-value pairs. Some header keys represent multiple values.
Defined Under Namespace
Classes: Merged
Constant Summary collapse
- Split =
Header::Split
- Multiple =
Header::Multiple
- TRAILER =
"trailer"- POLICY =
The policy for various headers, including how they are merged and normalized.
{ # Headers which may only be specified once: "content-disposition" => false, "content-length" => false, "content-type" => false, "expect" => false, "from" => false, "host" => false, "location" => false, "max-forwards" => false, "range" => false, "referer" => false, "retry-after" => false, "server" => false, "transfer-encoding" => Header::TransferEncoding, "user-agent" => false, "trailer" => Header::Trailer, # Custom headers: "connection" => Header::Connection, "cache-control" => Header::CacheControl, "te" => Header::TE, "vary" => Header::Vary, "priority" => Header::Priority, # Headers specifically for proxies: "via" => Split, "x-forwarded-for" => Split, # Authorization headers: "authorization" => Header::Authorization, "proxy-authorization" => Header::Authorization, # Cache validations: "etag" => Header::ETag, "if-match" => Header::ETags, "if-none-match" => Header::ETags, "if-range" => false, # Headers which may be specified multiple times, but which can't be concatenated: "www-authenticate" => Multiple, "proxy-authenticate" => Multiple, # Custom headers: "set-cookie" => Header::SetCookie, "cookie" => Header::Cookie, # Date headers: # These headers include a comma as part of the formatting so they can't be concatenated. "date" => Header::Date, "expires" => Header::Date, "last-modified" => Header::Date, "if-modified-since" => Header::Date, "if-unmodified-since" => Header::Date, # Accept headers: "accept" => Header::Accept, "accept-charset" => Header::AcceptCharset, "accept-encoding" => Header::AcceptEncoding, "accept-language" => Header::AcceptLanguage, # Performance headers: "server-timing" => Header::ServerTiming, # Content integrity headers: "digest" => Header::Digest, }.tap{|hash| hash.default = Split}
Instance Attribute Summary collapse
- #An array of `[key, value]` pairs.(arrayof`[key, value]`pairs.) ⇒ Object readonly
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
- #The index where trailers begin.(indexwheretrailers) ⇒ Object readonly
Class Method Summary collapse
-
.[](headers) ⇒ Headers
Construct an instance from a headers Array or Hash.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this object to another object.
-
#[](key) ⇒ Object
Get the value of the specified header key.
-
#add(key, value) ⇒ Object
(also: #[]=)
Add the specified header key value pair.
-
#clear ⇒ Object
Clear all headers.
-
#delete(key) ⇒ Object
Delete all header values for the given key, and return the merged value.
-
#each(&block) ⇒ Object
Enumerate all header keys and values.
- #empty? ⇒ Boolean
-
#extract(keys) ⇒ Object
Extract the specified keys from the headers.
-
#flatten ⇒ Object
Flatten trailer into the headers, returning a new instance of Headers.
-
#flatten! ⇒ Object
Flatten trailer into the headers, in-place.
-
#freeze ⇒ Object
Freeze the headers, and ensure the indexed hash is generated.
- #include?(key) ⇒ Boolean (also: #key?)
-
#initialize(fields = [], tail = nil, indexed: nil, policy: POLICY) ⇒ Headers
constructor
Initialize the headers with the specified fields.
-
#initialize_dup(other) ⇒ Object
Initialize a copy of the headers.
-
#inspect ⇒ Object
Inspect the headers.
- #keys ⇒ Object
-
#merge(headers) ⇒ Object
Merge the headers into a new instance of Headers.
-
#merge!(headers) ⇒ Object
Merge the headers into this instance.
-
#set(key, value) ⇒ Object
Set the specified header key to the specified value, replacing any existing header keys with the same name.
- #The policy for the headers.=(policy) ⇒ Object
- #to_a ⇒ Object
-
#to_h ⇒ Object
(also: #as_json)
Compute a hash table of headers, where the keys are normalized to lower case and the values are normalized according to the policy for that header.
-
#trailer(&block) ⇒ Object
Enumerate all headers in the trailer, if there are any.
-
#trailer!(&block) ⇒ Object
Record the current headers, and prepare to add trailers.
- #trailer? ⇒ Boolean
Constructor Details
#initialize(fields = [], tail = nil, indexed: nil, policy: POLICY) ⇒ Headers
Initialize the headers with the specified fields.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/protocol/http/headers.rb', line 73 def initialize(fields = [], tail = nil, indexed: nil, policy: POLICY) @fields = fields # Marks where trailer start in the @fields array: @tail = tail # The cached index of headers: @indexed = nil @policy = policy end |
Instance Attribute Details
#An array of `[key, value]` pairs.(arrayof`[key, value]`pairs.) ⇒ Object (readonly)
131 |
# File 'lib/protocol/http/headers.rb', line 131 attr :fields |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
131 132 133 |
# File 'lib/protocol/http/headers.rb', line 131 def fields @fields end |
#policy ⇒ Object
Returns the value of attribute policy.
86 87 88 |
# File 'lib/protocol/http/headers.rb', line 86 def policy @policy end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
134 135 136 |
# File 'lib/protocol/http/headers.rb', line 134 def tail @tail end |
#The index where trailers begin.(indexwheretrailers) ⇒ Object (readonly)
134 |
# File 'lib/protocol/http/headers.rb', line 134 attr :tail |
Class Method Details
.[](headers) ⇒ 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/protocol/http/headers.rb', line 47 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 |
Instance Method Details
#==(other) ⇒ Object
Compare this object to another object. May depend on the order of the fields.
432 433 434 435 436 437 438 439 440 441 |
# File 'lib/protocol/http/headers.rb', line 432 def == other case other when Hash to_h == other when Headers @fields == other.fields else @fields == other end end |
#[](key) ⇒ Object
Get the value of the specified header key.
399 400 401 |
# File 'lib/protocol/http/headers.rb', line 399 def [] key to_h[key] end |
#add(key, value) ⇒ Object Also known as: []=
Add the specified header key value pair.
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/protocol/http/headers.rb', line 230 def add(key, value) # The value MUST be a string, so we convert it to a string to prevent errors later on. value = value.to_s if @indexed merge_into(@indexed, key.downcase, value) end @fields << [key, value] end |
#clear ⇒ Object
Clear all headers.
109 110 111 112 113 |
# File 'lib/protocol/http/headers.rb', line 109 def clear @fields.clear @tail = nil @indexed = nil end |
#delete(key) ⇒ Object
Delete all header values for the given key, and return the merged value.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/protocol/http/headers.rb', line 340 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 |
#each(&block) ⇒ Object
Enumerate all header keys and values.
193 194 195 |
# File 'lib/protocol/http/headers.rb', line 193 def each(&block) @fields.each(&block) end |
#empty? ⇒ Boolean
184 185 186 |
# File 'lib/protocol/http/headers.rb', line 184 def empty? @fields.empty? end |
#extract(keys) ⇒ Object
Extract the specified keys from the headers.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/protocol/http/headers.rb', line 212 def extract(keys) deleted, @fields = @fields.partition do |field| keys.include?(field.first.downcase) end if @indexed keys.each do |key| @indexed.delete(key) end end return deleted end |
#flatten ⇒ Object
Flatten trailer into the headers, returning a new instance of Protocol::HTTP::Headers.
126 127 128 |
# File 'lib/protocol/http/headers.rb', line 126 def flatten self.dup.flatten! end |
#flatten! ⇒ Object
Flatten trailer into the headers, in-place.
116 117 118 119 120 121 122 123 |
# File 'lib/protocol/http/headers.rb', line 116 def flatten! if @tail self.delete(TRAILER) @tail = nil end return self end |
#freeze ⇒ Object
Freeze the headers, and ensure the indexed hash is generated.
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/protocol/http/headers.rb', line 171 def freeze return if frozen? # Ensure @indexed is generated: self.to_h @fields.freeze @indexed.freeze super end |
#include?(key) ⇒ Boolean Also known as: key?
198 199 200 |
# File 'lib/protocol/http/headers.rb', line 198 def include? key self[key] != nil end |
#initialize_dup(other) ⇒ Object
Initialize a copy of the headers.
101 102 103 104 105 106 |
# File 'lib/protocol/http/headers.rb', line 101 def initialize_dup(other) super @fields = @fields.dup @indexed = @indexed.dup end |
#inspect ⇒ Object
Inspect the headers.
425 426 427 |
# File 'lib/protocol/http/headers.rb', line 425 def inspect "#<#{self.class} #{@fields.inspect}>" end |
#keys ⇒ Object
205 206 207 |
# File 'lib/protocol/http/headers.rb', line 205 def keys self.to_h.keys end |
#merge(headers) ⇒ Object
Merge the headers into a new instance of Protocol::HTTP::Headers.
263 264 265 |
# File 'lib/protocol/http/headers.rb', line 263 def merge(headers) self.dup.merge!(headers) end |
#merge!(headers) ⇒ Object
Merge the headers into this instance.
254 255 256 257 258 259 260 |
# File 'lib/protocol/http/headers.rb', line 254 def merge!(headers) headers.each do |key, value| self.add(key, value) end return self end |
#set(key, value) ⇒ Object
Set the specified header key to the specified value, replacing any existing header keys with the same name.
247 248 249 250 251 |
# File 'lib/protocol/http/headers.rb', line 247 def set(key, value) # TODO This could be a bit more efficient: self.delete(key) self.add(key, value) end |
#The policy for the headers.=(policy) ⇒ Object
86 |
# File 'lib/protocol/http/headers.rb', line 86 attr :policy |
#to_a ⇒ Object
137 138 139 |
# File 'lib/protocol/http/headers.rb', line 137 def to_a @fields end |
#to_h ⇒ Object Also known as: as_json
Compute a hash table of headers, where the keys are normalized to lower case and the values are normalized according to the policy for that header.
406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/protocol/http/headers.rb', line 406 def to_h unless @indexed @indexed = {} @fields.each_with_index do |(key, value), index| trailer = (@tail && index >= @tail) merge_into(@indexed, key.downcase, value, trailer) end end return @indexed end |
#trailer(&block) ⇒ Object
Enumerate all headers in the trailer, if there are any.
162 163 164 165 166 167 168 |
# File 'lib/protocol/http/headers.rb', line 162 def trailer(&block) return to_enum(:trailer) unless block_given? if @tail @fields.drop(@tail).each(&block) end end |
#trailer!(&block) ⇒ Object
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.
155 156 157 158 159 |
# File 'lib/protocol/http/headers.rb', line 155 def trailer!(&block) @tail ||= @fields.size return trailer(&block) end |
#trailer? ⇒ Boolean
142 143 144 |
# File 'lib/protocol/http/headers.rb', line 142 def trailer? @tail != nil end |