Class: HTTP::Protocol::Headers
- Inherits:
-
Object
- Object
- HTTP::Protocol::Headers
show all
- Defined in:
- lib/http/protocol/headers.rb
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 = []) ⇒ Headers
Returns a new instance of Headers.
60
61
62
63
|
# File 'lib/http/protocol/headers.rb', line 60
def initialize(fields = [])
@fields = fields
@indexed = to_h
end
|
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
65
66
67
|
# File 'lib/http/protocol/headers.rb', line 65
def fields
@fields
end
|
Class Method Details
.[](hash) ⇒ Object
56
57
58
|
# File 'lib/http/protocol/headers.rb', line 56
def self.[] hash
self.new(hash.to_a)
end
|
Instance Method Details
#==(other) ⇒ Object
184
185
186
187
188
189
190
|
# File 'lib/http/protocol/headers.rb', line 184
def == other
if other.is_a? Hash
to_h == other
else
@fields == other.fields
end
end
|
#[](key) ⇒ Object
170
171
172
173
174
|
# File 'lib/http/protocol/headers.rb', line 170
def [] key
@indexed ||= to_h
@indexed[key]
end
|
#[]=(key, value) ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/http/protocol/headers.rb', line 118
def []= key, value
@fields << [key, value]
if @indexed
merge(@indexed, key.downcase, value)
end
end
|
#add(key, value) ⇒ Object
114
115
116
|
# File 'lib/http/protocol/headers.rb', line 114
def add(key, value)
self[key] = value
end
|
#delete(key) ⇒ Object
Delete all headers with the given key, and return the value of the last one, if any.
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/http/protocol/headers.rb', line 88
def delete(key)
values, @fields = @fields.partition do |field|
field.first.downcase == key
end
if @indexed
@indexed.delete(key)
end
if field = values.last
return field.last
end
end
|
#each(&block) ⇒ Object
79
80
81
|
# File 'lib/http/protocol/headers.rb', line 79
def each(&block)
@fields.each(&block)
end
|
#empty? ⇒ Boolean
75
76
77
|
# File 'lib/http/protocol/headers.rb', line 75
def empty?
@fields.empty?
end
|
#freeze ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/http/protocol/headers.rb', line 67
def freeze
return if frozen?
@indexed = to_h
super
end
|
#include?(key) ⇒ Boolean
83
84
85
|
# File 'lib/http/protocol/headers.rb', line 83
def include? key
self[key] != nil
end
|
#merge(hash, key, value) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/http/protocol/headers.rb', line 155
def merge(hash, key, value)
if policy = MERGE_POLICY[key]
if current_value = hash[key]
current_value << value
else
hash[key] = policy.new(value)
end
else
raise ArgumentError, "Header #{key} can only be set once!" if hash.include?(key)
hash[key] = value
end
end
|
#slice!(keys) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/http/protocol/headers.rb', line 102
def slice!(keys)
values, @fields = @fields.partition do |field|
keys.include?(field.first.downcase)
end
if @indexed
keys.each do |key|
@indexed.delete(key)
end
end
end
|
#to_h ⇒ Object
176
177
178
179
180
181
182
|
# File 'lib/http/protocol/headers.rb', line 176
def to_h
@fields.inject({}) do |hash, (key, value)|
merge(hash, key.downcase, value)
hash
end
end
|