@amritk/nish-x86_64-linux 0.42.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
|
@@ -0,0 +1,1458 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/net/static` — serves the files under one directory through
|
|
3
|
+
* `nish/net/http-server` (HTTP/1.1, HTTP/2 over TLS and cleartext, HTTP/3)
|
|
4
|
+
* or through `nish/net/handler` (HTTP/1.1), with a content type per
|
|
5
|
+
* extension, an ETag and a Last-Modified, a single byte range, and a path
|
|
6
|
+
* check that refuses to leave the directory.
|
|
7
|
+
*
|
|
8
|
+
* import { HANDLER_SENT } from "nish/net/handler"
|
|
9
|
+
* import { STATIC_NOT_FOUND, StaticFiles, StaticLimits } from "nish/net/static"
|
|
10
|
+
*
|
|
11
|
+
* const ASSETS: i32 = 0
|
|
12
|
+
* router.add("GET", "/assets/*")
|
|
13
|
+
* router.add("HEAD", "/assets/*")
|
|
14
|
+
* const files = new StaticFiles("./public", new StaticLimits())
|
|
15
|
+
* // per request:
|
|
16
|
+
* case ASSETS: {
|
|
17
|
+
* const sent: i32 = files.serve(server, r, r.paramStart(0), r.paramEnd(0))
|
|
18
|
+
* if (sent === STATIC_NOT_FOUND) server.empty(r, 404)
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* **The path is the program's span, decoded and checked.** `serve` takes a
|
|
22
|
+
* `[start, end)` span of the request's head, the rest of the path a `*`
|
|
23
|
+
* route gave, percent-decodes it strictly (`%00` and a bad escape are
|
|
24
|
+
* refused) and splits it on `/`. A segment that is empty (`a//b`), `.`, `..`
|
|
25
|
+
* or any other name that starts with a dot, or that holds a backslash or a
|
|
26
|
+
* control byte, refuses the whole path. What is left is joined under the
|
|
27
|
+
* root and resolved with `realpathSync`, and a result that is not the root's
|
|
28
|
+
* own real path or under it is refused too, so a symbolic link that leads
|
|
29
|
+
* out is not followed out. A refused path answers `STATIC_NOT_FOUND` with
|
|
30
|
+
* `refused` set; the program answers it, as it answers a missing file.
|
|
31
|
+
*
|
|
32
|
+
* **The cache follows the disk.** Each file of at most
|
|
33
|
+
* `StaticLimits.maxCachedBytes` is kept in one of `maxEntries` slots, keyed
|
|
34
|
+
* by its decoded path and its coding; every request stats the file again, and
|
|
35
|
+
* a slot whose modification time or size differs is read again before it is
|
|
36
|
+
* sent. A larger file is read from disk for each request, a chunk at a time.
|
|
37
|
+
* Nothing is cached by URL, and no response is cached.
|
|
38
|
+
*
|
|
39
|
+
* **A precompressed sibling is sent when it is asked for.** With
|
|
40
|
+
* `precompressed` on, a request whose `Accept-Encoding` offers `br` or `gzip`
|
|
41
|
+
* is sent `name.br` or `name.gz` when that file exists beside `name`, with a
|
|
42
|
+
* `Content-Encoding`, the base name's content type and an ETag of its own.
|
|
43
|
+
* Codings are weighed by their q-values; a tie goes to `br`, then `gzip`,
|
|
44
|
+
* then the file as it is. With it on, every file it sends, and every 304,
|
|
45
|
+
* says `Vary: Accept-Encoding`.
|
|
46
|
+
*
|
|
47
|
+
* **Validators and ranges.** The ETag is strong, `"<size>-<mtime µs>"` in
|
|
48
|
+
* hex, with `-br` or `-gz` for a coded variant. `If-None-Match` (weak
|
|
49
|
+
* comparison, or `*`) answers 304; without it, an `If-Modified-Since` in the
|
|
50
|
+
* IMF-fixdate form at or after the file's second answers 304, and another
|
|
51
|
+
* date form is ignored. One `bytes=` range answers 206 with its
|
|
52
|
+
* `Content-Range`; more than one sends the whole file; a malformed or
|
|
53
|
+
* unsatisfiable one answers 416. `If-Range` keeps the range only for an
|
|
54
|
+
* equal strong ETag, or the exact Last-Modified date of a file unchanged
|
|
55
|
+
* for a second or more. A range on a coded variant counts the coded bytes.
|
|
56
|
+
*
|
|
57
|
+
* **What it reaches.** `fs.read` for the files under the root and `clock`
|
|
58
|
+
* for their modification times; `--sandbox --allow fs.read=<root>` holds
|
|
59
|
+
* the reads to the directory. The record is `docs/security/static.md`.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
import { realpathSync, statMtimeSync } from "nish:fs"
|
|
63
|
+
import { copyBytes } from "nish/bytes"
|
|
64
|
+
import { HANDLER_SENT, HttpHandler, HttpRequest } from "nish/net/handler"
|
|
65
|
+
import { HTTP_DATE_SIZE, httpDateWrite, httpFieldBytes } from "nish/net/http-fields"
|
|
66
|
+
import { HttpServer, HttpServerRequest } from "nish/net/http-server"
|
|
67
|
+
import { tryPercentDecode } from "nish/net/url"
|
|
68
|
+
|
|
69
|
+
/** What `serve` answers when it sent nothing: the file is missing or not a regular file, or the path was refused (`refused`). */
|
|
70
|
+
export const STATIC_NOT_FOUND: i32 = -30
|
|
71
|
+
|
|
72
|
+
/** The coding of the file as it is on disk. */
|
|
73
|
+
const STATIC_IDENTITY: i32 = 0
|
|
74
|
+
/** The `name.br` sibling. */
|
|
75
|
+
const STATIC_BR: i32 = 1
|
|
76
|
+
/** The `name.gz` sibling. */
|
|
77
|
+
const STATIC_GZIP: i32 = 2
|
|
78
|
+
|
|
79
|
+
/** The largest file `serve` answers: a body length is an `i32`. */
|
|
80
|
+
const STATIC_MAX_FILE: i64 = 2147483647
|
|
81
|
+
|
|
82
|
+
/** How many bytes one streamed read takes. */
|
|
83
|
+
const STATIC_CHUNK: i32 = 65536
|
|
84
|
+
|
|
85
|
+
/** The most cache entries `StaticLimits.maxEntries` may ask for. */
|
|
86
|
+
const STATIC_MAX_ENTRIES: i32 = 4096
|
|
87
|
+
/** The longest path span `StaticLimits.maxPath` may ask for. */
|
|
88
|
+
const STATIC_MAX_PATH: i32 = 4096
|
|
89
|
+
|
|
90
|
+
/** What `StaticFiles` holds and reads. */
|
|
91
|
+
export class StaticLimits {
|
|
92
|
+
/** How many files the cache keeps at once. */
|
|
93
|
+
maxEntries: i32 = 32
|
|
94
|
+
/** The largest file the cache keeps, at most 2^30; a larger one is read from disk on each request. */
|
|
95
|
+
maxCachedBytes: i32 = 262144
|
|
96
|
+
/** The longest path span `serve` takes, before decoding; a longer one is refused. */
|
|
97
|
+
maxPath: i32 = 1024
|
|
98
|
+
/** Whether a `name.br` or `name.gz` beside `name` is sent when `Accept-Encoding` offers it. */
|
|
99
|
+
precompressed: boolean = true
|
|
100
|
+
/** Whether an empty path, or one that ends in `/`, serves `index.html` there. */
|
|
101
|
+
index: boolean = true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The extensions `staticContentType` knows, in small letters, each followed
|
|
106
|
+
* by a space, its content type and a `|`.
|
|
107
|
+
*/
|
|
108
|
+
const STATIC_TYPES: string =
|
|
109
|
+
"html text/html; charset=utf-8|" +
|
|
110
|
+
"htm text/html; charset=utf-8|" +
|
|
111
|
+
"css text/css; charset=utf-8|" +
|
|
112
|
+
"js text/javascript; charset=utf-8|" +
|
|
113
|
+
"mjs text/javascript; charset=utf-8|" +
|
|
114
|
+
"json application/json|" +
|
|
115
|
+
"map application/json|" +
|
|
116
|
+
"svg image/svg+xml|" +
|
|
117
|
+
"webp image/webp|" +
|
|
118
|
+
"woff2 font/woff2|" +
|
|
119
|
+
"woff font/woff|" +
|
|
120
|
+
"png image/png|" +
|
|
121
|
+
"jpg image/jpeg|" +
|
|
122
|
+
"jpeg image/jpeg|" +
|
|
123
|
+
"gif image/gif|" +
|
|
124
|
+
"ico image/x-icon|" +
|
|
125
|
+
"avif image/avif|" +
|
|
126
|
+
"txt text/plain; charset=utf-8|" +
|
|
127
|
+
"wasm application/wasm|" +
|
|
128
|
+
"xml application/xml|" +
|
|
129
|
+
"pdf application/pdf|" +
|
|
130
|
+
"mp4 video/mp4|"
|
|
131
|
+
|
|
132
|
+
/** `b` with an ASCII capital letter made small. */
|
|
133
|
+
const staticLower = (b: i32): i32 => (b >= 65 && b <= 90 ? b + 32 : b)
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* `a + b` worked in 64 bits and narrowed: the offsets and counts added here
|
|
137
|
+
* are spans of a request head, a path or a file under 2^31, so the sum
|
|
138
|
+
* fits; one that does not wraps to a value the caller's range test refuses.
|
|
139
|
+
*/
|
|
140
|
+
const staticAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
|
|
141
|
+
|
|
142
|
+
/** `a - b` worked in 64 bits and narrowed, for the same offsets as `staticAdd`. */
|
|
143
|
+
const staticSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Field `i`'s value start (`start`) or end in `r.head`, -1 for an absent field
|
|
147
|
+
* (`i` below 0), or the handler's `Err` for an index it gives no span for.
|
|
148
|
+
*/
|
|
149
|
+
const staticValue = (r: HttpRequest, i: i32, start: boolean): Result<i32, string> => {
|
|
150
|
+
if (i < 0) {
|
|
151
|
+
return Ok(toI32(-1))
|
|
152
|
+
}
|
|
153
|
+
return start ? r.tryValueStart(i) : r.tryValueEnd(i)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* `b[i]`, or -1 when `i` is outside `b`. Every caller compares the answer
|
|
158
|
+
* with a byte (a digit range, `,`, `.`, `/`, `"`, `=`, a space or an ETag
|
|
159
|
+
* byte), which -1 never is, so a read past the end fails its parse as the
|
|
160
|
+
* end of the input does.
|
|
161
|
+
*/
|
|
162
|
+
const staticByte = (b: u8[], i: i32): i32 => (i >= 0 && i < toI32(b.length) ? toI32(b[i]) : -1)
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* `a + b` for file offsets and sizes, worked in `u64`, whose wrapping sum
|
|
166
|
+
* has the bits of the signed one: they are below 2^31, so it never wraps.
|
|
167
|
+
*/
|
|
168
|
+
const staticAdd64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
|
|
169
|
+
|
|
170
|
+
/** `a - b` for the same offsets as `staticAdd64`, the same way. */
|
|
171
|
+
const staticSub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Writes `v` to `out[i]` and answers `i + 1`, the next write's index; -1,
|
|
175
|
+
* writing nothing, when `i` is outside `out`, so a chain of writes carries
|
|
176
|
+
* the -1 to its end, where every caller checks it.
|
|
177
|
+
*/
|
|
178
|
+
const staticPut = (out: u8[], i: i32, v: i32): i32 => {
|
|
179
|
+
if (i < 0 || i >= toI32(out.length)) {
|
|
180
|
+
return -1
|
|
181
|
+
}
|
|
182
|
+
out[i] = toU8(v)
|
|
183
|
+
return i + 1
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* `n` zeroed bytes, `n` at most 2^30: one `new Array` up to 65535, which a
|
|
188
|
+
* `u16` length proves, and pushes past it.
|
|
189
|
+
*/
|
|
190
|
+
const staticBytes = (n: i32): u8[] => {
|
|
191
|
+
if (n <= 65535) {
|
|
192
|
+
let size: i32 = 0
|
|
193
|
+
if (n > 0) {
|
|
194
|
+
size = n
|
|
195
|
+
}
|
|
196
|
+
return new Array<u8>(toU16(size))
|
|
197
|
+
}
|
|
198
|
+
const out: u8[] = new Array<u8>(65535)
|
|
199
|
+
for (let i: i32 = 65535; i < n; i++) {
|
|
200
|
+
out.push(0)
|
|
201
|
+
}
|
|
202
|
+
return out
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The power of two from 64 up that holds `size`, at most 2^30. */
|
|
206
|
+
const staticCap = (size: i32): i32 => {
|
|
207
|
+
let cap: i32 = 64
|
|
208
|
+
while (cap < size && cap < 1073741824) {
|
|
209
|
+
cap = staticAdd(cap, cap)
|
|
210
|
+
}
|
|
211
|
+
return cap
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Reads `size` bytes of `fd` from its start into `into`: false when a read fails or `into` is short. */
|
|
215
|
+
const staticReadAll = (fd: i32, into: u8[], size: i32): boolean => {
|
|
216
|
+
if (size > toI32(into.length)) {
|
|
217
|
+
return false
|
|
218
|
+
}
|
|
219
|
+
let got: i32 = 0
|
|
220
|
+
while (got < size) {
|
|
221
|
+
const n: i32 = fileReadAt(fd, into, got, staticSub(size, got), toI64(got))
|
|
222
|
+
if (n <= 0) {
|
|
223
|
+
return false
|
|
224
|
+
}
|
|
225
|
+
got = staticAdd(got, n)
|
|
226
|
+
}
|
|
227
|
+
return true
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The code unit of `s` at `i`, or -1 when `i` is outside `s`. */
|
|
231
|
+
const staticChar = (s: string, i: i32): i32 => (i >= 0 && i < toI32(s.length) ? toI32(s.charCodeAt(i)) : -1)
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The content type `serve` sends for a file name, by its extension (ASCII
|
|
235
|
+
* case is ignored); `application/octet-stream` for one the table lacks.
|
|
236
|
+
* @param name the file name, or a path ending in it
|
|
237
|
+
* @returns the `Content-Type` value
|
|
238
|
+
*/
|
|
239
|
+
export const staticContentType = (name: string): string => {
|
|
240
|
+
let dot: i32 = -1
|
|
241
|
+
for (let k: i32 = toI32(name.length) - 1; k >= 0 && dot < 0; k--) {
|
|
242
|
+
const c: i32 = toI32(name.charCodeAt(k))
|
|
243
|
+
if (c === 47) {
|
|
244
|
+
return "application/octet-stream"
|
|
245
|
+
}
|
|
246
|
+
if (c === 46) {
|
|
247
|
+
dot = k
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (dot < 0) {
|
|
251
|
+
return "application/octet-stream"
|
|
252
|
+
}
|
|
253
|
+
const length: i32 = toI32(name.length) - dot - 1
|
|
254
|
+
let at: i32 = 0
|
|
255
|
+
while (at < toI32(STATIC_TYPES.length)) {
|
|
256
|
+
let k: i32 = 0
|
|
257
|
+
// The name is shorter than the table, so `k` stays far below 2^31.
|
|
258
|
+
while (
|
|
259
|
+
k < length &&
|
|
260
|
+
k < 65536 &&
|
|
261
|
+
staticChar(STATIC_TYPES, staticAdd(at, k)) ===
|
|
262
|
+
staticLower(staticChar(name, staticAdd(dot, staticAdd(1, k))))
|
|
263
|
+
) {
|
|
264
|
+
k++
|
|
265
|
+
}
|
|
266
|
+
const space: i32 = toI32(STATIC_TYPES.indexOf(" ", at))
|
|
267
|
+
const bar: i32 = toI32(STATIC_TYPES.indexOf("|", space))
|
|
268
|
+
if (space < 0 || bar < 0) {
|
|
269
|
+
break
|
|
270
|
+
}
|
|
271
|
+
if (k === length && space === staticAdd(at, length)) {
|
|
272
|
+
return STATIC_TYPES.substring(staticAdd(space, 1), bar)
|
|
273
|
+
}
|
|
274
|
+
at = staticAdd(bar, 1)
|
|
275
|
+
}
|
|
276
|
+
return "application/octet-stream"
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Whether `bytes[start .. end)` is `text`, ASCII case ignored. */
|
|
280
|
+
const staticIs = (bytes: u8[], start: i32, end: i32, text: string): boolean => {
|
|
281
|
+
const size: i32 = toI32(text.length)
|
|
282
|
+
if (staticSub(end, start) !== size) {
|
|
283
|
+
return false
|
|
284
|
+
}
|
|
285
|
+
for (let k: i32 = 0; k < size; k++) {
|
|
286
|
+
const b: i32 = staticByte(bytes, staticAdd(start, k))
|
|
287
|
+
if (b < 0 || staticLower(b) !== staticLower(toI32(text.charCodeAt(k)))) {
|
|
288
|
+
return false
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return true
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Whether `b` is a space or a tab. */
|
|
295
|
+
const staticSpace = (b: i32): boolean => b === 32 || b === 9
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Writes `v` (0 or more) in base `radix` (10 or 16, lowercase) at
|
|
299
|
+
* `out[at ..)` and answers the index past it; -1, writing nothing, when `at`
|
|
300
|
+
* is -1 or the digits do not fit, which every caller checks. Every `v` a
|
|
301
|
+
* caller passes is 0 or more (a size `staticResolve` refused below 0, a
|
|
302
|
+
* range inside it, an mtime clamped at 0), so each remainder is a digit and
|
|
303
|
+
* the guard in the loop never skips a byte.
|
|
304
|
+
*/
|
|
305
|
+
const staticDigits = (out: u8[], at: i32, v: i64, radix: i64): i32 => {
|
|
306
|
+
// Any radix but 16 is 10.
|
|
307
|
+
const hex: boolean = radix === 16
|
|
308
|
+
let digits: i32 = 1
|
|
309
|
+
let rest: i64 = hex ? v / 16 : v / 10
|
|
310
|
+
// An i64 has at most 20 digits in base 10.
|
|
311
|
+
while (rest > 0 && digits < 32) {
|
|
312
|
+
digits++
|
|
313
|
+
rest = hex ? rest / 16 : rest / 10
|
|
314
|
+
}
|
|
315
|
+
if (at < 0 || staticAdd(at, digits) > toI32(out.length)) {
|
|
316
|
+
return -1
|
|
317
|
+
}
|
|
318
|
+
let x: i64 = v
|
|
319
|
+
for (let k: i32 = digits - 1; k >= 0; k--) {
|
|
320
|
+
const d: i32 = toI32(hex ? x % 16 : x % 10)
|
|
321
|
+
// The test above keeps `i` inside `out`, and a remainder is a digit;
|
|
322
|
+
// this repeats it where the prover reads it.
|
|
323
|
+
const i: i32 = staticAdd(at, k)
|
|
324
|
+
if (i >= 0 && i < toI32(out.length) && d >= 0 && d < 16) {
|
|
325
|
+
out[i] = toU8(d < 10 ? 48 + d : 87 + d)
|
|
326
|
+
}
|
|
327
|
+
x = hex ? x / 16 : x / 10
|
|
328
|
+
}
|
|
329
|
+
return staticAdd(at, digits)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** The two digits at `b[at ..)` as a number, or -1. */
|
|
333
|
+
const staticTwo = (b: u8[], at: i32): i32 => {
|
|
334
|
+
const hi: i32 = staticByte(b, at)
|
|
335
|
+
const lo: i32 = staticByte(b, staticAdd(at, 1))
|
|
336
|
+
if (hi < 48 || hi > 57 || lo < 48 || lo > 57) {
|
|
337
|
+
return -1
|
|
338
|
+
}
|
|
339
|
+
const ten: i32 = 10
|
|
340
|
+
return ten * (hi - 48) + (lo - 48)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The month names, three letters each from January. */
|
|
344
|
+
const STATIC_MONTHS: string = "JanFebMarAprMayJunJulAugSepOctNovDec"
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* The IMF-fixdate at `b[start .. end)` (`Sun, 06 Nov 1994 08:49:37 GMT`) as
|
|
348
|
+
* seconds since 1970, or -1 for anything else: the obsolete RFC 850 and
|
|
349
|
+
* asctime forms are not read.
|
|
350
|
+
*/
|
|
351
|
+
const staticDate = (b: u8[], start: i32, end: i32): i64 => {
|
|
352
|
+
if (
|
|
353
|
+
start < 0 ||
|
|
354
|
+
end > toI32(b.length) ||
|
|
355
|
+
staticSub(end, start) !== HTTP_DATE_SIZE ||
|
|
356
|
+
staticByte(b, staticAdd(start, 3)) !== 44 ||
|
|
357
|
+
staticByte(b, staticAdd(start, 4)) !== 32
|
|
358
|
+
) {
|
|
359
|
+
return -1
|
|
360
|
+
}
|
|
361
|
+
const at: i32 = staticAdd(start, 5)
|
|
362
|
+
const m0: i32 = staticByte(b, staticAdd(at, 3))
|
|
363
|
+
const m1: i32 = staticByte(b, staticAdd(at, 4))
|
|
364
|
+
const m2: i32 = staticByte(b, staticAdd(at, 5))
|
|
365
|
+
const day: i32 = staticTwo(b, at)
|
|
366
|
+
let month: i32 = -1
|
|
367
|
+
for (let m: i32 = 0; m < 12; m++) {
|
|
368
|
+
if (
|
|
369
|
+
m0 === staticChar(STATIC_MONTHS, 3 * m) &&
|
|
370
|
+
m1 === staticChar(STATIC_MONTHS, 3 * m + 1) &&
|
|
371
|
+
m2 === staticChar(STATIC_MONTHS, 3 * m + 2)
|
|
372
|
+
) {
|
|
373
|
+
month = m + 1
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const century: i32 = staticTwo(b, staticAdd(at, 7))
|
|
377
|
+
const years: i32 = staticTwo(b, staticAdd(at, 9))
|
|
378
|
+
const hour: i32 = staticTwo(b, staticAdd(at, 12))
|
|
379
|
+
const minute: i32 = staticTwo(b, staticAdd(at, 15))
|
|
380
|
+
const second: i32 = staticTwo(b, staticAdd(at, 18))
|
|
381
|
+
if (
|
|
382
|
+
day < 1 ||
|
|
383
|
+
day > 31 ||
|
|
384
|
+
month < 0 ||
|
|
385
|
+
month > 12 ||
|
|
386
|
+
century < 0 ||
|
|
387
|
+
century > 99 ||
|
|
388
|
+
years < 0 ||
|
|
389
|
+
years > 99 ||
|
|
390
|
+
hour < 0 ||
|
|
391
|
+
hour > 23 ||
|
|
392
|
+
minute < 0 ||
|
|
393
|
+
minute > 59 ||
|
|
394
|
+
second < 0 ||
|
|
395
|
+
second > 60 ||
|
|
396
|
+
!staticIs(b, staticAdd(at, 20), staticAdd(at, 24), " GMT") ||
|
|
397
|
+
staticByte(b, staticAdd(at, 2)) !== 32 ||
|
|
398
|
+
staticByte(b, staticAdd(at, 6)) !== 32 ||
|
|
399
|
+
staticByte(b, staticAdd(at, 11)) !== 32 ||
|
|
400
|
+
staticByte(b, staticAdd(at, 14)) !== 58 ||
|
|
401
|
+
staticByte(b, staticAdd(at, 17)) !== 58
|
|
402
|
+
) {
|
|
403
|
+
return -1
|
|
404
|
+
}
|
|
405
|
+
// The bounds again, alone, where the prover reads them for the sums below.
|
|
406
|
+
if (century < 0 || century > 99) {
|
|
407
|
+
return -1
|
|
408
|
+
}
|
|
409
|
+
if (years < 0 || years > 99) {
|
|
410
|
+
return -1
|
|
411
|
+
}
|
|
412
|
+
if (month < 1 || month > 12) {
|
|
413
|
+
return -1
|
|
414
|
+
}
|
|
415
|
+
if (day < 1 || day > 31) {
|
|
416
|
+
return -1
|
|
417
|
+
}
|
|
418
|
+
if (hour < 0 || hour > 23) {
|
|
419
|
+
return -1
|
|
420
|
+
}
|
|
421
|
+
if (minute < 0 || minute > 59) {
|
|
422
|
+
return -1
|
|
423
|
+
}
|
|
424
|
+
if (second < 0 || second > 60) {
|
|
425
|
+
return -1
|
|
426
|
+
}
|
|
427
|
+
// Days from 1970-01-01 to the civil date (Howard Hinnant's days_from_civil),
|
|
428
|
+
// with the year starting in March. For a year of 0 or more, the eras of
|
|
429
|
+
// 400 years add up to the leap days counted below, and so does 0000's
|
|
430
|
+
// January, the one year of -1; every term is small enough for an `i32`.
|
|
431
|
+
let year: i32 = 100 * century + years
|
|
432
|
+
let mp: i32 = month + 9
|
|
433
|
+
if (month > 2) {
|
|
434
|
+
mp = month - 3
|
|
435
|
+
} else {
|
|
436
|
+
year = year - 1
|
|
437
|
+
}
|
|
438
|
+
// The prover does not carry `year` through the branches, so `365 * year`
|
|
439
|
+
// is worked in `u64`, whose wrapping keeps the signed bits.
|
|
440
|
+
const y: i32 = year
|
|
441
|
+
const dayOfYear: i32 = (153 * mp + 2) / 5 + day - 1
|
|
442
|
+
const leaps: i32 = staticAdd(staticSub(y / 4, y / 100), y / 400)
|
|
443
|
+
const days: i32 = staticSub(
|
|
444
|
+
staticAdd(staticAdd(toI32(toI64(toU64(y) * toU64(365))), leaps), dayOfYear),
|
|
445
|
+
719468
|
|
446
|
+
)
|
|
447
|
+
// Worked in `u64`, whose wrapping has the bits of the signed result: `days`
|
|
448
|
+
// is within 2^22 of 0, so the seconds never wrap.
|
|
449
|
+
return toI64(toU64(days) * toU64(86400) + toU64(3600 * hour + 60 * minute + second))
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* The decimal digits at `b[start .. end)` as a number, or -1 for none, a
|
|
454
|
+
* byte that is not a digit, or more than 18 digits.
|
|
455
|
+
*/
|
|
456
|
+
const staticNumber = (b: u8[], start: i32, end: i32): i64 => {
|
|
457
|
+
if (start < 0 || end <= start || staticSub(end, start) > 18) {
|
|
458
|
+
return -1
|
|
459
|
+
}
|
|
460
|
+
let n: i64 = 0
|
|
461
|
+
for (let k: i32 = start; k < end && k < toI32(b.length); k++) {
|
|
462
|
+
const d: i32 = toI32(b[k]) - 48
|
|
463
|
+
if (d < 0 || d > 9) {
|
|
464
|
+
return -1
|
|
465
|
+
}
|
|
466
|
+
// Worked in `u64`, whose wrapping result has the bits of the signed
|
|
467
|
+
// one: eighteen digits are below 10^18, so it never wraps.
|
|
468
|
+
n = toI64(toU64(n) * toU64(10) + toU64(d))
|
|
469
|
+
}
|
|
470
|
+
return n
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* The q-value at `b[start .. end)` in thousandths (`0`, `1`, `0.5`,
|
|
475
|
+
* `1.000`), or -1 when it is not one.
|
|
476
|
+
*/
|
|
477
|
+
const staticQ = (b: u8[], start: i32, end: i32): i32 => {
|
|
478
|
+
if (end <= start || staticSub(end, start) > 5) {
|
|
479
|
+
return -1
|
|
480
|
+
}
|
|
481
|
+
const first: i32 = staticByte(b, start)
|
|
482
|
+
if (first < 48 || first > 49) {
|
|
483
|
+
return -1
|
|
484
|
+
}
|
|
485
|
+
const whole: i32 = first - 48
|
|
486
|
+
if (staticSub(end, start) === 1) {
|
|
487
|
+
return 1000 * whole
|
|
488
|
+
}
|
|
489
|
+
if (staticByte(b, staticAdd(start, 1)) !== 46) {
|
|
490
|
+
return -1
|
|
491
|
+
}
|
|
492
|
+
let q: i32 = 1000 * whole
|
|
493
|
+
let scale: i32 = 100
|
|
494
|
+
for (let k: i32 = staticAdd(start, 2); k >= 0 && k < end && k < toI32(b.length); k++) {
|
|
495
|
+
const d: i32 = toI32(b[k]) - 48
|
|
496
|
+
// Three digits at most, so `q` stays under 2000 and `scale` at most 100.
|
|
497
|
+
if (d < 0 || d > 9 || q < 0 || q > 2000 || scale < 0 || scale > 100) {
|
|
498
|
+
return -1
|
|
499
|
+
}
|
|
500
|
+
q += scale * d
|
|
501
|
+
scale = scale / 10
|
|
502
|
+
}
|
|
503
|
+
return q > 1000 ? -1 : q
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** A new array of `src[0 .. length)`. */
|
|
507
|
+
const staticCut = (src: u8[], length: i32): u8[] => {
|
|
508
|
+
const out: u8[] = []
|
|
509
|
+
for (let k: i32 = 0; k < length && k < toI32(src.length); k++) {
|
|
510
|
+
out.push(src[k])
|
|
511
|
+
}
|
|
512
|
+
return out
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Serves the files under one directory; one per server, not reentrant. */
|
|
516
|
+
export class StaticFiles {
|
|
517
|
+
/** The root's real path, or `""` when it did not resolve (then every request is not found). */
|
|
518
|
+
root: string
|
|
519
|
+
/** `root` with one trailing `/`: a resolved path under it starts with this. @internal */
|
|
520
|
+
under: string
|
|
521
|
+
/** A copy of the limits the constructor was given, within their ranges. */
|
|
522
|
+
private limits: StaticLimits
|
|
523
|
+
/** Whether the last `serve` that answered `STATIC_NOT_FOUND` refused the path rather than missing the file. */
|
|
524
|
+
refused: boolean = false
|
|
525
|
+
/** The status the last `serve` sent, or 0 when it sent none. */
|
|
526
|
+
status: i32 = 0
|
|
527
|
+
|
|
528
|
+
/** The decoded path, then the relative name it serves. @internal Written by `staticPlan`. */
|
|
529
|
+
path: u8[]
|
|
530
|
+
/** How many bytes of `path` are the name. @internal */
|
|
531
|
+
pathLength: i32 = 0
|
|
532
|
+
/** How many bytes of `etag` are the ETag; beside `pathLength`, so the two share one 8-byte slot. @internal */
|
|
533
|
+
etagLength: i32 = 0
|
|
534
|
+
/** The ETag the plan made, `etag[0 .. etagLength)`. @internal */
|
|
535
|
+
etag: u8[]
|
|
536
|
+
/** One streamed read. @internal */
|
|
537
|
+
chunk: u8[]
|
|
538
|
+
/** The open file the plan chose, or -1. @internal */
|
|
539
|
+
fd: i32 = -1
|
|
540
|
+
/** Its coding, a `STATIC_*` value. @internal */
|
|
541
|
+
coding: i32 = 0
|
|
542
|
+
/** Its size. @internal */
|
|
543
|
+
size: i64 = 0
|
|
544
|
+
/** Its modification time in milliseconds. @internal */
|
|
545
|
+
mtime: f64 = 0
|
|
546
|
+
/** The first byte sent and one past the last. @internal */
|
|
547
|
+
from: i64 = 0
|
|
548
|
+
/** @internal */
|
|
549
|
+
to: i64 = 0
|
|
550
|
+
/** The cache slot holding the bytes, or -1 to stream from `fd`. @internal */
|
|
551
|
+
slot: i32 = -1
|
|
552
|
+
/** The q-values `Accept-Encoding` gave `br`, `gzip` and the file as it is, in thousandths. @internal */
|
|
553
|
+
qBr: i32 = 0
|
|
554
|
+
/** @internal */
|
|
555
|
+
qGzip: i32 = 0
|
|
556
|
+
/** @internal */
|
|
557
|
+
qIdentity: i32 = 1000
|
|
558
|
+
|
|
559
|
+
/** The request's fields, as `[start, end)` spans of its head, -1 when absent. @internal Copied by each wrapper. */
|
|
560
|
+
acceptStart: i32 = -1
|
|
561
|
+
/** @internal */
|
|
562
|
+
acceptEnd: i32 = -1
|
|
563
|
+
/** @internal */
|
|
564
|
+
noneMatchStart: i32 = -1
|
|
565
|
+
/** @internal */
|
|
566
|
+
noneMatchEnd: i32 = -1
|
|
567
|
+
/** @internal */
|
|
568
|
+
sinceStart: i32 = -1
|
|
569
|
+
/** @internal */
|
|
570
|
+
sinceEnd: i32 = -1
|
|
571
|
+
/** @internal */
|
|
572
|
+
rangeStart: i32 = -1
|
|
573
|
+
/** @internal */
|
|
574
|
+
rangeEnd: i32 = -1
|
|
575
|
+
/** @internal */
|
|
576
|
+
ifRangeStart: i32 = -1
|
|
577
|
+
/** @internal */
|
|
578
|
+
ifRangeEnd: i32 = -1
|
|
579
|
+
|
|
580
|
+
/** The cache keys, one of `path.length` bytes per entry. @internal */
|
|
581
|
+
keys: u8[][]
|
|
582
|
+
/** How many bytes of each key are used, -1 for an empty slot. @internal */
|
|
583
|
+
keyLength: i32[]
|
|
584
|
+
/** Each key's coding. @internal */
|
|
585
|
+
keyCoding: i32[]
|
|
586
|
+
/** Each slot's bytes, `bytes[k][0 .. sizes[k])`. @internal */
|
|
587
|
+
bytes: u8[][]
|
|
588
|
+
/** @internal */
|
|
589
|
+
sizes: i64[]
|
|
590
|
+
/** @internal */
|
|
591
|
+
mtimes: f64[]
|
|
592
|
+
/** The next slot to replace. @internal */
|
|
593
|
+
hand: i32 = 0
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* A server of the files under `root`, which is resolved once, now. Limits
|
|
597
|
+
* out of range (`maxEntries` or `maxPath` outside 1 to 4096, or a
|
|
598
|
+
* `maxCachedBytes` outside 0 to 2^30) leave the root empty, so every GET or
|
|
599
|
+
* HEAD is not found.
|
|
600
|
+
* @param root the directory to serve
|
|
601
|
+
* @param limits the cache and path limits
|
|
602
|
+
*/
|
|
603
|
+
constructor(root: string, limits: StaticLimits) {
|
|
604
|
+
const ok: boolean =
|
|
605
|
+
limits.maxEntries >= 1 &&
|
|
606
|
+
limits.maxEntries <= STATIC_MAX_ENTRIES &&
|
|
607
|
+
limits.maxPath >= 1 &&
|
|
608
|
+
limits.maxPath <= STATIC_MAX_PATH &&
|
|
609
|
+
limits.maxCachedBytes >= 0 &&
|
|
610
|
+
limits.maxCachedBytes <= 1073741824
|
|
611
|
+
const real: string | null = ok ? realpathSync(root) : null
|
|
612
|
+
this.root = real === null ? "" : real
|
|
613
|
+
this.under = this.root.endsWith("/") ? this.root : this.root + "/"
|
|
614
|
+
// A copy, so a program that changes its limits later cannot outgrow the arrays sized here.
|
|
615
|
+
const own = new StaticLimits()
|
|
616
|
+
own.maxCachedBytes = ok ? limits.maxCachedBytes : 0
|
|
617
|
+
own.precompressed = limits.precompressed
|
|
618
|
+
own.index = limits.index
|
|
619
|
+
this.limits = own
|
|
620
|
+
let entries: i32 = 1
|
|
621
|
+
let room: i32 = 17
|
|
622
|
+
const asked: i32 = limits.maxEntries
|
|
623
|
+
const longest: i32 = limits.maxPath
|
|
624
|
+
// Literal bounds, so the prover sees that the sum cannot overflow and that
|
|
625
|
+
// each length below fits the `u16` an array length is proven by.
|
|
626
|
+
if (ok && asked <= 4096 && longest <= 4096 && asked >= 1 && longest >= 1) {
|
|
627
|
+
entries = asked
|
|
628
|
+
room = longest + 16
|
|
629
|
+
}
|
|
630
|
+
own.maxEntries = entries
|
|
631
|
+
own.maxPath = room - 16
|
|
632
|
+
const width: u16 = toU16(room)
|
|
633
|
+
const count: u16 = toU16(entries)
|
|
634
|
+
this.path = new Array<u8>(width)
|
|
635
|
+
this.etag = new Array<u8>(48)
|
|
636
|
+
this.chunk = new Array<u8>(STATIC_CHUNK)
|
|
637
|
+
this.keyCoding = new Array<i32>(count)
|
|
638
|
+
this.sizes = new Array<i64>(count)
|
|
639
|
+
this.mtimes = new Array<f64>(count)
|
|
640
|
+
const lengths: i32[] = []
|
|
641
|
+
const keys: u8[][] = []
|
|
642
|
+
const slots: u8[][] = []
|
|
643
|
+
for (let k: i32 = 0; k < entries; k++) {
|
|
644
|
+
lengths.push(-1)
|
|
645
|
+
keys.push(new Array<u8>(width))
|
|
646
|
+
slots.push(new Array<u8>(0))
|
|
647
|
+
}
|
|
648
|
+
this.keyLength = lengths
|
|
649
|
+
this.keys = keys
|
|
650
|
+
this.bytes = slots
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Serves the file `r`'s head names at `[pathStart, pathEnd)`, a path
|
|
655
|
+
* relative to the root (one leading `/` is dropped), through `server`.
|
|
656
|
+
* @param server the server `r` came from
|
|
657
|
+
* @param r a request taken and not yet answered
|
|
658
|
+
* @param pathStart where the path starts in `r.head`
|
|
659
|
+
* @param pathEnd one past its last byte
|
|
660
|
+
* @returns `HANDLER_SENT`, `HANDLER_DROPPED`, or `STATIC_NOT_FOUND` with nothing sent
|
|
661
|
+
*/
|
|
662
|
+
serve(server: HttpServer, r: HttpServerRequest, pathStart: i32, pathEnd: i32): i32 {
|
|
663
|
+
const ae: i32 = r.header("accept-encoding")
|
|
664
|
+
this.acceptStart = ae < 0 ? -1 : r.valueStart(ae)
|
|
665
|
+
this.acceptEnd = ae < 0 ? -1 : r.valueEnd(ae)
|
|
666
|
+
const nm: i32 = r.header("if-none-match")
|
|
667
|
+
this.noneMatchStart = nm < 0 ? -1 : r.valueStart(nm)
|
|
668
|
+
this.noneMatchEnd = nm < 0 ? -1 : r.valueEnd(nm)
|
|
669
|
+
const ims: i32 = r.header("if-modified-since")
|
|
670
|
+
this.sinceStart = ims < 0 ? -1 : r.valueStart(ims)
|
|
671
|
+
this.sinceEnd = ims < 0 ? -1 : r.valueEnd(ims)
|
|
672
|
+
const rg: i32 = r.header("range")
|
|
673
|
+
this.rangeStart = rg < 0 ? -1 : r.valueStart(rg)
|
|
674
|
+
this.rangeEnd = rg < 0 ? -1 : r.valueEnd(rg)
|
|
675
|
+
const ir: i32 = r.header("if-range")
|
|
676
|
+
this.ifRangeStart = ir < 0 ? -1 : r.valueStart(ir)
|
|
677
|
+
this.ifRangeEnd = ir < 0 ? -1 : r.valueEnd(ir)
|
|
678
|
+
const head: boolean = r.methodIs("HEAD")
|
|
679
|
+
const status: i32 = this.staticPlan(r.head, pathStart, pathEnd, head || r.methodIs("GET"), head)
|
|
680
|
+
if (status === STATIC_NOT_FOUND) {
|
|
681
|
+
return STATIC_NOT_FOUND
|
|
682
|
+
}
|
|
683
|
+
const names: u8[][] = []
|
|
684
|
+
const values: u8[][] = []
|
|
685
|
+
this.staticFields(status, names, values)
|
|
686
|
+
let sent: i32 = server.start(r, status, names, values, toI32(this.staticBodyLength(status)))
|
|
687
|
+
if (sent === HANDLER_SENT && !head && this.staticBodyLength(status) > 0) {
|
|
688
|
+
let pos: i64 = this.from
|
|
689
|
+
while (sent === HANDLER_SENT && pos < this.to) {
|
|
690
|
+
const n: i32 = this.staticFill(pos)
|
|
691
|
+
// `staticLoad` made `slot` an index of `bytes`, or -1.
|
|
692
|
+
const slot: i32 = this.slot
|
|
693
|
+
sent = server.write(
|
|
694
|
+
r,
|
|
695
|
+
slot >= 0 && slot < toI32(this.bytes.length) ? this.bytes[slot] : this.chunk,
|
|
696
|
+
slot >= 0 ? toI32(pos) : toI32(0),
|
|
697
|
+
n
|
|
698
|
+
)
|
|
699
|
+
pos = staticAdd64(pos, toI64(n))
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
this.staticClose()
|
|
703
|
+
return sent === HANDLER_SENT ? server.end(r) : sent
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* `serve` through `nish/net/handler`'s HTTP/1.1 carrier. Panics where
|
|
708
|
+
* `HttpHandler.start`, `write` or `end` would, and on a field index the
|
|
709
|
+
* handler gives no value span for; `tryServeHandler` answers each as an
|
|
710
|
+
* `Err` instead.
|
|
711
|
+
* @param h the handler `r` came from
|
|
712
|
+
* @param r a request taken and not yet answered
|
|
713
|
+
* @param pathStart where the path starts in `r.head`
|
|
714
|
+
* @param pathEnd one past its last byte
|
|
715
|
+
* @returns `HANDLER_SENT`, `HANDLER_DROPPED`, or `STATIC_NOT_FOUND` with nothing sent
|
|
716
|
+
*/
|
|
717
|
+
serveHandler(h: HttpHandler, r: HttpRequest, pathStart: i32, pathEnd: i32): i32 {
|
|
718
|
+
const head: boolean = r.methodIs("HEAD")
|
|
719
|
+
const planned = this.staticRequest(r, pathStart, pathEnd)
|
|
720
|
+
if (planned.isErr()) {
|
|
721
|
+
// `HttpRequest.valueStart`'s contract, as this read it before: an index
|
|
722
|
+
// `header` answered always has a span, so this is the handler's fault.
|
|
723
|
+
// `tryServeHandler` answers it as an `Err` instead.
|
|
724
|
+
panic(planned.error)
|
|
725
|
+
}
|
|
726
|
+
const status: i32 = planned.value
|
|
727
|
+
if (status === STATIC_NOT_FOUND) {
|
|
728
|
+
return STATIC_NOT_FOUND
|
|
729
|
+
}
|
|
730
|
+
const names: u8[][] = []
|
|
731
|
+
const values: u8[][] = []
|
|
732
|
+
this.staticFields(status, names, values)
|
|
733
|
+
let sent: i32 = h.start(r, status, names, values, toI32(this.staticBodyLength(status)))
|
|
734
|
+
if (sent === HANDLER_SENT && !head && this.staticBodyLength(status) > 0) {
|
|
735
|
+
let pos: i64 = this.from
|
|
736
|
+
while (sent === HANDLER_SENT && pos < this.to) {
|
|
737
|
+
const n: i32 = this.staticFill(pos)
|
|
738
|
+
// `staticLoad` made `slot` an index of `bytes`, or -1.
|
|
739
|
+
const slot: i32 = this.slot
|
|
740
|
+
sent = h.write(
|
|
741
|
+
r,
|
|
742
|
+
slot >= 0 && slot < toI32(this.bytes.length) ? this.bytes[slot] : this.chunk,
|
|
743
|
+
slot >= 0 ? toI32(pos) : toI32(0),
|
|
744
|
+
n
|
|
745
|
+
)
|
|
746
|
+
pos = staticAdd64(pos, toI64(n))
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
this.staticClose()
|
|
750
|
+
// `end` also after `HANDLER_DROPPED`: it is what hands `r` back.
|
|
751
|
+
return h.end(r)
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* `serveHandler` through the handler's `try` calls: a misuse that makes
|
|
756
|
+
* `start`, `write` or `end` panic comes back as the handler's reason
|
|
757
|
+
* instead, with the file closed; so does a field index the handler gives
|
|
758
|
+
* no value span for, before anything is sent. An `Err` from before the
|
|
759
|
+
* head (that field index, a request not taken) leaves `r` owed an answer;
|
|
760
|
+
* one from after it (`tryWrite` or `tryEnd` refusing the body) has closed
|
|
761
|
+
* the connection and handed `r` back, as `HttpHandler` describes.
|
|
762
|
+
* @param h the handler `r` came from
|
|
763
|
+
* @param r a request taken and not yet answered
|
|
764
|
+
* @param pathStart where the path starts in `r.head`
|
|
765
|
+
* @param pathEnd one past its last byte
|
|
766
|
+
* @returns Ok of what `serveHandler` answers, or Err of the handler's reason
|
|
767
|
+
*/
|
|
768
|
+
tryServeHandler(h: HttpHandler, r: HttpRequest, pathStart: i32, pathEnd: i32): Result<i32, string> {
|
|
769
|
+
const head: boolean = r.methodIs("HEAD")
|
|
770
|
+
const status: i32 = this.staticRequest(r, pathStart, pathEnd).orReturn()
|
|
771
|
+
if (status === STATIC_NOT_FOUND) {
|
|
772
|
+
return Ok(STATIC_NOT_FOUND)
|
|
773
|
+
}
|
|
774
|
+
const names: u8[][] = []
|
|
775
|
+
const values: u8[][] = []
|
|
776
|
+
this.staticFields(status, names, values)
|
|
777
|
+
const started: Result<i32, string> = h.tryStart(
|
|
778
|
+
r,
|
|
779
|
+
status,
|
|
780
|
+
names,
|
|
781
|
+
values,
|
|
782
|
+
toI32(this.staticBodyLength(status))
|
|
783
|
+
)
|
|
784
|
+
if (started.isErr()) {
|
|
785
|
+
this.staticClose()
|
|
786
|
+
return Err(started.error)
|
|
787
|
+
}
|
|
788
|
+
let sent: i32 = started.value
|
|
789
|
+
if (sent === HANDLER_SENT && !head && this.staticBodyLength(status) > 0) {
|
|
790
|
+
let pos: i64 = this.from
|
|
791
|
+
while (sent === HANDLER_SENT && pos < this.to) {
|
|
792
|
+
const n: i32 = this.staticFill(pos)
|
|
793
|
+
// `staticLoad` made `slot` an index of `bytes`, or -1.
|
|
794
|
+
const slot: i32 = this.slot
|
|
795
|
+
const wrote: Result<i32, string> = h.tryWrite(
|
|
796
|
+
r,
|
|
797
|
+
slot >= 0 && slot < toI32(this.bytes.length) ? this.bytes[slot] : this.chunk,
|
|
798
|
+
slot >= 0 ? toI32(pos) : toI32(0),
|
|
799
|
+
n
|
|
800
|
+
)
|
|
801
|
+
if (wrote.isErr()) {
|
|
802
|
+
this.staticClose()
|
|
803
|
+
return Err(wrote.error)
|
|
804
|
+
}
|
|
805
|
+
sent = wrote.value
|
|
806
|
+
pos = staticAdd64(pos, toI64(n))
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
this.staticClose()
|
|
810
|
+
// `tryEnd` also after `HANDLER_DROPPED`: it is what hands `r` back, and
|
|
811
|
+
// for a dropped request it answers `Ok(HANDLER_DROPPED)`.
|
|
812
|
+
const ended: Result<i32, string> = h.tryEnd(r)
|
|
813
|
+
if (ended.isErr()) {
|
|
814
|
+
return Err(ended.error)
|
|
815
|
+
}
|
|
816
|
+
return Ok(ended.value)
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Reads the request's conditional, range and coding headers and plans the
|
|
821
|
+
* answer: its status, or `STATIC_NOT_FOUND`; `Err` when the handler gives
|
|
822
|
+
* no value span for a field index its `header` answered.
|
|
823
|
+
* @internal Called by `serveHandler` and `tryServeHandler`.
|
|
824
|
+
*/
|
|
825
|
+
staticRequest(r: HttpRequest, pathStart: i32, pathEnd: i32): Result<i32, string> {
|
|
826
|
+
const ae: i32 = r.header("accept-encoding")
|
|
827
|
+
this.acceptStart = staticValue(r, ae, true).orReturn()
|
|
828
|
+
this.acceptEnd = staticValue(r, ae, false).orReturn()
|
|
829
|
+
const nm: i32 = r.header("if-none-match")
|
|
830
|
+
this.noneMatchStart = staticValue(r, nm, true).orReturn()
|
|
831
|
+
this.noneMatchEnd = staticValue(r, nm, false).orReturn()
|
|
832
|
+
const ims: i32 = r.header("if-modified-since")
|
|
833
|
+
this.sinceStart = staticValue(r, ims, true).orReturn()
|
|
834
|
+
this.sinceEnd = staticValue(r, ims, false).orReturn()
|
|
835
|
+
const rg: i32 = r.header("range")
|
|
836
|
+
this.rangeStart = staticValue(r, rg, true).orReturn()
|
|
837
|
+
this.rangeEnd = staticValue(r, rg, false).orReturn()
|
|
838
|
+
const ir: i32 = r.header("if-range")
|
|
839
|
+
this.ifRangeStart = staticValue(r, ir, true).orReturn()
|
|
840
|
+
this.ifRangeEnd = staticValue(r, ir, false).orReturn()
|
|
841
|
+
const head: boolean = r.methodIs("HEAD")
|
|
842
|
+
return Ok(this.staticPlan(r.head, pathStart, pathEnd, head || r.methodIs("GET"), head))
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Decides the answer to a request whose fields the wrapper copied: 405 for
|
|
847
|
+
* a method other than GET and HEAD, `STATIC_NOT_FOUND`, or 200, 206, 304 or
|
|
848
|
+
* 416 with the file open on `fd` and its bytes in a slot when they fit.
|
|
849
|
+
* @internal Called by `serve` and `serveHandler`.
|
|
850
|
+
*/
|
|
851
|
+
staticPlan(head: u8[], pathStart: i32, pathEnd: i32, allowed: boolean, quiet: boolean): i32 {
|
|
852
|
+
this.refused = false
|
|
853
|
+
this.status = 0
|
|
854
|
+
this.fd = -1
|
|
855
|
+
this.slot = -1
|
|
856
|
+
if (!allowed) {
|
|
857
|
+
this.status = 405
|
|
858
|
+
return 405
|
|
859
|
+
}
|
|
860
|
+
if (this.root.length === 0 || this.staticResolve(head, pathStart, pathEnd) !== 0) {
|
|
861
|
+
return STATIC_NOT_FOUND
|
|
862
|
+
}
|
|
863
|
+
if (this.size > STATIC_MAX_FILE) {
|
|
864
|
+
this.staticClose()
|
|
865
|
+
return STATIC_NOT_FOUND
|
|
866
|
+
}
|
|
867
|
+
this.staticTag()
|
|
868
|
+
this.from = 0
|
|
869
|
+
this.to = this.size
|
|
870
|
+
let status: i32 = 200
|
|
871
|
+
if (this.noneMatchStart >= 0) {
|
|
872
|
+
if (this.staticNoneMatch(head)) {
|
|
873
|
+
status = 304
|
|
874
|
+
}
|
|
875
|
+
} else if (this.sinceStart >= 0) {
|
|
876
|
+
const since: i64 = staticDate(head, this.sinceStart, this.sinceEnd)
|
|
877
|
+
const last: i64 = this.staticSecond()
|
|
878
|
+
if (since >= 0 && last >= 0 && last <= since) {
|
|
879
|
+
status = 304
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
if (status === 200 && this.rangeStart >= 0 && !quiet) {
|
|
883
|
+
status = this.staticRange(head)
|
|
884
|
+
}
|
|
885
|
+
if (status === 200 || status === 206) {
|
|
886
|
+
this.staticLoad()
|
|
887
|
+
}
|
|
888
|
+
this.status = status
|
|
889
|
+
return status
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Decodes and checks the path, resolves it under the root, chooses the
|
|
894
|
+
* coding and opens the file: 0, or `STATIC_NOT_FOUND` with `refused` set
|
|
895
|
+
* for a path that was refused.
|
|
896
|
+
* @internal Called by `staticPlan`.
|
|
897
|
+
*/
|
|
898
|
+
staticResolve(head: u8[], pathStart: i32, pathEnd: i32): i32 {
|
|
899
|
+
using _scope = arena()
|
|
900
|
+
const length: i32 = staticSub(pathEnd, pathStart)
|
|
901
|
+
if (length < 0 || length > this.limits.maxPath) {
|
|
902
|
+
this.refused = true
|
|
903
|
+
return STATIC_NOT_FOUND
|
|
904
|
+
}
|
|
905
|
+
const path: u8[] = this.path
|
|
906
|
+
const decoded: i32 = tryPercentDecode(head, pathStart, length, path, 0, 0)
|
|
907
|
+
if (decoded < 0) {
|
|
908
|
+
this.refused = true
|
|
909
|
+
return STATIC_NOT_FOUND
|
|
910
|
+
}
|
|
911
|
+
// `path` has 16 bytes of room past `maxPath`, so the decoded name and an
|
|
912
|
+
// appended `index.html` fit; each loop is still bounded by `room`, the
|
|
913
|
+
// guard the prover reads.
|
|
914
|
+
const room: i32 = toI32(path.length)
|
|
915
|
+
let m: i32 = decoded
|
|
916
|
+
if (decoded > 0 && staticByte(path, 0) === 47) {
|
|
917
|
+
m = decoded - 1
|
|
918
|
+
for (let k: i32 = 1; k < decoded && k < room; k++) {
|
|
919
|
+
path[k - 1] = path[k]
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
if (m === 0 || staticByte(path, staticSub(m, 1)) === 47) {
|
|
923
|
+
if (!this.limits.index) {
|
|
924
|
+
return STATIC_NOT_FOUND
|
|
925
|
+
}
|
|
926
|
+
const index: string = "index.html"
|
|
927
|
+
const size: i32 = toI32(index.length)
|
|
928
|
+
for (let k: i32 = 0; k < size; k++) {
|
|
929
|
+
if (staticPut(path, staticAdd(m, k), toI32(index.charCodeAt(k))) < 0) {
|
|
930
|
+
this.refused = true
|
|
931
|
+
return STATIC_NOT_FOUND
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
m = staticAdd(m, size)
|
|
935
|
+
}
|
|
936
|
+
if (m > room) {
|
|
937
|
+
this.refused = true
|
|
938
|
+
return STATIC_NOT_FOUND
|
|
939
|
+
}
|
|
940
|
+
// Each segment is refused when it is empty or starts with `.`, which
|
|
941
|
+
// `dot` holds from its first byte, and any byte below 32, 127 or `\`.
|
|
942
|
+
let segment: i32 = 0
|
|
943
|
+
let dot: boolean = false
|
|
944
|
+
for (let k: i32 = 0; k < m && k < room; k++) {
|
|
945
|
+
const b: i32 = toI32(path[k])
|
|
946
|
+
if (b === 47) {
|
|
947
|
+
if (k === segment || dot) {
|
|
948
|
+
this.refused = true
|
|
949
|
+
return STATIC_NOT_FOUND
|
|
950
|
+
}
|
|
951
|
+
segment = k + 1
|
|
952
|
+
dot = false
|
|
953
|
+
} else if (b < 32 || b === 127 || b === 92) {
|
|
954
|
+
this.refused = true
|
|
955
|
+
return STATIC_NOT_FOUND
|
|
956
|
+
} else if (k === segment && b === 46) {
|
|
957
|
+
dot = true
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
if (m === segment || dot) {
|
|
961
|
+
this.refused = true
|
|
962
|
+
return STATIC_NOT_FOUND
|
|
963
|
+
}
|
|
964
|
+
if (m < 0 || m > toI32(path.length)) {
|
|
965
|
+
this.refused = true
|
|
966
|
+
return STATIC_NOT_FOUND
|
|
967
|
+
}
|
|
968
|
+
this.pathLength = m
|
|
969
|
+
const base: string = this.under + String.fromBytes(path, 0, m)
|
|
970
|
+
const real: string | null = realpathSync(base)
|
|
971
|
+
if (real === null) {
|
|
972
|
+
return STATIC_NOT_FOUND
|
|
973
|
+
}
|
|
974
|
+
if (!real.startsWith(this.under)) {
|
|
975
|
+
this.refused = true
|
|
976
|
+
return STATIC_NOT_FOUND
|
|
977
|
+
}
|
|
978
|
+
// Each time is read before its file is opened: a file renamed over it in
|
|
979
|
+
// between is then served once under the older time, and the next request
|
|
980
|
+
// sees the newer one and reads it again, never the reverse.
|
|
981
|
+
let mtime: f64 = statMtimeSync(real)
|
|
982
|
+
let fd: i32 = fileOpen(real)
|
|
983
|
+
if (fd < 0) {
|
|
984
|
+
return STATIC_NOT_FOUND
|
|
985
|
+
}
|
|
986
|
+
this.coding = STATIC_IDENTITY
|
|
987
|
+
if (this.limits.precompressed && this.acceptStart >= 0) {
|
|
988
|
+
this.staticAccept(head)
|
|
989
|
+
const brFirst: boolean = this.qBr >= this.qGzip
|
|
990
|
+
for (let pass: i32 = 0; pass < 2; pass++) {
|
|
991
|
+
const coding: i32 = (pass === 0) === brFirst ? STATIC_BR : STATIC_GZIP
|
|
992
|
+
const q: i32 = coding === STATIC_BR ? this.qBr : this.qGzip
|
|
993
|
+
if (this.coding === STATIC_IDENTITY && q > 0 && q >= this.qIdentity) {
|
|
994
|
+
const sibling: string | null = realpathSync(base + (coding === STATIC_BR ? ".br" : ".gz"))
|
|
995
|
+
if (sibling !== null && sibling.startsWith(this.under)) {
|
|
996
|
+
const otherTime: f64 = statMtimeSync(sibling)
|
|
997
|
+
const other: i32 = fileOpen(sibling)
|
|
998
|
+
if (other >= 0) {
|
|
999
|
+
fileClose(fd)
|
|
1000
|
+
fd = other
|
|
1001
|
+
mtime = otherTime
|
|
1002
|
+
this.coding = coding
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
this.fd = fd
|
|
1009
|
+
this.size = fileSize(fd)
|
|
1010
|
+
// biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
|
|
1011
|
+
this.mtime = mtime === mtime ? mtime : 0
|
|
1012
|
+
if (this.size < 0) {
|
|
1013
|
+
this.staticClose()
|
|
1014
|
+
return STATIC_NOT_FOUND
|
|
1015
|
+
}
|
|
1016
|
+
return 0
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Reads `Accept-Encoding` into `qBr`, `qGzip` and `qIdentity`: `x-gzip` is
|
|
1021
|
+
* `gzip`, `*` stands for a coding the field does not name, the file as it
|
|
1022
|
+
* is stays acceptable unless given q=0 but ranks below any coding offered
|
|
1023
|
+
* when the field does not name it, and a malformed element is skipped.
|
|
1024
|
+
* @internal Called by `staticResolve`.
|
|
1025
|
+
*/
|
|
1026
|
+
staticAccept(head: u8[]): void {
|
|
1027
|
+
let br: i32 = -1
|
|
1028
|
+
let gzip: i32 = -1
|
|
1029
|
+
let identity: i32 = -1
|
|
1030
|
+
let star: i32 = -1
|
|
1031
|
+
// Each scan is bounded by `e` and by `n`, and starts at `i`, which is
|
|
1032
|
+
// never below 0 (an absent field is -1): the guards the prover reads for
|
|
1033
|
+
// every index below.
|
|
1034
|
+
const n: i32 = toI32(head.length)
|
|
1035
|
+
let i: i32 = this.acceptStart
|
|
1036
|
+
const e: i32 = this.acceptEnd
|
|
1037
|
+
while (i < e && i >= 0) {
|
|
1038
|
+
let j: i32 = i
|
|
1039
|
+
while (j < e && j < n && head[j] !== 44) {
|
|
1040
|
+
j++
|
|
1041
|
+
}
|
|
1042
|
+
let a: i32 = i
|
|
1043
|
+
while (a < j && a < n && staticSpace(toI32(head[a]))) {
|
|
1044
|
+
a++
|
|
1045
|
+
}
|
|
1046
|
+
let b: i32 = a
|
|
1047
|
+
while (b < j && b < n && head[b] !== 59 && !staticSpace(toI32(head[b]))) {
|
|
1048
|
+
b++
|
|
1049
|
+
}
|
|
1050
|
+
let q: i32 = 1000
|
|
1051
|
+
let ok: boolean = b > a
|
|
1052
|
+
let c: i32 = b
|
|
1053
|
+
while (ok && c < j) {
|
|
1054
|
+
while (c < j && c < n && staticSpace(toI32(head[c]))) {
|
|
1055
|
+
c++
|
|
1056
|
+
}
|
|
1057
|
+
if (c === j) {
|
|
1058
|
+
break
|
|
1059
|
+
}
|
|
1060
|
+
if (c >= n || head[c] !== 59) {
|
|
1061
|
+
ok = false
|
|
1062
|
+
break
|
|
1063
|
+
}
|
|
1064
|
+
c++
|
|
1065
|
+
while (c < j && c < n && staticSpace(toI32(head[c]))) {
|
|
1066
|
+
c++
|
|
1067
|
+
}
|
|
1068
|
+
const ps: i32 = c
|
|
1069
|
+
while (c < j && c < n && head[c] !== 59 && !staticSpace(toI32(head[c]))) {
|
|
1070
|
+
c++
|
|
1071
|
+
}
|
|
1072
|
+
if (
|
|
1073
|
+
staticSub(c, ps) >= 2 &&
|
|
1074
|
+
staticLower(staticByte(head, ps)) === 113 &&
|
|
1075
|
+
staticByte(head, staticAdd(ps, 1)) === 61
|
|
1076
|
+
) {
|
|
1077
|
+
q = staticQ(head, staticAdd(ps, 2), c)
|
|
1078
|
+
ok = q >= 0
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
if (ok) {
|
|
1082
|
+
if (staticIs(head, a, b, "br")) {
|
|
1083
|
+
br = q
|
|
1084
|
+
} else if (staticIs(head, a, b, "gzip") || staticIs(head, a, b, "x-gzip")) {
|
|
1085
|
+
gzip = q
|
|
1086
|
+
} else if (staticIs(head, a, b, "identity")) {
|
|
1087
|
+
identity = q
|
|
1088
|
+
} else if (staticSub(b, a) === 1 && staticByte(head, a) === 42) {
|
|
1089
|
+
star = q
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
i = staticAdd(j, 1)
|
|
1093
|
+
}
|
|
1094
|
+
// A coding the header does not name takes `*`'s weight, else its default.
|
|
1095
|
+
const coded: i32 = star >= 0 ? star : 0
|
|
1096
|
+
const plain: i32 = star >= 0 ? star : 1
|
|
1097
|
+
this.qBr = br >= 0 ? br : coded
|
|
1098
|
+
this.qGzip = gzip >= 0 ? gzip : coded
|
|
1099
|
+
this.qIdentity = identity >= 0 ? identity : plain
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
/** The file's modification time in whole seconds, or -1 when it has none. @internal */
|
|
1103
|
+
staticSecond(): i64 {
|
|
1104
|
+
return this.mtime > 0 ? toI64(this.mtime / 1000) : -1
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/** Writes the strong ETag into `etag`. @internal Called by `staticPlan`. */
|
|
1108
|
+
staticTag(): void {
|
|
1109
|
+
// `etag` has 48 bytes, and the longest tag, two 16-digit numbers, three
|
|
1110
|
+
// quotes and dashes and a coding, is 38.
|
|
1111
|
+
// A write that does not fit answers -1 and every later one carries it:
|
|
1112
|
+
// the file then has no ETag (`etagLength` 0), sent or matched — which
|
|
1113
|
+
// RFC 9110 §8.8.3 allows, an ETag being optional; at 38 bytes of 48 it
|
|
1114
|
+
// does not happen.
|
|
1115
|
+
const out: u8[] = this.etag
|
|
1116
|
+
let at: i32 = staticPut(out, 0, 34)
|
|
1117
|
+
at = staticDigits(out, at, this.size, 16)
|
|
1118
|
+
at = staticPut(out, at, 45)
|
|
1119
|
+
at = staticDigits(out, at, this.mtime > 0 ? toI64(this.mtime * 1000) : 0, 16)
|
|
1120
|
+
if (this.coding !== STATIC_IDENTITY) {
|
|
1121
|
+
at = staticPut(out, at, 45)
|
|
1122
|
+
at = staticPut(out, at, this.coding === STATIC_BR ? 98 : 103)
|
|
1123
|
+
at = staticPut(out, at, this.coding === STATIC_BR ? 114 : 122)
|
|
1124
|
+
}
|
|
1125
|
+
at = staticPut(out, at, 34)
|
|
1126
|
+
this.etagLength = at < 0 ? 0 : at
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/** Whether `head[start .. end)` is the ETag, byte for byte. @internal */
|
|
1130
|
+
staticSameTag(head: u8[], start: i32, end: i32): boolean {
|
|
1131
|
+
if (this.etagLength <= 0 || staticSub(end, start) !== this.etagLength) {
|
|
1132
|
+
return false
|
|
1133
|
+
}
|
|
1134
|
+
const etag: u8[] = this.etag
|
|
1135
|
+
for (let k: i32 = 0; k < this.etagLength; k++) {
|
|
1136
|
+
if (k >= toI32(etag.length) || staticByte(head, staticAdd(start, k)) !== toI32(etag[k])) {
|
|
1137
|
+
return false
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
return true
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/** Whether `If-None-Match` is `*` or names the ETag, weakly compared. @internal Called by `staticPlan`. */
|
|
1144
|
+
staticNoneMatch(head: u8[]): boolean {
|
|
1145
|
+
// Every index below is at least `k`, never below 0 (an absent field is
|
|
1146
|
+
// -1), and is tested against `n`: the guards the prover reads.
|
|
1147
|
+
const n: i32 = toI32(head.length)
|
|
1148
|
+
let k: i32 = this.noneMatchStart
|
|
1149
|
+
const e: i32 = this.noneMatchEnd
|
|
1150
|
+
while (k < e && k >= 0 && k < n) {
|
|
1151
|
+
const b: i32 = toI32(head[k])
|
|
1152
|
+
if (b === 42) {
|
|
1153
|
+
return true
|
|
1154
|
+
}
|
|
1155
|
+
if (
|
|
1156
|
+
b === 87 &&
|
|
1157
|
+
staticByte(head, staticAdd(k, 1)) === 47 &&
|
|
1158
|
+
staticByte(head, staticAdd(k, 2)) === 34 &&
|
|
1159
|
+
staticAdd(k, 2) < e
|
|
1160
|
+
) {
|
|
1161
|
+
k = staticAdd(k, 2)
|
|
1162
|
+
} else if (b === 34) {
|
|
1163
|
+
let close: i32 = k + 1
|
|
1164
|
+
while (close < e && close < n && head[close] !== 34) {
|
|
1165
|
+
close++
|
|
1166
|
+
}
|
|
1167
|
+
if (close >= e || close >= n) {
|
|
1168
|
+
return false
|
|
1169
|
+
}
|
|
1170
|
+
if (this.staticSameTag(head, k, close + 1)) {
|
|
1171
|
+
return true
|
|
1172
|
+
}
|
|
1173
|
+
k = close + 1
|
|
1174
|
+
} else {
|
|
1175
|
+
k++
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
return false
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
/** Whether `If-Range` is the strong ETag, or the Last-Modified date of a file unchanged for a second or more. @internal Called by `staticRange`. */
|
|
1182
|
+
staticIfRange(head: u8[]): boolean {
|
|
1183
|
+
let s: i32 = this.ifRangeStart
|
|
1184
|
+
let e: i32 = this.ifRangeEnd
|
|
1185
|
+
// `s` is never below 0 here (an absent field is -1) and each scan is
|
|
1186
|
+
// tested against `n`: the guards the prover reads.
|
|
1187
|
+
const n: i32 = toI32(head.length)
|
|
1188
|
+
while (s < e && s >= 0 && s < n && staticSpace(toI32(head[s]))) {
|
|
1189
|
+
s++
|
|
1190
|
+
}
|
|
1191
|
+
while (e > s && staticSpace(staticByte(head, staticSub(e, 1)))) {
|
|
1192
|
+
e = staticSub(e, 1)
|
|
1193
|
+
}
|
|
1194
|
+
if (s < e && staticByte(head, s) === 34) {
|
|
1195
|
+
return this.staticSameTag(head, s, e)
|
|
1196
|
+
}
|
|
1197
|
+
const at: i64 = staticDate(head, s, e)
|
|
1198
|
+
const last: i64 = this.staticSecond()
|
|
1199
|
+
// A date validates only a strong Last-Modified, a second or more before
|
|
1200
|
+
// now (RFC 9110 §13.1.5): two versions within one second share the date.
|
|
1201
|
+
// `last` is a time in seconds, far below 2^53, so the product never wraps.
|
|
1202
|
+
return at >= 0 && at === last && toI64((toU64(last) + toU64(1)) * toU64(1000)) <= toI64(Date.now())
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/**
|
|
1206
|
+
* Reads `Range`: 200 to send the whole file (another unit, more than one
|
|
1207
|
+
* range, or an `If-Range` that does not match), 206 with `from` and `to`
|
|
1208
|
+
* set, or 416 for a malformed or unsatisfiable range.
|
|
1209
|
+
* @internal Called by `staticPlan`.
|
|
1210
|
+
*/
|
|
1211
|
+
staticRange(head: u8[]): i32 {
|
|
1212
|
+
let s: i32 = this.rangeStart
|
|
1213
|
+
let e: i32 = this.rangeEnd
|
|
1214
|
+
// `s` is never below 0 here (an absent field is -1) and each scan is
|
|
1215
|
+
// tested against `n`: the guards the prover reads.
|
|
1216
|
+
const n: i32 = toI32(head.length)
|
|
1217
|
+
while (s < e && s >= 0 && s < n && staticSpace(toI32(head[s]))) {
|
|
1218
|
+
s++
|
|
1219
|
+
}
|
|
1220
|
+
while (e > s && staticSpace(staticByte(head, staticSub(e, 1)))) {
|
|
1221
|
+
e = staticSub(e, 1)
|
|
1222
|
+
}
|
|
1223
|
+
if (staticSub(e, s) < 6 || !staticIs(head, s, staticAdd(s, 6), "bytes=")) {
|
|
1224
|
+
return 200
|
|
1225
|
+
}
|
|
1226
|
+
if (this.ifRangeStart >= 0 && !this.staticIfRange(head)) {
|
|
1227
|
+
return 200
|
|
1228
|
+
}
|
|
1229
|
+
let k: i32 = staticAdd(s, 6)
|
|
1230
|
+
while (k < e && k >= 0 && k < n && staticSpace(toI32(head[k]))) {
|
|
1231
|
+
k++
|
|
1232
|
+
}
|
|
1233
|
+
let dash: i32 = -1
|
|
1234
|
+
for (let x: i32 = k; x < e && x >= 0 && x < n; x++) {
|
|
1235
|
+
if (head[x] === 44) {
|
|
1236
|
+
return 200
|
|
1237
|
+
}
|
|
1238
|
+
if (head[x] === 45 && dash < 0) {
|
|
1239
|
+
dash = x
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
if (dash < 0 || this.size === 0) {
|
|
1243
|
+
return 416
|
|
1244
|
+
}
|
|
1245
|
+
const first: i64 = dash > k ? staticNumber(head, k, dash) : -2
|
|
1246
|
+
const last: i64 = staticAdd(dash, 1) < e ? staticNumber(head, staticAdd(dash, 1), e) : -2
|
|
1247
|
+
if (first === -1 || last === -1) {
|
|
1248
|
+
return 416
|
|
1249
|
+
}
|
|
1250
|
+
if (first === -2) {
|
|
1251
|
+
if (last <= 0) {
|
|
1252
|
+
return 416
|
|
1253
|
+
}
|
|
1254
|
+
this.from = this.size > last ? staticSub64(this.size, last) : 0
|
|
1255
|
+
this.to = this.size
|
|
1256
|
+
return 206
|
|
1257
|
+
}
|
|
1258
|
+
if ((last !== -2 && last < first) || first >= this.size) {
|
|
1259
|
+
return 416
|
|
1260
|
+
}
|
|
1261
|
+
this.from = first
|
|
1262
|
+
this.to = last === -2 || last >= this.size ? this.size : staticAdd64(last, 1)
|
|
1263
|
+
return 206
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* Finds the file's slot, or reads it into one when it is missing or its
|
|
1268
|
+
* time or size moved; leaves `slot` at -1 for a file too big to keep or
|
|
1269
|
+
* one that changed while it was read, which is then streamed from `fd`.
|
|
1270
|
+
* @internal Called by `staticPlan`.
|
|
1271
|
+
*/
|
|
1272
|
+
staticLoad(): void {
|
|
1273
|
+
if (this.size > toI64(this.limits.maxCachedBytes)) {
|
|
1274
|
+
return
|
|
1275
|
+
}
|
|
1276
|
+
const m: i32 = this.pathLength
|
|
1277
|
+
const path: u8[] = this.path
|
|
1278
|
+
const keyLength: i32[] = this.keyLength
|
|
1279
|
+
const keyCoding: i32[] = this.keyCoding
|
|
1280
|
+
const keys: u8[][] = this.keys
|
|
1281
|
+
const entries: i32 = this.limits.maxEntries
|
|
1282
|
+
let hit: i32 = -1
|
|
1283
|
+
for (let k: i32 = 0; k < entries && hit < 0; k++) {
|
|
1284
|
+
if (k < toI32(keyLength.length) && k < toI32(keyCoding.length) && k < toI32(keys.length)) {
|
|
1285
|
+
if (keyLength[k] === m && keyCoding[k] === this.coding) {
|
|
1286
|
+
const key: u8[] = keys[k]
|
|
1287
|
+
let same: boolean = true
|
|
1288
|
+
for (let x: i32 = 0; x < m && same; x++) {
|
|
1289
|
+
same = x < toI32(key.length) && x < toI32(path.length) && key[x] === path[x]
|
|
1290
|
+
}
|
|
1291
|
+
if (same) {
|
|
1292
|
+
hit = k
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
const sizes: i64[] = this.sizes
|
|
1298
|
+
const mtimes: f64[] = this.mtimes
|
|
1299
|
+
const bytes: u8[][] = this.bytes
|
|
1300
|
+
let k: i32 = hit
|
|
1301
|
+
if (k < 0) {
|
|
1302
|
+
k = this.hand
|
|
1303
|
+
const next: i32 = staticAdd(k, 1)
|
|
1304
|
+
this.hand = next < 0 || next >= entries ? 0 : next
|
|
1305
|
+
}
|
|
1306
|
+
// Every row has `maxEntries` entries and `hand` stays below it; an entry
|
|
1307
|
+
// outside them, which never happens, streams like a file too big to keep.
|
|
1308
|
+
if (
|
|
1309
|
+
k < 0 ||
|
|
1310
|
+
k >= toI32(keyLength.length) ||
|
|
1311
|
+
k >= toI32(keyCoding.length) ||
|
|
1312
|
+
k >= toI32(keys.length) ||
|
|
1313
|
+
k >= toI32(sizes.length) ||
|
|
1314
|
+
k >= toI32(mtimes.length) ||
|
|
1315
|
+
k >= toI32(bytes.length)
|
|
1316
|
+
) {
|
|
1317
|
+
return
|
|
1318
|
+
}
|
|
1319
|
+
if (hit >= 0 && sizes[k] === this.size && mtimes[k] === this.mtime) {
|
|
1320
|
+
this.slot = k
|
|
1321
|
+
return
|
|
1322
|
+
}
|
|
1323
|
+
const at: i32 = k
|
|
1324
|
+
keyLength[at] = -1
|
|
1325
|
+
const size: i32 = toI32(this.size)
|
|
1326
|
+
let into: u8[] = bytes[at]
|
|
1327
|
+
if (toI32(into.length) < size) {
|
|
1328
|
+
into = staticBytes(staticCap(size))
|
|
1329
|
+
// A call may change any array's length, so the bound is read again.
|
|
1330
|
+
if (at >= toI32(bytes.length)) {
|
|
1331
|
+
return
|
|
1332
|
+
}
|
|
1333
|
+
bytes[at] = into
|
|
1334
|
+
}
|
|
1335
|
+
if (!staticReadAll(this.fd, into, size)) {
|
|
1336
|
+
return
|
|
1337
|
+
}
|
|
1338
|
+
if (
|
|
1339
|
+
at >= toI32(keyLength.length) ||
|
|
1340
|
+
at >= toI32(keyCoding.length) ||
|
|
1341
|
+
at >= toI32(keys.length) ||
|
|
1342
|
+
at >= toI32(sizes.length) ||
|
|
1343
|
+
at >= toI32(mtimes.length)
|
|
1344
|
+
) {
|
|
1345
|
+
return
|
|
1346
|
+
}
|
|
1347
|
+
copyBytes(keys[at], 0, path, 0, m)
|
|
1348
|
+
keyLength[at] = m
|
|
1349
|
+
keyCoding[at] = this.coding
|
|
1350
|
+
sizes[at] = this.size
|
|
1351
|
+
mtimes[at] = this.mtime
|
|
1352
|
+
this.slot = at
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* How many body bytes from `pos` are ready to write: the rest of the range
|
|
1357
|
+
* from a slot, or one chunk read from `fd` into `chunk`, its tail zeroed
|
|
1358
|
+
* when the file shrank since it was measured.
|
|
1359
|
+
* @internal Called by `serve` and `serveHandler`.
|
|
1360
|
+
*/
|
|
1361
|
+
staticFill(pos: i64): i32 {
|
|
1362
|
+
const left: i64 = staticSub64(this.to, pos)
|
|
1363
|
+
if (this.slot >= 0) {
|
|
1364
|
+
return toI32(left)
|
|
1365
|
+
}
|
|
1366
|
+
const n: i32 = left > toI64(STATIC_CHUNK) ? STATIC_CHUNK : toI32(left)
|
|
1367
|
+
const got: i32 = fileReadAt(this.fd, this.chunk, 0, n, pos)
|
|
1368
|
+
const chunk: u8[] = this.chunk
|
|
1369
|
+
for (let k: i32 = 0; k < n && k < toI32(chunk.length); k++) {
|
|
1370
|
+
if (k >= got) {
|
|
1371
|
+
chunk[k] = 0
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
return n
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/** Closes the file the plan opened, if any. @internal */
|
|
1378
|
+
staticClose(): void {
|
|
1379
|
+
if (this.fd >= 0) {
|
|
1380
|
+
fileClose(this.fd)
|
|
1381
|
+
this.fd = -1
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
/** The body length the head declares for `status`. @internal */
|
|
1386
|
+
staticBodyLength(status: i32): i64 {
|
|
1387
|
+
return status === 200 || status === 206 ? staticSub64(this.to, this.from) : 0
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
/**
|
|
1391
|
+
* Pushes the response fields for `status` onto `names` and `values`, all
|
|
1392
|
+
* made fresh: the caller holds them in an arena block.
|
|
1393
|
+
* @internal Called by `serve` and `serveHandler`.
|
|
1394
|
+
*/
|
|
1395
|
+
staticFields(status: i32, names: u8[][], values: u8[][]): void {
|
|
1396
|
+
if (status === 405) {
|
|
1397
|
+
names.push(httpFieldBytes("allow"))
|
|
1398
|
+
values.push(httpFieldBytes("GET, HEAD"))
|
|
1399
|
+
return
|
|
1400
|
+
}
|
|
1401
|
+
const scratch: u8[] = new Array<u8>(64)
|
|
1402
|
+
if (status === 416) {
|
|
1403
|
+
// A size below 2^31 (`STATIC_MAX_FILE`) has 10 digits, which
|
|
1404
|
+
// `scratch` holds; -1 would send the 416 without the field, which
|
|
1405
|
+
// RFC 9110 §15.5.17 only recommends.
|
|
1406
|
+
const at: i32 = staticDigits(scratch, 8, this.size, 10)
|
|
1407
|
+
copyBytes(scratch, 0, httpFieldBytes("bytes */"), 0, 8)
|
|
1408
|
+
if (at >= 0) {
|
|
1409
|
+
names.push(httpFieldBytes("content-range"))
|
|
1410
|
+
values.push(staticCut(scratch, at))
|
|
1411
|
+
}
|
|
1412
|
+
return
|
|
1413
|
+
}
|
|
1414
|
+
if (status !== 304) {
|
|
1415
|
+
names.push(httpFieldBytes("content-type"))
|
|
1416
|
+
values.push(httpFieldBytes(staticContentType(String.fromBytes(this.path, 0, this.pathLength))))
|
|
1417
|
+
}
|
|
1418
|
+
if (this.etagLength > 0) {
|
|
1419
|
+
names.push(httpFieldBytes("etag"))
|
|
1420
|
+
values.push(staticCut(this.etag, this.etagLength))
|
|
1421
|
+
}
|
|
1422
|
+
const date: u8[] = new Array<u8>(HTTP_DATE_SIZE)
|
|
1423
|
+
if (httpDateWrite(date, 0, this.staticSecond()) > 0) {
|
|
1424
|
+
names.push(httpFieldBytes("last-modified"))
|
|
1425
|
+
values.push(date)
|
|
1426
|
+
}
|
|
1427
|
+
if (this.limits.precompressed) {
|
|
1428
|
+
names.push(httpFieldBytes("vary"))
|
|
1429
|
+
values.push(httpFieldBytes("accept-encoding"))
|
|
1430
|
+
}
|
|
1431
|
+
if (status === 304) {
|
|
1432
|
+
return
|
|
1433
|
+
}
|
|
1434
|
+
names.push(httpFieldBytes("accept-ranges"))
|
|
1435
|
+
values.push(httpFieldBytes("bytes"))
|
|
1436
|
+
names.push(httpFieldBytes("x-content-type-options"))
|
|
1437
|
+
values.push(httpFieldBytes("nosniff"))
|
|
1438
|
+
if (this.coding !== STATIC_IDENTITY) {
|
|
1439
|
+
names.push(httpFieldBytes("content-encoding"))
|
|
1440
|
+
values.push(httpFieldBytes(this.coding === STATIC_BR ? "br" : "gzip"))
|
|
1441
|
+
}
|
|
1442
|
+
if (status === 206) {
|
|
1443
|
+
copyBytes(scratch, 0, httpFieldBytes("bytes "), 0, 6)
|
|
1444
|
+
// Three numbers below 2^31 (`STATIC_MAX_FILE`) and the words around
|
|
1445
|
+
// them, 38 bytes at most, fit `scratch`'s 64, so the field is never
|
|
1446
|
+
// dropped: a 206 without it would break RFC 9110 §15.3.7.
|
|
1447
|
+
let at: i32 = staticDigits(scratch, 6, this.from, 10)
|
|
1448
|
+
at = staticPut(scratch, at, 45)
|
|
1449
|
+
at = staticDigits(scratch, at, staticSub64(this.to, 1), 10)
|
|
1450
|
+
at = staticPut(scratch, at, 47)
|
|
1451
|
+
at = staticDigits(scratch, at, this.size, 10)
|
|
1452
|
+
if (at >= 0) {
|
|
1453
|
+
names.push(httpFieldBytes("content-range"))
|
|
1454
|
+
values.push(staticCut(scratch, at))
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
}
|