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
TRAILERS =
'trailers'
POLICY =
{
	# Headers which may only be specified once.
	'content-type' => false,
	'content-disposition' => false,
	'content-length' => false,
	'user-agent' => false,
	'referer' => false,
	'host' => false,
	'authorization' => false,
	'proxy-authorization' => 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,
	
	# 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Headers.



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

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

Instance Attribute Details

#fieldsObject (readonly)

An array of ‘[key, value]` pairs.



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

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.



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

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



305
306
307
308
309
310
311
312
313
314
# File 'lib/protocol/http/headers.rb', line 305

def == other
	case other
	when Hash
		to_h == other
	when Headers
		@fields == other.fields
	else
		@fields == other
	end
end

#[](key) ⇒ Object



288
289
290
# File 'lib/protocol/http/headers.rb', line 288

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.

Parameters:

  • key (String)

    The header key.

  • value

    The header value.



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

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.

Parameters:

  • key (String)

    the header key.

  • value (String)

    the header value to assign.



175
176
177
# File 'lib/protocol/http/headers.rb', line 175

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

#clearObject



80
81
82
83
84
# File 'lib/protocol/http/headers.rb', line 80

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

#delete(key) ⇒ Object

Delete all headers with the given key, and return the merged value.



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

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



146
147
148
# File 'lib/protocol/http/headers.rb', line 146

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@fields.empty?
end

#extract(keys) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/protocol/http/headers.rb', line 158

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



96
97
98
# File 'lib/protocol/http/headers.rb', line 96

def flatten
	self.dup.flatten!
end

#flatten!Object

Flatten trailers into the headers.



87
88
89
90
91
92
93
94
# File 'lib/protocol/http/headers.rb', line 87

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

#freezeObject



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

def freeze
	return if frozen?
	
	# Ensure @indexed is generated:
	self.to_h
	
	@fields.freeze
	@indexed.freeze
	
	super
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/protocol/http/headers.rb', line 150

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

#initialize_dup(other) ⇒ Object



73
74
75
76
77
78
# File 'lib/protocol/http/headers.rb', line 73

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

#inspectObject



301
302
303
# File 'lib/protocol/http/headers.rb', line 301

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

#keysObject



154
155
156
# File 'lib/protocol/http/headers.rb', line 154

def keys
	self.to_h.keys
end

#merge(headers) ⇒ Object



196
197
198
# File 'lib/protocol/http/headers.rb', line 196

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

#merge!(headers) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/protocol/http/headers.rb', line 188

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.

Parameters:

  • key (String)

    the header key to replace.

  • value (String)

    the header value to assign.



182
183
184
185
186
# File 'lib/protocol/http/headers.rb', line 182

def set(key, value)
	# TODO This could be a bit more efficient:
	self.delete(key)
	self.add(key, value)
end

#to_hObject

A hash table of ‘policy.map(values)`



293
294
295
296
297
298
299
# File 'lib/protocol/http/headers.rb', line 293

def to_h
	@indexed ||= @fields.inject({}) do |hash, (key, value)|
		merge_into(hash, key.downcase, value)
		
		hash
	end
end

#trailers(&block) ⇒ Object

Enumerate all trailers, if there are any.



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

def trailers(&block)
	return to_enum(:trailers) unless block_given?
	
	if @tail
		@fields.drop(@tail).each(&block)
	end
end

#trailers!(&block) ⇒ Object

Record the current headers, and prepare to receive trailers.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/protocol/http/headers.rb', line 109

def trailers!(&block)
	return nil unless self.include?(TRAILERS)
	
	@tail ||= @fields.size
	
	return to_enum(:trailers!) unless block_given?
	
	if @tail
		@fields.drop(@tail).each(&block)
	end
end

#trailers?Boolean

Returns the trailers if there are any.

Returns:

  • (Boolean)

    the trailers if there are any.



104
105
106
# File 'lib/protocol/http/headers.rb', line 104

def trailers?
	@tail != nil
end