-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathheaders.cr
359 lines (300 loc) · 7.51 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# 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(hasher)
name.each_byte do |c|
hasher = normalize_byte(c).hash(hasher)
end
hasher
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
# We keep a Hash with String | Array(String) values because
# the most common case is a single value and so we avoid allocating
# memory for arrays.
@hash = Hash(Key, String | Array(String)).new
end
def []=(key, value : String)
check_invalid_header_content(value)
@hash[wrap(key)] = value
end
def []=(key, value : Array(String))
value.each { |val| check_invalid_header_content val }
@hash[wrap(key)] = value
end
def [](key)
values = @hash[wrap(key)]
concat values
end
def []?(key)
fetch(key, 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).
#
# ```
# require "http/headers"
#
# headers = HTTP::Headers{"Connection" => "keep-alive, Upgrade"}
# headers.includes_word?("Connection", "Upgrade") # => true
# ```
def includes_word?(key, word)
return false if word.empty?
values = @hash[wrap(key)]?
case values
when Nil
false
when String
includes_word_in_header_value?(word.downcase, values.downcase)
else
word = word.downcase
values.any? do |value|
includes_word_in_header_value?(word, value.downcase)
end
end
end
private def includes_word_in_header_value?(word, value)
offset = 0
while true
start = value.index(word, offset)
return false 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
false
end
def add(key, value : String)
check_invalid_header_content value
unsafe_add(key, value)
self
end
def add(key, value : Array(String))
value.each { |val| check_invalid_header_content val }
unsafe_add(key, value)
self
end
def add?(key, value : String)
return false unless valid_value?(value)
unsafe_add(key, value)
true
end
def add?(key, value : Array(String))
value.each { |val| return false unless valid_value?(val) }
unsafe_add(key, value)
true
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)]?
case {value, this_value}
when {String, String}
return false unless value == this_value
when {Array, Array}
return false unless value == this_value
when {String, Array}
return false unless this_value.size == 1 && this_value[0] == value
when {Array, String}
return false unless value.size == 1 && value[0] == this_value
else
return false unless value.nil?
end
end
true
end
def each
@hash.each do |key, value|
yield({key.name, cast(value)})
end
end
def get(key)
cast @hash[wrap(key)]
end
def get?(key)
@hash[wrap(key)]?.try { |value| cast(value) }
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) : Nil
io << "HTTP::Headers{"
@hash.each_with_index do |(key, values), index|
io << ", " if index > 0
key.name.inspect(io)
io << " => "
case values
when Array
if values.size == 1
values.first.inspect(io)
else
values.inspect(io)
end
else
values.inspect(io)
end
end
io << '}'
end
def inspect(io : IO) : Nil
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
def valid_value?(value)
return invalid_value_char(value).nil?
end
forward_missing_to @hash
private def unsafe_add(key, value : String)
key = wrap(key)
existing = @hash[key]?
if existing
if existing.is_a?(Array)
existing << value
else
@hash[key] = [existing, value]
end
else
@hash[key] = value
end
end
private def unsafe_add(key, value : Array(String))
key = wrap(key)
existing = @hash[key]?
if existing
if existing.is_a?(Array)
existing.concat value
else
new_value = [existing]
new_value.concat(value)
@hash[key] = new_value
end
else
@hash[key] = value
end
end
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 : String)
values
end
private def concat(values : Array(String))
case values.size
when 0
""
when 1
values.first
else
values.join ","
end
end
private def check_invalid_header_content(value)
if char = invalid_value_char(value)
raise ArgumentError.new("Header content contains invalid character #{char.inspect}")
end
end
private def valid_char?(char)
# 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.)
return true if char == '\t'
if char < ' ' || char > '\u{ff}' || char == '\u{7f}'
return false
end
true
end
private def invalid_value_char(value)
value.each_byte do |byte|
unless valid_char?(char = byte.unsafe_chr)
return char
end
end
end
end