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-type" => false,
	"content-disposition" => false,
	"content-length" => false,
	"user-agent" => false,
	"referer" => false,
	"host" => false,
	"from" => false,
	"location" => false,
	"max-forwards" => false,
	"retry-after" => false,
	
	# Custom headers:
	"connection" => Header::Connection,
	"cache-control" => Header::CacheControl,
	"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,
	
	# 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,
}.tap{|hash| hash.default = Split}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = [], indexed = nil) ⇒ Headers

Initialize the headers with the specified fields.



66
67
68
69
70
71
72
# File 'lib/protocol/http/headers.rb', line 66

def initialize(fields = [], indexed = nil)
	@fields = fields
	@indexed = indexed
	
	# Marks where trailer start in the @fields array.
	@tail = nil
end

Instance Attribute Details

#An array of `[key, value]` pairs.(arrayof`[key, value]`pairs.) ⇒ Object (readonly)



107
# File 'lib/protocol/http/headers.rb', line 107

attr :fields

#fieldsObject (readonly)

Returns the value of attribute fields.



107
108
109
# File 'lib/protocol/http/headers.rb', line 107

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.

Returns:

  • (Headers)

    an instance of headers.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/protocol/http/headers.rb', line 40

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.



371
372
373
374
375
376
377
378
379
380
# File 'lib/protocol/http/headers.rb', line 371

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.



344
345
346
# File 'lib/protocol/http/headers.rb', line 344

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.



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

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.



198
199
200
# File 'lib/protocol/http/headers.rb', line 198

def add(key, value)
	self[key] = value
end

#clearObject

Clear all headers.



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

def clear
	@fields.clear
	@indexed = nil
	@tail = nil
end

#delete(key) ⇒ Object

Delete all header values for the given key, and return the merged value.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/protocol/http/headers.rb', line 298

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.



161
162
163
# File 'lib/protocol/http/headers.rb', line 161

def each(&block)
	@fields.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/protocol/http/headers.rb', line 152

def empty?
	@fields.empty?
end

#extract(keys) ⇒ Object

Extract the specified keys from the headers.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/protocol/http/headers.rb', line 180

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.



102
103
104
# File 'lib/protocol/http/headers.rb', line 102

def flatten
	self.dup.flatten!
end

#flatten!Object

Flatten trailer into the headers, in-place.



92
93
94
95
96
97
98
99
# File 'lib/protocol/http/headers.rb', line 92

def flatten!
	if @tail
		self.delete(TRAILER)
		@tail = nil
	end
	
	return self
end

#freezeObject

Freeze the headers, and ensure the indexed hash is generated.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/protocol/http/headers.rb', line 139

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)


166
167
168
# File 'lib/protocol/http/headers.rb', line 166

def include? key
	self[key] != nil
end

#initialize_dup(other) ⇒ Object

Initialize a copy of the headers.



77
78
79
80
81
82
# File 'lib/protocol/http/headers.rb', line 77

def initialize_dup(other)
	super
	
	@fields = @fields.dup
	@indexed = @indexed.dup
end

#inspectObject

Inspect the headers.



364
365
366
# File 'lib/protocol/http/headers.rb', line 364

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

#keysObject



173
174
175
# File 'lib/protocol/http/headers.rb', line 173

def keys
	self.to_h.keys
end

#merge(headers) ⇒ Object

Merge the headers into a new instance of Protocol::HTTP::Headers.



222
223
224
# File 'lib/protocol/http/headers.rb', line 222

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

#merge!(headers) ⇒ Object

Merge the headers into this instance.



213
214
215
216
217
218
219
# File 'lib/protocol/http/headers.rb', line 213

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.



206
207
208
209
210
# File 'lib/protocol/http/headers.rb', line 206

def set(key, value)
	# TODO This could be a bit more efficient:
	self.delete(key)
	self.add(key, value)
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.



351
352
353
354
355
356
357
# File 'lib/protocol/http/headers.rb', line 351

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.



130
131
132
133
134
135
136
# File 'lib/protocol/http/headers.rb', line 130

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.



123
124
125
126
127
# File 'lib/protocol/http/headers.rb', line 123

def trailer!(&block)
	@tail ||= @fields.size
	
	return trailer(&block)
end

#trailer?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/protocol/http/headers.rb', line 110

def trailer?
	@tail != nil
end