/
headers.cr
296 lines (244 loc) · 5.92 KB
/
headers.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# A `Hash`-like object that holds HTTP headers.
#
# Two headers are considered the same if their downcase representation is the same
# (in which `_` is the downcase version of `-`).
struct HTTP::Headers
include Enumerable({String, Array(String)})
# :nodoc:
record Key, name : String do
forward_missing_to @name
def hash
h = 0
name.each_byte do |c|
c = normalize_byte(c)
h = 31 * h + c
end
h
end
def ==(key2)
key1 = name
key2 = key2.name
return false if key1.bytesize != key2.bytesize
cstr1 = key1.to_unsafe
cstr2 = key2.to_unsafe
key1.bytesize.times do |i|
next if cstr1[i] == cstr2[i] # Optimize the common case
byte1 = normalize_byte(cstr1[i])
byte2 = normalize_byte(cstr2[i])
return false if byte1 != byte2
end
true
end
private def normalize_byte(byte)
char = byte.unsafe_chr
return byte if char.ascii_lowercase? || char == '-' # Optimize the common case
return byte + 32 if char.ascii_uppercase?
return '-'.ord if char == '_'
byte
end
end
def initialize
@hash = Hash(Key, Array(String)).new
end
def []=(key, value : String)
self[wrap(key)] = [value]
end
def []=(key, value : Array(String))
value.each { |val| check_invalid_header_content val }
@hash[wrap(key)] = value
end
def [](key)
fetch wrap(key)
end
def []?(key)
values = @hash[wrap(key)]?
values ? concat(values) : nil
end
# Returns if among the headers for *key* there is some that contains *word* as a value.
# The *word* is expected to match between word boundaries (i.e. non-alphanumeric chars).
#
# ```
# headers = HTTP::Headers{"Connection" => "keep-alive, Upgrade"}
# headers.includes_word?("Connection", "Upgrade") # => true
# ```
def includes_word?(key, word)
return false if word.empty?
word = word.downcase
# iterates over all header values avoiding the concatenation
get?(key).try &.each do |value|
value = value.downcase
offset = 0
while true
start = value.index(word, offset)
break unless start
offset = start + word.size
# check if the match is not surrounded by alphanumeric chars
next if start > 0 && value[start - 1].ascii_alphanumeric?
next if start + word.size < value.size && value[start + word.size].ascii_alphanumeric?
return true
end
end
false
end
def add(key, value : String)
check_invalid_header_content value
key = wrap(key)
existing = @hash[key]?
if existing
existing << value
else
@hash[key] = [value]
end
self
end
def add(key, value : Array(String))
value.each { |val| check_invalid_header_content val }
key = wrap(key)
existing = @hash[key]?
if existing
existing.concat value
else
@hash[key] = value
end
self
end
def fetch(key)
values = @hash.fetch wrap(key)
concat values
end
def fetch(key, default)
fetch(wrap(key)) { default }
end
def fetch(key)
values = @hash[wrap(key)]?
values ? concat(values) : yield key
end
def has_key?(key)
@hash.has_key? wrap(key)
end
def empty?
@hash.empty?
end
def delete(key)
values = @hash.delete wrap(key)
values ? concat(values) : nil
end
def merge!(other)
other.each do |key, value|
self[wrap(key)] = value
end
self
end
def ==(other : self)
self == other.@hash
end
def ==(other : Hash)
return false unless @hash.size == other.size
other.each do |key, value|
this_value = @hash[wrap(key)]?
if this_value
case value
when String
return false unless this_value.size == 1 && this_value[0] == value
when Array(String)
return false unless this_value == value
else
false
end
else
return false unless value.nil?
end
end
true
end
def each
@hash.each do |key, value|
yield({key.name, value})
end
end
def get(key)
@hash[wrap(key)]
end
def get?(key)
@hash[wrap(key)]?
end
def dup
dup = HTTP::Headers.new
@hash.each do |key, value|
dup.@hash[key] = value
end
dup
end
def clone
dup
end
def same?(other : HTTP::Headers)
object_id == other.object_id
end
def to_s(io : IO)
io << "HTTP::Headers{"
@hash.each_with_index do |(key, values), index|
io << ", " if index > 0
key.name.inspect(io)
io << " => "
if values.size == 1
values.first.inspect(io)
else
values.inspect(io)
end
end
io << "}"
end
def inspect(io : IO)
to_s(io)
end
def pretty_print(pp)
pp.list("HTTP::Headers{", @hash.keys.sort_by(&.name), "}") do |key|
pp.group do
key.name.pretty_print(pp)
pp.text " =>"
pp.nest do
pp.breakable
values = get(key)
if values.size == 1
values.first.pretty_print(pp)
else
values.pretty_print(pp)
end
end
end
end
end
forward_missing_to @hash
private def wrap(key)
key.is_a?(Key) ? key : Key.new(key)
end
private def cast(value : String)
[value]
end
private def cast(value : Array(String))
value
end
private def concat(values)
case values.size
when 0
""
when 1
values.first
else
values.join ","
end
end
private def check_invalid_header_content(value)
# According to RFC 7230, characters accepted as HTTP header
# are '\t', ' ', all US-ASCII printable characters and
# range from '\x80' to '\xff' (but the last is obsoleted.)
value.each_byte do |byte|
char = byte.unsafe_chr
next if char == '\t'
if char < ' ' || char > '\u{ff}' || char == '\u{7f}'
raise ArgumentError.new("Header content contains invalid character #{char.inspect}")
end
end
end
end