Class: Protocol::HTTP::Headers
- Inherits:
-
Object
- Object
- Protocol::HTTP::Headers
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, Multiple, Split
Constant Summary
collapse
- MERGE_POLICY =
{
'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,
'connection' => Split,
'via' => Split,
'x-forwarded-for' => Split,
'set-cookie' => Multiple,
'www-authenticate' => Multiple,
'proxy-authenticate' => Multiple
}.tap{|hash| hash.default = Split}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(fields = nil, indexed = nil) ⇒ Headers
Returns a new instance of Headers.
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/protocol/http/headers.rb', line 59
def initialize(fields = nil, indexed = nil)
if fields
@fields = fields.dup
else
@fields = []
end
if indexed
@indexed = indexed.dup
else
@indexed = nil
end
end
|
Instance Attribute Details
#fields ⇒ Object
An array of ‘[key, value]` pairs.
78
79
80
|
# File 'lib/protocol/http/headers.rb', line 78
def fields
@fields
end
|
Class Method Details
.[](hash) ⇒ Object
55
56
57
|
# File 'lib/protocol/http/headers.rb', line 55
def self.[] hash
self.new(hash.to_a)
end
|
Instance Method Details
#==(other) ⇒ Object
224
225
226
227
228
229
230
|
# File 'lib/protocol/http/headers.rb', line 224
def == other
if other.is_a? Hash
to_h == other
else
@fields == other.fields
end
end
|
#[](key) ⇒ Object
207
208
209
|
# File 'lib/protocol/http/headers.rb', line 207
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.
137
138
139
140
141
142
143
|
# File 'lib/protocol/http/headers.rb', line 137
def []= key, value
if @indexed
merge_into(@indexed, key.downcase, value)
end
@fields << [key, value]
end
|
#add(key, value) ⇒ Object
118
119
120
|
# File 'lib/protocol/http/headers.rb', line 118
def add(key, value)
self[key] = value
end
|
#delete(key) ⇒ Object
Delete all headers with the given key, and return the merged value.
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/protocol/http/headers.rb', line 174
def delete(key)
deleted, @fields = @fields.partition do |field|
field.first.downcase == key
end
if @indexed
return @indexed.delete(key)
elsif policy = MERGE_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
|
#dup ⇒ Object
73
74
75
|
# File 'lib/protocol/http/headers.rb', line 73
def dup
self.class.new(@fields, @indexed)
end
|
#each(&block) ⇒ Object
93
94
95
|
# File 'lib/protocol/http/headers.rb', line 93
def each(&block)
@fields.each(&block)
end
|
#empty? ⇒ Boolean
89
90
91
|
# File 'lib/protocol/http/headers.rb', line 89
def empty?
@fields.empty?
end
|
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/protocol/http/headers.rb', line 101
def (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
|
#freeze ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/protocol/http/headers.rb', line 80
def freeze
return if frozen?
self.to_h
super
end
|
#include?(key) ⇒ Boolean
97
98
99
|
# File 'lib/protocol/http/headers.rb', line 97
def include? key
self[key] != nil
end
|
#inspect ⇒ Object
220
221
222
|
# File 'lib/protocol/http/headers.rb', line 220
def inspect
"#<#{self.class} #{@fields.inspect}>"
end
|
#merge(headers) ⇒ Object
130
131
132
|
# File 'lib/protocol/http/headers.rb', line 130
def merge()
self.dup.merge!()
end
|
#merge!(headers) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/protocol/http/headers.rb', line 122
def merge!()
.each do |key, value|
self[key] = value
end
return self
end
|
#to_h ⇒ Object
A hash table of ‘policy.map(values)`
212
213
214
215
216
217
218
|
# File 'lib/protocol/http/headers.rb', line 212
def to_h
@indexed ||= @fields.inject({}) do |hash, (key, value)|
merge_into(hash, key.downcase, value)
hash
end
end
|