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 = nil, indexed = nil) ⇒ Headers
Returns a new instance of Headers.
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/http/protocol/headers.rb', line 68
def initialize(fields = nil, indexed = nil)
if fields
@fields = fields.dup
else
@fields = []
end
if indexed
@indexed = indexed.dup
else
@indexed = self.to_h
end
end
|
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
86
87
88
|
# File 'lib/http/protocol/headers.rb', line 86
def fields
@fields
end
|
Class Method Details
.[](hash) ⇒ Object
64
65
66
|
# File 'lib/http/protocol/headers.rb', line 64
def self.[] hash
self.new(hash.to_a)
end
|
Instance Method Details
#==(other) ⇒ Object
213
214
215
216
217
218
219
|
# File 'lib/http/protocol/headers.rb', line 213
def == other
if other.is_a? Hash
to_h == other
else
@fields == other.fields
end
end
|
#[](key) ⇒ Object
201
202
203
|
# File 'lib/http/protocol/headers.rb', line 201
def [] key
@indexed[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.
143
144
145
146
147
|
# File 'lib/http/protocol/headers.rb', line 143
def []= key, value
merge_into(@indexed, key.downcase, value)
@fields << [key, value]
end
|
#add(key, value) ⇒ Object
124
125
126
|
# File 'lib/http/protocol/headers.rb', line 124
def add(key, value)
self[key] = value
end
|
#delete(key) ⇒ Object
Delete all headers with the given key, and return the merged value.
178
179
180
181
182
183
184
|
# File 'lib/http/protocol/headers.rb', line 178
def delete(key)
_, @fields = @fields.partition do |field|
field.first.downcase == key
end
return @indexed.delete(key)
end
|
#dup ⇒ Object
82
83
84
|
# File 'lib/http/protocol/headers.rb', line 82
def dup
self.class.new(@fields, @indexed)
end
|
#each(&block) ⇒ Object
100
101
102
|
# File 'lib/http/protocol/headers.rb', line 100
def each(&block)
@fields.each(&block)
end
|
#empty? ⇒ Boolean
96
97
98
|
# File 'lib/http/protocol/headers.rb', line 96
def empty?
@fields.empty?
end
|
#freeze ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/http/protocol/headers.rb', line 88
def freeze
return if frozen?
@indexed = to_h
super
end
|
#include?(key) ⇒ Boolean
104
105
106
|
# File 'lib/http/protocol/headers.rb', line 104
def include? key
self[key] != nil
end
|
#merge(headers) ⇒ Object
136
137
138
|
# File 'lib/http/protocol/headers.rb', line 136
def merge()
self.dup.merge!()
end
|
#merge!(headers) ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/http/protocol/headers.rb', line 128
def merge!()
.each do |key, value|
self[key] = value
end
return self
end
|
#slice(keys) ⇒ Object
120
121
122
|
# File 'lib/http/protocol/headers.rb', line 120
def slice(keys)
self.dup.slice!(keys)
end
|
#slice!(keys) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/http/protocol/headers.rb', line 108
def slice!(keys)
_, @fields = @fields.partition do |field|
keys.include?(field.first.downcase)
end
keys.each do |key|
@indexed.delete(key)
end
return self
end
|
#to_h ⇒ Object
205
206
207
208
209
210
211
|
# File 'lib/http/protocol/headers.rb', line 205
def to_h
@fields.inject({}) do |hash, (key, value)|
merge_into(hash, key.downcase, value)
hash
end
end
|