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 =
{ # Headers which may only be specified once. 'content-type' => false, 'content-disposition' => false, 'content-length' => false, 'user-agent' => false, 'referer' => false, 'host' => false, 'if-modified-since' => false, 'if-unmodified-since' => false, 'from' => false, 'location' => false, 'max-forwards' => false, # Custom headers: 'connection' => Header::Connection, 'cache-control' => Header::CacheControl, 'vary' => Header::Vary, # 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, # 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, }.tap{|hash| hash.default = Split}
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
An array of ‘[key, value]` pairs.
Class Method Summary collapse
-
.[](headers) ⇒ Headers
Construct an instance from a headers Array or Hash.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
Append the value to the given key.
-
#add(key, value) ⇒ Object
Add the specified header key value pair.
- #clear ⇒ Object
-
#delete(key) ⇒ Object
Delete all headers with the given key, and return the merged value.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #extract(keys) ⇒ Object
- #flatten ⇒ Object
-
#flatten! ⇒ Object
Flatten trailer into the headers.
- #freeze ⇒ Object
- #include?(key) ⇒ Boolean (also: #key?)
-
#initialize(fields = [], indexed = nil) ⇒ Headers
constructor
A new instance of Headers.
- #initialize_dup(other) ⇒ Object
- #inspect ⇒ Object
- #keys ⇒ Object
- #merge(headers) ⇒ Object
- #merge!(headers) ⇒ Object
-
#set(key, value) ⇒ Object
Set the specified header key to the specified value, replacing any existing header keys with the same name.
-
#to_h ⇒ Object
A hash table of ‘policy.map(values)`.
-
#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 = [], indexed = nil) ⇒ Headers
Returns a new instance of Headers.
49 50 51 52 53 54 55 |
# File 'lib/protocol/http/headers.rb', line 49 def initialize(fields = [], indexed = nil) @fields = fields @indexed = indexed # Marks where trailer start in the @fields array. @tail = nil end |
Instance Attribute Details
#fields ⇒ Object (readonly)
An array of ‘[key, value]` pairs.
85 86 87 |
# File 'lib/protocol/http/headers.rb', line 85 def fields @fields end |
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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/protocol/http/headers.rb', line 27 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
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/protocol/http/headers.rb', line 299 def == other case other when Hash to_h == other when Headers @fields == other.fields else @fields == other end end |
#[](key) ⇒ Object
282 283 284 |
# File 'lib/protocol/http/headers.rb', line 282 def [] key to_h[key] end |
#[]=(key, value) ⇒ Object
Append the value to the given key. Some values can be appended multiple times, others can only be set once.
195 196 197 198 199 200 201 |
# File 'lib/protocol/http/headers.rb', line 195 def []= key, value if @indexed merge_into(@indexed, key.downcase, value) end @fields << [key, value] end |
#add(key, value) ⇒ Object
Add the specified header key value pair.
167 168 169 |
# File 'lib/protocol/http/headers.rb', line 167 def add(key, value) self[key] = value end |
#clear ⇒ Object
64 65 66 67 68 |
# File 'lib/protocol/http/headers.rb', line 64 def clear @fields.clear @indexed = nil @tail = nil end |
#delete(key) ⇒ Object
Delete all headers with the given key, and return the merged value.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/protocol/http/headers.rb', line 245 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
135 136 137 |
# File 'lib/protocol/http/headers.rb', line 135 def each(&block) @fields.each(&block) end |
#empty? ⇒ Boolean
131 132 133 |
# File 'lib/protocol/http/headers.rb', line 131 def empty? @fields.empty? end |
#extract(keys) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/protocol/http/headers.rb', line 149 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
80 81 82 |
# File 'lib/protocol/http/headers.rb', line 80 def flatten self.dup.flatten! end |
#flatten! ⇒ Object
Flatten trailer into the headers.
71 72 73 74 75 76 77 78 |
# File 'lib/protocol/http/headers.rb', line 71 def flatten! if @tail self.delete(TRAILER) @tail = nil end return self end |
#freeze ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/protocol/http/headers.rb', line 119 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?
139 140 141 |
# File 'lib/protocol/http/headers.rb', line 139 def include? key self[key] != nil end |
#initialize_dup(other) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/protocol/http/headers.rb', line 57 def initialize_dup(other) super @fields = @fields.dup @indexed = @indexed.dup end |
#inspect ⇒ Object
295 296 297 |
# File 'lib/protocol/http/headers.rb', line 295 def inspect "#<#{self.class} #{@fields.inspect}>" end |
#keys ⇒ Object
145 146 147 |
# File 'lib/protocol/http/headers.rb', line 145 def keys self.to_h.keys end |
#merge(headers) ⇒ Object
188 189 190 |
# File 'lib/protocol/http/headers.rb', line 188 def merge(headers) self.dup.merge!(headers) end |
#merge!(headers) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/protocol/http/headers.rb', line 180 def merge!(headers) headers.each do |key, value| self[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.
174 175 176 177 178 |
# File 'lib/protocol/http/headers.rb', line 174 def set(key, value) # TODO This could be a bit more efficient: self.delete(key) self.add(key, value) end |
#to_h ⇒ Object
A hash table of ‘policy.map(values)`
287 288 289 290 291 292 293 |
# File 'lib/protocol/http/headers.rb', line 287 def to_h @indexed ||= @fields.inject({}) do |hash, (key, value)| merge_into(hash, key.downcase, value) hash end end |
#trailer(&block) ⇒ Object
Enumerate all headers in the trailer, if there are any.
111 112 113 114 115 116 117 |
# File 'lib/protocol/http/headers.rb', line 111 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.
104 105 106 107 108 |
# File 'lib/protocol/http/headers.rb', line 104 def trailer!(&block) @tail ||= @fields.size return trailer(&block) end |
#trailer? ⇒ Boolean
88 89 90 |
# File 'lib/protocol/http/headers.rb', line 88 def trailer? @tail != nil end |