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.
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/http/protocol/headers.rb', line 67
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.
85
86
87
|
# File 'lib/http/protocol/headers.rb', line 85
def fields
@fields
end
|
Class Method Details
.[](hash) ⇒ Object
63
64
65
|
# File 'lib/http/protocol/headers.rb', line 63
def self.[] hash
self.new(hash.to_a)
end
|
Instance Method Details
#==(other) ⇒ Object
212
213
214
215
216
217
218
|
# File 'lib/http/protocol/headers.rb', line 212
def == other
if other.is_a? Hash
to_h == other
else
@fields == other.fields
end
end
|
#[](key) ⇒ Object
200
201
202
|
# File 'lib/http/protocol/headers.rb', line 200
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.
142
143
144
145
146
|
# File 'lib/http/protocol/headers.rb', line 142
def []= key, value
merge_into(@indexed, key.downcase, value)
@fields << [key, value]
end
|
#add(key, value) ⇒ Object
123
124
125
|
# File 'lib/http/protocol/headers.rb', line 123
def add(key, value)
self[key] = value
end
|
#delete(key) ⇒ Object
Delete all headers with the given key, and return the merged value.
177
178
179
180
181
182
183
|
# File 'lib/http/protocol/headers.rb', line 177
def delete(key)
values, @fields = @fields.partition do |field|
field.first.downcase == key
end
return @indexed.delete(key)
end
|
#dup ⇒ Object
81
82
83
|
# File 'lib/http/protocol/headers.rb', line 81
def dup
self.class.new(@fields, @indexed)
end
|
#each(&block) ⇒ Object
99
100
101
|
# File 'lib/http/protocol/headers.rb', line 99
def each(&block)
@fields.each(&block)
end
|
#empty? ⇒ Boolean
95
96
97
|
# File 'lib/http/protocol/headers.rb', line 95
def empty?
@fields.empty?
end
|
#freeze ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/http/protocol/headers.rb', line 87
def freeze
return if frozen?
@indexed = to_h
super
end
|
#include?(key) ⇒ Boolean
103
104
105
|
# File 'lib/http/protocol/headers.rb', line 103
def include? key
self[key] != nil
end
|
#merge(headers) ⇒ Object
135
136
137
|
# File 'lib/http/protocol/headers.rb', line 135
def merge()
self.dup.merge!()
end
|
#merge!(headers) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/http/protocol/headers.rb', line 127
def merge!()
.each do |key, value|
self[key] = value
end
return self
end
|
#slice(keys) ⇒ Object
119
120
121
|
# File 'lib/http/protocol/headers.rb', line 119
def slice(keys)
self.dup.slice!(keys)
end
|
#slice!(keys) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/http/protocol/headers.rb', line 107
def slice!(keys)
values, @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
204
205
206
207
208
209
210
|
# File 'lib/http/protocol/headers.rb', line 204
def to_h
@fields.inject({}) do |hash, (key, value)|
merge_into(hash, key.downcase, value)
hash
end
end
|