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
|
import std/strutils
import std/tables
import monoucha/fromjs
import monoucha/javascript
import monoucha/jserror
import monoucha/quickjs
import monoucha/tojs
import types/opt
import types/url
import utils/twtstr
type
HeaderGuard* = enum
hgNone = "none"
hgImmutable = "immutable"
hgRequest = "request"
hgRequestNoCors = "request-no-cors"
hgResponse = "response"
Headers* = ref object
table: Table[string, seq[string]]
guard*: HeaderGuard
HeadersInitType = enum
hitSequence, hitTable
HeadersInit* = object
case t: HeadersInitType
of hitSequence:
s: seq[(string, string)]
of hitTable:
tab: Table[string, string]
jsDestructor(Headers)
func isForbiddenResponseHeaderName*(name: string): bool
iterator pairs*(this: Headers): (string, string) =
for k, vs in this.table:
if this.guard == hgResponse and k.isForbiddenResponseHeaderName():
continue
for v in vs:
yield (k, v)
iterator allPairs*(headers: Headers): (string, string) =
for k, vs in headers.table:
for v in vs:
yield (k, v)
proc fromJS(ctx: JSContext; val: JSValueConst; res: var HeadersInit):
Err[void] =
if JS_IsUndefined(val) or JS_IsNull(val):
return err()
var headers: Headers
if ctx.fromJS(val, headers).isSome:
res = HeadersInit(t: hitSequence, s: @[])
for k, v in headers.table:
for vv in v:
res.s.add((k, vv))
return ok()
if ctx.isSequence(val):
res = HeadersInit(t: hitSequence)
if ctx.fromJS(val, res.s).isSome:
return ok()
res = HeadersInit(t: hitTable)
?ctx.fromJS(val, res.tab)
return ok()
const TokenChars = {
'!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~'
} + AsciiAlphaNumeric
func isValidHeaderName*(s: string): bool =
return s.len > 0 and AllChars - TokenChars notin s
func isValidHeaderValue*(s: string): bool =
return s.len == 0 or s[0] notin {' ', '\t'} and s[^1] notin {' ', '\t'} and
'\n' notin s
func isForbiddenRequestHeader*(name, value: string): bool =
const ForbiddenNames = [
"Accept-Charset",
"Accept-Encoding",
"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Connection",
"Content-Length",
"Cookie",
"Cookie2",
"Date",
"DNT",
"Expect",
"Host",
"Keep-Alive",
"Origin",
"Referer",
"Set-Cookie",
"TE",
"Trailer",
"Transfer-Encoding",
"Upgrade",
"Via"
]
for x in ForbiddenNames:
if name.equalsIgnoreCase(x):
return true
if name.startsWithIgnoreCase("proxy-") or name.startsWithIgnoreCase("sec-"):
return true
if name.equalsIgnoreCase("X-HTTP-Method") or
name.equalsIgnoreCase("X-HTTP-Method-Override") or
name.equalsIgnoreCase("X-Method-Override"):
return true # meh
return false
func isForbiddenResponseHeaderName*(name: string): bool =
return name.equalsIgnoreCase("Set-Cookie") or
name.equalsIgnoreCase("Set-Cookie2")
proc validate(this: Headers; name, value: string): JSResult[bool] =
if not name.isValidHeaderName() or not value.isValidHeaderValue():
return errTypeError("Invalid header name or value")
if this.guard == hgImmutable:
return errTypeError("Tried to modify immutable Headers object")
if this.guard == hgRequest and isForbiddenRequestHeader(name, value):
return ok(false)
if this.guard == hgResponse and name.isForbiddenResponseHeaderName():
return ok(false)
return ok(true)
func isNoCorsSafelistedName(name: string): bool =
return name.equalsIgnoreCase("Accept") or
name.equalsIgnoreCase("Accept-Language") or
name.equalsIgnoreCase("Content-Language") or
name.equalsIgnoreCase("Content-Type")
const CorsUnsafeRequestByte = {
char(0x00)..char(0x08), char(0x10)..char(0x1F), '"', '(', ')', ':', '<', '>',
'?', '@', '[', '\\', ']', '{', '}', '\e'
}
func isNoCorsSafelisted(name, value: string): bool =
if value.len > 128:
return false
if name.equalsIgnoreCase("Accept"):
return CorsUnsafeRequestByte notin value
if name.equalsIgnoreCase("Accept-Language") or
name.equalsIgnoreCase("Content-Language"):
const Forbidden = AllChars - AsciiAlphaNumeric -
{' ', '*', ',', '-', '.', ';', '='}
return Forbidden notin value
if name.equalsIgnoreCase("Content-Type"):
return value.strip(chars = AsciiWhitespace).toLowerAscii() in [
"multipart/form-data",
"text/plain",
"application-x-www-form-urlencoded"
]
return false
func get0(this: Headers; name: string): string =
return this.table[name].join(", ")
proc get*(ctx: JSContext; this: Headers; name: string): JSValue {.jsfunc.} =
if not name.isValidHeaderName():
JS_ThrowTypeError(ctx, "Invalid header name")
return JS_EXCEPTION
let name = name.toHeaderCase()
if name notin this.table:
return JS_NULL
return ctx.toJS(this.get0(name))
proc removeRange(this: Headers) =
if this.guard == hgRequestNoCors:
#TODO do this case insensitively
this.table.del("Range") # privileged no-CORS request headers
this.table.del("range")
proc append(this: Headers; name, value: string): JSResult[void] {.jsfunc.} =
let value = value.strip(chars = HTTPWhitespace)
if not ?this.validate(name, value):
return ok()
let name = name.toHeaderCase()
if this.guard == hgRequestNoCors:
if name in this.table:
let tmp = this.get0(name) & ", " & value
if not name.isNoCorsSafelisted(tmp):
return ok()
if name in this.table:
this.table[name].add(value)
else:
this.table[name] = @[value]
this.removeRange()
ok()
proc delete(this: Headers; name: string): JSResult[void] {.jsfunc.} =
let name = name.toHeaderCase()
if not ?this.validate(name, "") or name notin this.table:
return ok()
if not name.isNoCorsSafelistedName() and not name.equalsIgnoreCase("Range"):
return ok()
this.table.del(name)
this.removeRange()
ok()
proc has(this: Headers; name: string): JSResult[bool] {.jsfunc.} =
if not name.isValidHeaderName():
return errTypeError("Invalid header name")
let name = name.toHeaderCase()
return ok(name in this.table)
proc set(this: Headers; name, value: string): JSResult[void] {.jsfunc.} =
let value = value.strip(chars = HTTPWhitespace)
if not ?this.validate(name, value):
return ok()
if this.guard == hgRequestNoCors and not name.isNoCorsSafelisted(value):
return ok()
this.table[name.toHeaderCase()] = @[value]
this.removeRange()
ok()
proc fill(headers: Headers; s: seq[(string, string)]): JSResult[void] =
for (k, v) in s:
?headers.append(k, v)
ok()
proc fill(headers: Headers; tab: Table[string, string]): JSResult[void] =
for k, v in tab:
?headers.append(k, v)
ok()
proc fill*(headers: Headers; init: HeadersInit): JSResult[void] =
case init.t
of hitSequence: return headers.fill(init.s)
of hitTable: return headers.fill(init.tab)
func newHeaders*(guard: HeaderGuard): Headers =
return Headers(guard: guard)
func newHeaders*(guard: HeaderGuard; table: openArray[(string, string)]):
Headers =
let headers = newHeaders(guard)
for (k, v) in table:
let k = k.toHeaderCase()
headers.table.withValue(k, vs):
vs[].add(v)
do:
headers.table[k] = @[v]
return headers
func newHeaders*(guard: HeaderGuard; table: Table[string, string]): Headers =
let headers = newHeaders(guard)
for k, v in table:
let k = k.toHeaderCase()
headers.table.withValue(k, vs):
vs[].add(v)
do:
headers.table[k] = @[v]
return headers
func newHeaders(obj = none(HeadersInit)): JSResult[Headers] {.jsctor.} =
let headers = Headers(guard: hgNone)
if obj.isSome:
?headers.fill(obj.get)
return ok(headers)
func clone*(headers: Headers): Headers =
return Headers(table: headers.table)
proc add*(headers: Headers; k: string; v: sink string) =
let k = k.toHeaderCase()
headers.table.withValue(k, p):
p[].add(v)
do:
headers.table[k] = @[v]
proc `[]=`*(headers: Headers; k: string; v: sink string) =
let k = k.toHeaderCase()
headers.table[k] = @[v]
func `[]`*(headers: Headers; k: string): var string =
let k = k.toHeaderCase()
return headers.table[k][0]
func contains*(headers: Headers; k: string): bool =
return k.toHeaderCase() in headers.table
func getOrDefault*(headers: Headers; k: string; default = ""): string =
let k = k.toHeaderCase()
headers.table.withValue(k, p):
return p[][0]
do:
return default
proc del*(headers: Headers; k: string) =
headers.table.del(k)
func getAllCommaSplit*(headers: Headers; k: string): seq[string] =
headers.table.withValue(k, p):
return p[].join(",").split(',')
return @[]
func getAllNoComma*(headers: Headers; k: string): seq[string] =
headers.table.withValue(k, p):
return p[]
return @[]
type CheckRefreshResult* = object
# n is timeout in millis. -1 => not found
n*: int
# url == nil => self
url*: URL
func parseRefresh*(s: string; baseURL: URL): CheckRefreshResult =
var i = s.skipBlanks(0)
let s0 = s.until(AllChars - AsciiDigit, i)
let x = parseUInt32(s0, allowSign = false)
if s0 != "":
if x.isNone and (i >= s.len or s[i] != '.'):
return CheckRefreshResult(n: -1)
var n = int(x.get(0) * 1000)
i = s.skipBlanks(i + s0.len)
if i < s.len and s[i] == '.':
inc i
let s1 = s.until(AllChars - AsciiDigit, i)
if s1 != "":
n += int(parseUInt32(s1, allowSign = false).get(0))
i = s.skipBlanks(i + s1.len)
elif s0 == "": # empty string or blanks
return CheckRefreshResult(n: -1)
if i >= s.len: # just reload this page
return CheckRefreshResult(n: n)
if s[i] notin {',', ';'}:
return CheckRefreshResult(n: -1)
i = s.skipBlanks(i + 1)
if s.toOpenArray(i, s.high).startsWithIgnoreCase("url="):
i = s.skipBlanks(i + "url=".len)
var q = false
if i < s.len and s[i] in {'"', '\''}:
q = true
inc i
var s2 = s.substr(i)
if q and s2.len > 0 and s[^1] in {'"', '\''}:
s2.setLen(s2.high)
let url = parseURL(s2, some(baseURL))
if url.isNone:
return CheckRefreshResult(n: -1)
return CheckRefreshResult(n: n, url: url.get)
proc addHeadersModule*(ctx: JSContext) =
ctx.registerType(Headers)
|