Class: Protocol::HTTP::Headers

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#fieldsObject (readonly)

Returns the value of attribute fields.



131
132
133
# File 'lib/protocol/http/headers.rb', line 131

def fields
  @fields
end

#policyObject

Returns the value of attribute policy.



86
87
88
# File 'lib/protocol/http/headers.rb', line 86

def policy
  @policy
end

#tailObject (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.

Returns:

  • (Headers)

    an instance of headers.



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.



465
466
467
468
469
470
471
472
473
474
# File 'lib/protocol/http/headers.rb', line 465

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.



281
282
283
# File 'lib/protocol/http/headers.rb', line 281

def [] key
	to_h[key]
end

#[]=(key, value) ⇒ Object

Set the specified header key to the specified value, replacing any existing values.

The value can be a String or a coercable value.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/protocol/http/headers.rb', line 255

def []=(key, value)
	key = key.downcase
	
	# Delete existing value if any:
	self.delete(key)
	
	if policy = @policy[key]
		unless value.is_a?(policy)
			value = policy.coerce(value)
		end
	else
		value = value.to_s
	end
	
	# Clear the indexed cache so it will be rebuilt with parsed values when accessed:
	if @indexed
		@indexed[key] = value
	end
	
	@fields << [key, value.to_s]
end

#add(key, value) ⇒ Object

Add the specified header key value pair.



230
231
232
233
234
235
236
237
238
# File 'lib/protocol/http/headers.rb', line 230

def add(key, value)
	value = value.to_s
	
	if @indexed
		merge_into(@indexed, key.downcase, value)
	end
	
	@fields << [key, value]
end

#clearObject

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.



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/protocol/http/headers.rb', line 372

def delete(key)
	# If we've indexed the headers, we can bail out early if the key is not present:
	if @indexed && !@indexed.key?(key.downcase)
		return nil
	end
	
	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.parse(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

Returns:

  • (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

#flattenObject

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

#freezeObject

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?

Returns:

  • (Boolean)


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

#inspectObject

Inspect the headers.



458
459
460
# File 'lib/protocol/http/headers.rb', line 458

def inspect
	"#<#{self.class} #{@fields.inspect}>"
end

#keysObject



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.



295
296
297
# File 'lib/protocol/http/headers.rb', line 295

def merge(headers)
	self.dup.merge!(headers)
end

#merge!(headers) ⇒ Object

Merge the headers into this instance.



286
287
288
289
290
291
292
# File 'lib/protocol/http/headers.rb', line 286

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.



244
245
246
247
# File 'lib/protocol/http/headers.rb', line 244

def set(key, value)
	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_aObject



137
138
139
# File 'lib/protocol/http/headers.rb', line 137

def to_a
	@fields
end

#to_hObject 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.



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/protocol/http/headers.rb', line 439

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

Returns:

  • (Boolean)


142
143
144
# File 'lib/protocol/http/headers.rb', line 142

def trailer?
	@tail != nil
end