@amritk/nish-aarch64-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
package/std/net/serve.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* const HOME: i32 = 0 // module constants, in the order the routes are added
|
|
10
10
|
* const USER: i32 = 1
|
|
11
11
|
*
|
|
12
|
-
* async
|
|
12
|
+
* const handle = async (req: Request): Promise<Response> => {
|
|
13
13
|
* switch (req.route) {
|
|
14
14
|
* case HOME: return new Response(200).text("hello\n")
|
|
15
15
|
* case USER: {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* }
|
|
21
21
|
* }
|
|
22
22
|
*
|
|
23
|
-
* export async
|
|
23
|
+
* export const main = async (): Promise<i32> => {
|
|
24
24
|
* const router = new Router(new RouterLimits())
|
|
25
25
|
* router.add("GET", "/")
|
|
26
26
|
* router.add("GET", "/users/:id")
|
|
@@ -32,14 +32,16 @@
|
|
|
32
32
|
* the program's other tasks run while no socket is ready. It routes each
|
|
33
33
|
* request at its head, gathers its body, and hands the whole request to
|
|
34
34
|
* `handle` in a task of its own, spawned with the request's slot; the
|
|
35
|
-
* task's allocations — the
|
|
36
|
-
* whatever `handle` makes on the way — live in the task's region and
|
|
37
|
-
* when it ends.
|
|
35
|
+
* task's allocations — the `Request` it reads through, the response it
|
|
36
|
+
* builds, whatever `handle` makes on the way — live in the task's region and
|
|
37
|
+
* go when it ends.
|
|
38
38
|
*
|
|
39
|
-
* **The task reads
|
|
40
|
-
* the connection parser's own buffer,
|
|
41
|
-
*
|
|
42
|
-
*
|
|
39
|
+
* **The task reads the request in place.** Until it is answered, a request's
|
|
40
|
+
* head is the connection parser's own buffer, and the task reads it, its
|
|
41
|
+
* spans and its body where the server parsed them, with no copy: the record
|
|
42
|
+
* is lent to the task (`lend` of `nish:async`), which the compiler holds to
|
|
43
|
+
* reading it and writing numbers into it (NL2822). A connection that closes
|
|
44
|
+
* while the task runs leaves the buffers to the record until the task ends,
|
|
43
45
|
* and when `handle` answers it writes the status, the content type and the
|
|
44
46
|
* body back into the slot's fixed buffers, in bulk, and wakes the
|
|
45
47
|
* loop. The loop then sends the answer and frees the slot. Nothing the task
|
|
@@ -94,7 +96,7 @@ import {
|
|
|
94
96
|
Http1Server,
|
|
95
97
|
} from "nish/net/http1-server"
|
|
96
98
|
import { ROUTER_BAD_ESCAPE, ROUTER_METHOD_NOT_ALLOWED, Router } from "nish/net/router"
|
|
97
|
-
import { cancel, cancelAfter, readable, sleep, spawn, taskId } from "nish:async"
|
|
99
|
+
import { cancel, cancelAfter, lend, readable, sleep, spawn, taskId } from "nish:async"
|
|
98
100
|
import {
|
|
99
101
|
NET_UNIX_STREAM,
|
|
100
102
|
netClose,
|
|
@@ -125,8 +127,8 @@ const SERVE_LISTENER: i32 = -1
|
|
|
125
127
|
const SERVE_WAKE: i32 = -2
|
|
126
128
|
/** The ready bits that mean a socket can be read or has hung up: EPOLLIN | EPOLLHUP's poll mapping. */
|
|
127
129
|
const SERVE_READ_BITS: i32 = 5
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
// A poll reports 256 sockets at most, and `readyFds` holds two slots for each:
|
|
131
|
+
// 512. The turn loop bounds `k` by 256 directly, so the prover sees `k + k` fit.
|
|
130
132
|
/** `accept`'s answer when the backlog is empty. */
|
|
131
133
|
const SERVE_ACCEPT_NONE: i32 = -11
|
|
132
134
|
/** `accept`'s answer when a connection was reset in the backlog: the next one is tried. */
|
|
@@ -211,7 +213,12 @@ export class ServeStats {
|
|
|
211
213
|
answered: i32 = 0
|
|
212
214
|
/** Requests answered 503 because `inFlight` tasks were already running. */
|
|
213
215
|
busy: i32 = 0
|
|
214
|
-
/**
|
|
216
|
+
/**
|
|
217
|
+
* Answers that failed: one a task wrote that broke a cap or a rule, or a
|
|
218
|
+
* request the server could not hand a task, answered 500 instead before
|
|
219
|
+
* its head; and one whose head or body the carrier refused, whose
|
|
220
|
+
* connection is closed with it unfinished.
|
|
221
|
+
*/
|
|
215
222
|
failed: i32 = 0
|
|
216
223
|
/** How many slots the server made: the most requests ever in flight at once. */
|
|
217
224
|
slots: i32 = 0
|
|
@@ -226,18 +233,19 @@ export class ServeStats {
|
|
|
226
233
|
}
|
|
227
234
|
|
|
228
235
|
/**
|
|
229
|
-
* A request, as `handle` reads it: the
|
|
230
|
-
* valid for as long as the task
|
|
236
|
+
* A request, as `handle` reads it: the server's parsed record, read in place
|
|
237
|
+
* (`lend` of `nish:async`), its head and body valid for as long as the task
|
|
238
|
+
* runs. Copy out what must outlive the task.
|
|
231
239
|
*/
|
|
232
240
|
export class Request {
|
|
233
241
|
/** The index `Router.add` answered for the route, or a negative `ROUTER_*` code. */
|
|
234
242
|
route: i32 = 0
|
|
235
|
-
/** The request as the server parsed it,
|
|
243
|
+
/** The request as the server parsed it, lent to the task: its spans, its head and its body. */
|
|
236
244
|
raw: HttpRequest
|
|
237
245
|
|
|
238
246
|
/**
|
|
239
247
|
* A request over `raw`, which it keeps.
|
|
240
|
-
* @param raw the parsed request, the task
|
|
248
|
+
* @param raw the parsed request, lent to the task for as long as it runs
|
|
241
249
|
*/
|
|
242
250
|
constructor(raw: HttpRequest) {
|
|
243
251
|
this.raw = raw
|
|
@@ -266,11 +274,67 @@ export class Request {
|
|
|
266
274
|
* @returns the decoded value, or an empty string when it holds a malformed escape
|
|
267
275
|
*/
|
|
268
276
|
param(k: i32): string {
|
|
269
|
-
|
|
277
|
+
return this.paramText(k, serveSpanBuffer(this.raw.paramStart(k), this.raw.paramEnd(k)))
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Route parameter `k`, percent-decoded, as `param` reads it, or an error
|
|
282
|
+
* naming the parameter the route does not have.
|
|
283
|
+
* @param k the parameter's position in the route pattern, from 0
|
|
284
|
+
* @returns `Ok` with the decoded value, or an empty string when it holds a malformed escape; `Err` when the route has no parameter `k`
|
|
285
|
+
*/
|
|
286
|
+
tryParam(k: i32): Result<string, string> {
|
|
287
|
+
const out: Result<u8[], string> = this.paramBuffer(k)
|
|
288
|
+
if (out.isErr()) {
|
|
289
|
+
return Err(out.error)
|
|
290
|
+
}
|
|
291
|
+
return Ok(this.paramText(k, out.value))
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Route parameter `k` decoded into `out`, which holds its raw bytes, as text; empty for a malformed escape. */
|
|
295
|
+
private paramText(k: i32, out: u8[]): string {
|
|
270
296
|
const n: i32 = this.raw.param(k, out)
|
|
271
297
|
return n < 0 ? "" : String.fromBytes(out, 0, n)
|
|
272
298
|
}
|
|
273
299
|
|
|
300
|
+
/**
|
|
301
|
+
* Route parameter `k` read as a decimal number, as `paramNumber` reads it,
|
|
302
|
+
* or an error naming the parameter the route does not have.
|
|
303
|
+
* @param k the parameter's position in the route pattern, from 0
|
|
304
|
+
* @returns `Ok` with the number, or -1 when it is empty, holds a non-digit or overflows; `Err` when the route has no parameter `k`
|
|
305
|
+
*/
|
|
306
|
+
tryParamNumber(k: i32): Result<i32, string> {
|
|
307
|
+
return this.raw.tryParamDecimal(k)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Whether route parameter `k` holds a malformed percent escape, as
|
|
312
|
+
* `paramMalformed` answers it, or an error naming the parameter the route
|
|
313
|
+
* does not have.
|
|
314
|
+
* @param k the parameter's position in the route pattern, from 0
|
|
315
|
+
* @returns `Ok` with true when decoding the parameter fails; `Err` when the route has no parameter `k`
|
|
316
|
+
*/
|
|
317
|
+
tryParamMalformed(k: i32): Result<boolean, string> {
|
|
318
|
+
const out: Result<u8[], string> = this.paramBuffer(k)
|
|
319
|
+
if (out.isErr()) {
|
|
320
|
+
return Err(out.error)
|
|
321
|
+
}
|
|
322
|
+
return Ok(this.raw.param(k, out.value) === ROUTER_BAD_ESCAPE)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** A buffer as long as route parameter `k`'s raw bytes, which its decoding never outgrows; `Err` when the route has no parameter `k`. */
|
|
326
|
+
private paramBuffer(k: i32): Result<u8[], string> {
|
|
327
|
+
const start: Result<i32, string> = this.raw.tryParamStart(k)
|
|
328
|
+
if (start.isErr()) {
|
|
329
|
+
return Err(start.error)
|
|
330
|
+
}
|
|
331
|
+
const end: Result<i32, string> = this.raw.tryParamEnd(k)
|
|
332
|
+
if (end.isErr()) {
|
|
333
|
+
return Err(end.error)
|
|
334
|
+
}
|
|
335
|
+
return Ok(serveSpanBuffer(start.value, end.value))
|
|
336
|
+
}
|
|
337
|
+
|
|
274
338
|
/**
|
|
275
339
|
* Route parameter `k` read as a decimal number that is not negative and fits an `i32`. Panics when the request's route has no parameter `k`.
|
|
276
340
|
* @param k the parameter's position in the route pattern, from 0
|
|
@@ -286,7 +350,7 @@ export class Request {
|
|
|
286
350
|
* @returns true when decoding the parameter fails
|
|
287
351
|
*/
|
|
288
352
|
paramMalformed(k: i32): boolean {
|
|
289
|
-
const out: u8[] =
|
|
353
|
+
const out: u8[] = serveSpanBuffer(this.raw.paramStart(k), this.raw.paramEnd(k))
|
|
290
354
|
return this.raw.param(k, out) === ROUTER_BAD_ESCAPE
|
|
291
355
|
}
|
|
292
356
|
|
|
@@ -299,7 +363,7 @@ export class Request {
|
|
|
299
363
|
if (this.raw.queryIndex(name) < 0) {
|
|
300
364
|
return ""
|
|
301
365
|
}
|
|
302
|
-
const out: u8[] =
|
|
366
|
+
const out: u8[] = serveSpanBuffer(this.raw.queryValueStart, this.raw.queryValueEnd)
|
|
303
367
|
const n: i32 = this.raw.query(name, out)
|
|
304
368
|
return n < 0 ? "" : String.fromBytes(out, 0, n)
|
|
305
369
|
}
|
|
@@ -374,21 +438,38 @@ export class Response {
|
|
|
374
438
|
* @returns this response, for a chain
|
|
375
439
|
*/
|
|
376
440
|
bytes(type: string, buf: u8[], off: i32, len: i32): Response {
|
|
377
|
-
|
|
378
|
-
|
|
441
|
+
const set: Result<Response, string> = this.tryBytes(type, buf, off, len)
|
|
442
|
+
if (set.isErr()) {
|
|
443
|
+
panic(set.error)
|
|
444
|
+
}
|
|
445
|
+
return this
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Sets a body of bytes with a content type, as `bytes` does, or an error
|
|
450
|
+
* when the window lies outside `buf`; nothing is set on an error.
|
|
451
|
+
* @param type the `Content-Type` value
|
|
452
|
+
* @param buf the buffer holding the body, kept by the response
|
|
453
|
+
* @param off where the body starts in `buf`
|
|
454
|
+
* @param len how many body bytes there are
|
|
455
|
+
* @returns `Ok` with this response, for a chain, or `Err` when `[off, off + len)` is not inside `buf`
|
|
456
|
+
*/
|
|
457
|
+
tryBytes(type: string, buf: u8[], off: i32, len: i32): Result<Response, string> {
|
|
458
|
+
// In i64, where two i32s cannot overflow.
|
|
459
|
+
const end: i64 = toI64(off) + toI64(len)
|
|
460
|
+
if (off < 0 || len < 0 || end > toI64(buf.length)) {
|
|
461
|
+
return Err(`Response.bytes: [${off}, ${end}) is outside a buffer of ${buf.length}`)
|
|
379
462
|
}
|
|
380
463
|
this.type = type
|
|
381
464
|
if (off === 0) {
|
|
382
465
|
this.body = buf
|
|
383
466
|
} else {
|
|
384
|
-
const copy: u8[] =
|
|
385
|
-
|
|
386
|
-
copy[i] = buf[off + i]
|
|
387
|
-
}
|
|
467
|
+
const copy: u8[] = serveSpanBuffer(0, len)
|
|
468
|
+
copyBytes(copy, 0, buf, off, len)
|
|
388
469
|
this.body = copy
|
|
389
470
|
}
|
|
390
471
|
this.length = len
|
|
391
|
-
return this
|
|
472
|
+
return Ok(this)
|
|
392
473
|
}
|
|
393
474
|
}
|
|
394
475
|
|
|
@@ -406,8 +487,15 @@ export const refuse = (_req: Request): Response => new Response(SERVE_REFUSE)
|
|
|
406
487
|
* used, and kept.
|
|
407
488
|
*/
|
|
408
489
|
class ServeState {
|
|
409
|
-
/** The request each slot holds: its connection's,
|
|
490
|
+
/** The request each slot holds: its connection's own parser state, lent to the task (`lend`) until the loop takes it back. */
|
|
410
491
|
reqs: HttpRequest[]
|
|
492
|
+
/**
|
|
493
|
+
* Per slot: the head and spans its connection's parser takes if the
|
|
494
|
+
* connection restarts while the slot's task still reads the old ones in
|
|
495
|
+
* place. Empty at first; then what an earlier such task was left with.
|
|
496
|
+
*/
|
|
497
|
+
spareHeads: u8[][]
|
|
498
|
+
spareSpans: i32[][]
|
|
411
499
|
/** The connection slot each request was read on, and that slot's generation then: where its answer goes. */
|
|
412
500
|
slots: i32[]
|
|
413
501
|
gens: i32[]
|
|
@@ -474,6 +562,8 @@ class ServeState {
|
|
|
474
562
|
|
|
475
563
|
constructor() {
|
|
476
564
|
this.reqs = []
|
|
565
|
+
this.spareHeads = []
|
|
566
|
+
this.spareSpans = []
|
|
477
567
|
this.slots = []
|
|
478
568
|
this.gens = []
|
|
479
569
|
this.busy = []
|
|
@@ -523,18 +613,22 @@ const serveFields = (st: ServeState, server: string): void => {
|
|
|
523
613
|
/**
|
|
524
614
|
* The last freed slot, making one when every slot is busy and `inFlight`
|
|
525
615
|
* allows; -1 when none. A slot made here starts on `rec`, the record its
|
|
526
|
-
* first request is
|
|
616
|
+
* first request is read from, which the caller sets again for each one.
|
|
527
617
|
*/
|
|
528
618
|
const serveFreeSlot = (st: ServeState, limits: ServeLimits, rec: HttpRequest): i32 => {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
619
|
+
const free: i32[] = st.free
|
|
620
|
+
const top: i32 = st.freeCount
|
|
621
|
+
if (top > 0 && top <= toI32(free.length)) {
|
|
622
|
+
st.freeCount = top - 1
|
|
623
|
+
return free[top - 1]
|
|
532
624
|
}
|
|
533
625
|
const n: i32 = toI32(st.busy.length)
|
|
534
626
|
if (n >= limits.inFlight) {
|
|
535
627
|
return -1
|
|
536
628
|
}
|
|
537
629
|
st.reqs.push(rec)
|
|
630
|
+
st.spareHeads.push(new Array<u8>(0))
|
|
631
|
+
st.spareSpans.push(new Array<i32>(0))
|
|
538
632
|
st.slots.push(toI32(0))
|
|
539
633
|
st.gens.push(toI32(0))
|
|
540
634
|
st.busy.push(false)
|
|
@@ -543,7 +637,7 @@ const serveFreeSlot = (st: ServeState, limits: ServeLimits, rec: HttpRequest): i
|
|
|
543
637
|
st.cancelled.push(false)
|
|
544
638
|
st.status.push(toI32(0))
|
|
545
639
|
st.lengths.push(toI32(0))
|
|
546
|
-
st.bodies.push(
|
|
640
|
+
st.bodies.push(serveU8s(limits.responseBytes))
|
|
547
641
|
st.types.push(new Array<u8>(SERVE_TYPE_BYTES))
|
|
548
642
|
st.typeLengths.push(toI32(0))
|
|
549
643
|
st.typeIndex.push(toI32(-1))
|
|
@@ -559,9 +653,15 @@ const serveFreeSlot = (st: ServeState, limits: ServeLimits, rec: HttpRequest): i
|
|
|
559
653
|
* runs to its end and writes an answer nobody may read.
|
|
560
654
|
*/
|
|
561
655
|
const serveCancel = (st: ServeState, k: i32): void => {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
656
|
+
const cancelled: boolean[] = st.cancelled
|
|
657
|
+
const tasks: i64[] = st.tasks
|
|
658
|
+
if (k < 0 || k >= toI32(cancelled.length) || k >= toI32(tasks.length)) {
|
|
659
|
+
return
|
|
660
|
+
}
|
|
661
|
+
const task: i64 = tasks[k]
|
|
662
|
+
if (!cancelled[k] && task !== 0) {
|
|
663
|
+
cancelled[k] = true
|
|
664
|
+
cancel(task)
|
|
565
665
|
}
|
|
566
666
|
}
|
|
567
667
|
|
|
@@ -575,34 +675,70 @@ const serveAnyBusy = (st: ServeState): boolean => {
|
|
|
575
675
|
return false
|
|
576
676
|
}
|
|
577
677
|
|
|
578
|
-
/** Sends slot `k`'s answer, as its task wrote it. */
|
|
678
|
+
/** Sends slot `k`'s answer, as its task wrote it; one it cannot send is counted in `stats.failed`. */
|
|
579
679
|
const serveAnswer = (c: ServeConns, st: ServeState, k: i32, stats: ServeStats): void => {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
const
|
|
680
|
+
// Every per-slot array grows together in `serveFreeSlot`, so `k`, a slot
|
|
681
|
+
// that array made, is inside each; the reads take them as locals to see it.
|
|
682
|
+
const slots: i32[] = st.slots
|
|
683
|
+
const gens: i32[] = st.gens
|
|
684
|
+
const statuses: i32[] = st.status
|
|
685
|
+
const lengths: i32[] = st.lengths
|
|
686
|
+
const typeLengths: i32[] = st.typeLengths
|
|
687
|
+
const typeIndex: i32[] = st.typeIndex
|
|
688
|
+
const bodies: u8[][] = st.bodies
|
|
689
|
+
const types: u8[][] = st.types
|
|
690
|
+
if (
|
|
691
|
+
k < 0 ||
|
|
692
|
+
k >= toI32(slots.length) ||
|
|
693
|
+
k >= toI32(gens.length) ||
|
|
694
|
+
k >= toI32(statuses.length) ||
|
|
695
|
+
k >= toI32(lengths.length) ||
|
|
696
|
+
k >= toI32(typeLengths.length) ||
|
|
697
|
+
k >= toI32(typeIndex.length) ||
|
|
698
|
+
k >= toI32(bodies.length) ||
|
|
699
|
+
k >= toI32(types.length)
|
|
700
|
+
) {
|
|
701
|
+
// `k` is no slot `serveFreeSlot` made, so it names no connection to
|
|
702
|
+
// answer: the answer is lost, and counted failed like one that cannot
|
|
703
|
+
// be framed below.
|
|
704
|
+
serveCountFailed(stats)
|
|
705
|
+
return
|
|
706
|
+
}
|
|
707
|
+
const s: i32 = slots[k]
|
|
708
|
+
const gen: i32 = gens[k]
|
|
709
|
+
const status: i32 = statuses[k]
|
|
583
710
|
if (status === SERVE_REFUSE) {
|
|
584
711
|
c.refuse(s, gen)
|
|
585
712
|
return
|
|
586
713
|
}
|
|
587
|
-
const length: i32 =
|
|
588
|
-
|
|
589
|
-
|
|
714
|
+
const length: i32 = lengths[k]
|
|
715
|
+
const typeLength: i32 = typeLengths[k]
|
|
716
|
+
if (status < 200 || status > 599 || length < 0 || typeLength < 0) {
|
|
717
|
+
serveCountFailed(stats)
|
|
590
718
|
c.problem(s, gen, SERVE_FAILED, c.failed)
|
|
591
719
|
return
|
|
592
720
|
}
|
|
593
|
-
if (
|
|
594
|
-
c.send(s, gen, status, st.plainNames, st.plainValues,
|
|
721
|
+
if (typeLength === 0) {
|
|
722
|
+
if (!c.send(s, gen, status, st.plainNames, st.plainValues, bodies[k], length)) {
|
|
723
|
+
serveCountFailed(stats)
|
|
724
|
+
}
|
|
595
725
|
return
|
|
596
726
|
}
|
|
597
|
-
const
|
|
598
|
-
|
|
599
|
-
|
|
727
|
+
const body: u8[] = bodies[k]
|
|
728
|
+
const known: i32 = typeIndex[k]
|
|
729
|
+
const type: i32 = known >= 0 ? known : serveType(st, types[k], typeLength)
|
|
730
|
+
const typeBytes: u8[][] = st.typeBytes
|
|
731
|
+
const typedValues: u8[][] = st.typedValues
|
|
732
|
+
const last: i32 = toI32(typedValues.length) - 1
|
|
733
|
+
if (type < 0 || type >= toI32(typeBytes.length) || last < 0) {
|
|
734
|
+
serveCountFailed(stats)
|
|
600
735
|
c.problem(s, gen, SERVE_FAILED, c.failed)
|
|
601
736
|
return
|
|
602
737
|
}
|
|
603
|
-
|
|
604
|
-
st.typedValues
|
|
605
|
-
|
|
738
|
+
typedValues[last] = typeBytes[type]
|
|
739
|
+
if (!c.send(s, gen, status, st.typedNames, typedValues, body, length)) {
|
|
740
|
+
serveCountFailed(stats)
|
|
741
|
+
}
|
|
606
742
|
}
|
|
607
743
|
|
|
608
744
|
/** The index in `st.typeBytes` of the bytes `type[0 .. len)`, adding them while there is room; -1 when full or when they hold a control byte. */
|
|
@@ -627,13 +763,13 @@ const serveType = (st: ServeState, type: u8[], len: i32): i32 => {
|
|
|
627
763
|
return -1
|
|
628
764
|
}
|
|
629
765
|
// A control byte other than a tab would make the carrier refuse the head,
|
|
630
|
-
// which `ServeConns.send`
|
|
766
|
+
// which `ServeConns.send` answers by closing the connection unanswered.
|
|
631
767
|
for (let j: i32 = 0; j < len && j < toI32(type.length); j++) {
|
|
632
768
|
if ((type[j] < 32 && type[j] !== 9) || type[j] === 127) {
|
|
633
769
|
return -1
|
|
634
770
|
}
|
|
635
771
|
}
|
|
636
|
-
const copy: u8[] =
|
|
772
|
+
const copy: u8[] = serveU8s(len)
|
|
637
773
|
for (let j: i32 = 0; j < toI32(copy.length) && j < toI32(type.length); j++) {
|
|
638
774
|
copy[j] = type[j]
|
|
639
775
|
}
|
|
@@ -642,17 +778,73 @@ const serveType = (st: ServeState, type: u8[], len: i32): i32 => {
|
|
|
642
778
|
return toI32(cache.length) - 1
|
|
643
779
|
}
|
|
644
780
|
|
|
781
|
+
/** A buffer as long as `[start, end)`, empty when `end` is not past `start`: the room a span's decoding never outgrows. */
|
|
782
|
+
const serveSpanBuffer = (start: i32, end: i32): u8[] => {
|
|
783
|
+
// In i64, where two i32s cannot overflow.
|
|
784
|
+
const n: i64 = toI64(end) - toI64(start)
|
|
785
|
+
if (n <= 0 || n > toI64(I32_MOST)) {
|
|
786
|
+
return new Array<u8>(0)
|
|
787
|
+
}
|
|
788
|
+
return serveU8s(toI32(n))
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/** The most an `i32` holds. */
|
|
792
|
+
const I32_MOST: i32 = 2147483647
|
|
793
|
+
|
|
794
|
+
/** `2 * maxParams + 2` slots for a router's spans, for a `maxParams` the router took (0 to 256). */
|
|
795
|
+
const serveParamSlots = (maxParams: i32): i32 => {
|
|
796
|
+
if (maxParams < 0 || maxParams > 65535) {
|
|
797
|
+
return 2
|
|
798
|
+
}
|
|
799
|
+
return 2 * maxParams + 2
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/** `a + b` on a clock reading, wrapping as the `u64` sum does rather than trapping. */
|
|
803
|
+
const serveLater = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
|
|
804
|
+
|
|
805
|
+
/** How long from `from` to `to` on a clock reading: 0 when `to` is not after it. */
|
|
806
|
+
const serveSince = (from: i64, to: i64): i64 => (to > from ? toI64(toU64(to) - toU64(from)) : toI64(0))
|
|
807
|
+
|
|
808
|
+
/** A zeroed array of `n` bytes, empty when `n` is not positive. */
|
|
809
|
+
const serveU8s = (n: i32): u8[] => {
|
|
810
|
+
if (n <= 0) {
|
|
811
|
+
return new Array<u8>(0)
|
|
812
|
+
}
|
|
813
|
+
// The guard rules out a negative length, but the check is the length's
|
|
814
|
+
// type's (an `i32`), not a guard's: this one site keeps it.
|
|
815
|
+
return new Array<u8>(n)
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/** A zeroed array of `n` integers, empty when `n` is not positive. */
|
|
819
|
+
const serveI32s = (n: i32): i32[] => {
|
|
820
|
+
if (n <= 0) {
|
|
821
|
+
return new Array<i32>(0)
|
|
822
|
+
}
|
|
823
|
+
// The guard rules out a negative length, but the check is the length's
|
|
824
|
+
// type's (an `i32`), not a guard's: this one site keeps it.
|
|
825
|
+
return new Array<i32>(n)
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/** An array of `n` falses, empty when `n` is not positive. */
|
|
829
|
+
const serveFlags = (n: i32): boolean[] => {
|
|
830
|
+
if (n <= 0) {
|
|
831
|
+
return new Array<boolean>(0)
|
|
832
|
+
}
|
|
833
|
+
// The guard rules out a negative length, but the check is the length's
|
|
834
|
+
// type's (an `i32`), not a guard's: this one site keeps it.
|
|
835
|
+
return new Array<boolean>(n)
|
|
836
|
+
}
|
|
837
|
+
|
|
645
838
|
/** A string's bytes: native strings are UTF-8, so its code units are its bytes. */
|
|
646
839
|
const serveBytes = (s: string): u8[] => {
|
|
647
|
-
const
|
|
648
|
-
const out: u8[] = new Array<u8>(n)
|
|
840
|
+
const out: u8[] = serveU8s(toI32(s.length))
|
|
649
841
|
for (let i: i32 = 0; i < toI32(out.length) && i < toI32(s.length); i++) {
|
|
650
842
|
out[i] = toU8(s.charCodeAt(i))
|
|
651
843
|
}
|
|
652
844
|
return out
|
|
653
845
|
}
|
|
654
846
|
|
|
655
|
-
/** One request's task:
|
|
847
|
+
/** One request's task: runs `handle` on the record it reads in place, writes the answer into slot `k` and wakes the loop. */
|
|
656
848
|
const serveTask = async (
|
|
657
849
|
handle: (req: Request) => Promise<Response>,
|
|
658
850
|
st: ServeState,
|
|
@@ -665,43 +857,7 @@ const serveTask = async (
|
|
|
665
857
|
} else if (st.deadline > 0) {
|
|
666
858
|
cancelAfter(self, st.deadline)
|
|
667
859
|
}
|
|
668
|
-
|
|
669
|
-
// shared request and the task's own would be refused, as it could store
|
|
670
|
-
// one into the other.
|
|
671
|
-
const src: HttpRequest = st.reqs[k]
|
|
672
|
-
const r: HttpRequest = new HttpRequest(-1)
|
|
673
|
-
const head: u8[] = new Array<u8>(src.headLength)
|
|
674
|
-
copyBytes(head, 0, src.head, 0, src.headLength)
|
|
675
|
-
const spans: i32[] = new Array<i32>(4 * src.headerCount)
|
|
676
|
-
for (let i: i32 = 0; i < toI32(spans.length) && i < toI32(src.spans.length); i++) {
|
|
677
|
-
spans[i] = src.spans[i]
|
|
678
|
-
}
|
|
679
|
-
// No parameters and no body keep the empty arrays the constructor made.
|
|
680
|
-
if (src.paramCount > 0) {
|
|
681
|
-
const params: i32[] = new Array<i32>(2 * src.paramCount)
|
|
682
|
-
for (let i: i32 = 0; i < toI32(params.length) && i < toI32(src.params.length); i++) {
|
|
683
|
-
params[i] = src.params[i]
|
|
684
|
-
}
|
|
685
|
-
r.params = params
|
|
686
|
-
}
|
|
687
|
-
if (src.bodyLength > 0) {
|
|
688
|
-
const body: u8[] = new Array<u8>(src.bodyLength)
|
|
689
|
-
copyBytes(body, 0, src.body, 0, src.bodyLength)
|
|
690
|
-
r.body = body
|
|
691
|
-
}
|
|
692
|
-
r.head = head
|
|
693
|
-
r.spans = spans
|
|
694
|
-
r.route = src.route
|
|
695
|
-
r.paramCount = src.paramCount
|
|
696
|
-
r.methodStart = src.methodStart
|
|
697
|
-
r.methodEnd = src.methodEnd
|
|
698
|
-
r.targetStart = src.targetStart
|
|
699
|
-
r.targetEnd = src.targetEnd
|
|
700
|
-
r.pathEnd = src.pathEnd
|
|
701
|
-
r.headLength = src.headLength
|
|
702
|
-
r.headerCount = src.headerCount
|
|
703
|
-
r.bodyLength = src.bodyLength
|
|
704
|
-
const res: Response = await handle(new Request(r))
|
|
860
|
+
const res: Response = await handle(new Request(lend(st.reqs[k])))
|
|
705
861
|
st.status[k] = res.status
|
|
706
862
|
const out: u8[] = st.bodies[k]
|
|
707
863
|
// A length the body cannot hold, negative or past either array, is the
|
|
@@ -775,19 +931,35 @@ const serveProblem = (status: i32, title: string): u8[] => {
|
|
|
775
931
|
w.key("status")
|
|
776
932
|
w.i32(status)
|
|
777
933
|
w.endObject()
|
|
778
|
-
const
|
|
779
|
-
|
|
934
|
+
const end: i32 = w.end
|
|
935
|
+
const out: u8[] = serveU8s(end)
|
|
936
|
+
copyBytes(out, 0, buf, 0, toI32(out.length))
|
|
780
937
|
return out
|
|
781
938
|
}
|
|
782
939
|
|
|
940
|
+
/** Counts one failed answer in `stats`, holding at the largest `i32` rather than wrapping. */
|
|
941
|
+
const serveCountFailed = (stats: ServeStats): void => {
|
|
942
|
+
const failed: i32 = stats.failed
|
|
943
|
+
if (failed < 2147483647) {
|
|
944
|
+
stats.failed = failed + 1
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
783
948
|
/** Whether `bytes[start .. end)` is the ASCII text `text`. */
|
|
784
949
|
const serveSpanIs = (bytes: u8[], start: i32, end: i32, text: string): boolean => {
|
|
785
950
|
const n: i32 = toI32(text.length)
|
|
786
|
-
if (
|
|
951
|
+
if (
|
|
952
|
+
start < 0 ||
|
|
953
|
+
start > 1073741823 ||
|
|
954
|
+
n > 1073741823 ||
|
|
955
|
+
end > toI32(bytes.length) ||
|
|
956
|
+
toI64(end) - toI64(start) !== toI64(n)
|
|
957
|
+
) {
|
|
787
958
|
return false
|
|
788
959
|
}
|
|
789
|
-
for (let k: i32 = 0; k < n; k++) {
|
|
790
|
-
|
|
960
|
+
for (let k: i32 = 0; k < n && k <= 1073741823; k++) {
|
|
961
|
+
const at: i32 = start + k
|
|
962
|
+
if (at < 0 || at >= toI32(bytes.length) || toI32(bytes[at]) !== toI32(text.charCodeAt(k))) {
|
|
791
963
|
return false
|
|
792
964
|
}
|
|
793
965
|
}
|
|
@@ -810,17 +982,21 @@ const serveAllowAppend = (value: u8[], bytes: u8[], start: i32, end: i32): void
|
|
|
810
982
|
/** A buffer size at least `need`, doubling from `have` (64 at least). */
|
|
811
983
|
const serveGrown = (have: i32, need: i32): i32 => {
|
|
812
984
|
let size: i32 = have < 64 ? 64 : have
|
|
813
|
-
while (size < need
|
|
985
|
+
while (size < need) {
|
|
986
|
+
// `size` starts at 64 and only doubles, so the low test never holds.
|
|
987
|
+
if (size < 0 || size > 1073741823) {
|
|
988
|
+
break
|
|
989
|
+
}
|
|
814
990
|
size = size * 2
|
|
815
991
|
}
|
|
816
992
|
return size < need ? need : size
|
|
817
993
|
}
|
|
818
994
|
|
|
819
|
-
/** One slot's exchange: what it needs after its task's
|
|
995
|
+
/** One slot's exchange: what it needs after its task's spawn, and its record until then. */
|
|
820
996
|
class ServeExchange {
|
|
821
|
-
/** The slot's connection: the carrier
|
|
997
|
+
/** The slot's connection as of its last drive: the carrier lends one to a busy slot, so `driveOn` sets it again. */
|
|
822
998
|
conn: Http1Connection
|
|
823
|
-
/** The record, from the head's parse to the task's
|
|
999
|
+
/** The record, from the head's parse to the task's spawn (`lendOut`); `ServeConns.none` otherwise. */
|
|
824
1000
|
rec: HttpRequest
|
|
825
1001
|
/** What the output had no room for yet, `pending[pendingStart .. pendingEnd)`; `noBytes` until the slot needs its own. */
|
|
826
1002
|
pending: u8[]
|
|
@@ -842,14 +1018,15 @@ class ServeExchange {
|
|
|
842
1018
|
|
|
843
1019
|
/**
|
|
844
1020
|
* The connections: an `Http1Server` driven straight from the loop. A slot
|
|
845
|
-
* holds an `HttpRequest` record only from the head's parse to
|
|
846
|
-
*
|
|
847
|
-
* record is held)
|
|
848
|
-
*
|
|
849
|
-
*
|
|
850
|
-
*
|
|
851
|
-
*
|
|
852
|
-
*
|
|
1021
|
+
* holds an `HttpRequest` record only from the head's parse to its task's
|
|
1022
|
+
* spawn: `begin` takes a spare one (building one only when every built
|
|
1023
|
+
* record is held), `lendOut` hands it to the task, which reads it in place,
|
|
1024
|
+
* and `giveBack` takes it back once the task ends; a 503, or a connection
|
|
1025
|
+
* closed first, gives it back at once. The record's head and spans are the
|
|
1026
|
+
* parser's own: the parser keeps them until the exchange's answer is sent,
|
|
1027
|
+
* and a connection that restarts while the task runs leaves them to the
|
|
1028
|
+
* record and takes the slot's spares instead (`lendParsed`). What an
|
|
1029
|
+
* exchange needs past the spawn — its state, its route for a 404 or 405, the answer's pending bytes — is
|
|
853
1030
|
* the slot's own, in a `ServeExchange`: a few dozen bytes, and the pending
|
|
854
1031
|
* array, which doubles from 64 bytes until it holds the most of an answer
|
|
855
1032
|
* the output could not take (under 2 × `responseBytes`, at most the power of
|
|
@@ -910,6 +1087,11 @@ class ServeConns {
|
|
|
910
1087
|
sent: i64 = 0
|
|
911
1088
|
listener: i32 = 0
|
|
912
1089
|
loop: i32 = 0
|
|
1090
|
+
/**
|
|
1091
|
+
* The ring's first slot. The ring's length is a power of two, so each step
|
|
1092
|
+
* is a sum worked in 64 bits and masked: wrapping and masking give the same
|
|
1093
|
+
* slot, and no step can overflow.
|
|
1094
|
+
*/
|
|
913
1095
|
queueStart: i32 = 0
|
|
914
1096
|
queueCount: i32 = 0
|
|
915
1097
|
owedCount: i32 = 0
|
|
@@ -935,14 +1117,16 @@ class ServeConns {
|
|
|
935
1117
|
this.ex = []
|
|
936
1118
|
this.none = new HttpRequest(-1)
|
|
937
1119
|
this.noBytes = []
|
|
938
|
-
|
|
939
|
-
this.
|
|
940
|
-
this.
|
|
941
|
-
this.
|
|
1120
|
+
const slots: i32 = limits.connections
|
|
1121
|
+
this.slotGen = serveI32s(slots)
|
|
1122
|
+
this.open = serveFlags(slots)
|
|
1123
|
+
this.owing = serveFlags(slots)
|
|
1124
|
+
this.owed = serveI32s(slots)
|
|
942
1125
|
this.queue = new Array<i32>(16)
|
|
943
1126
|
this.queueGen = new Array<i32>(16)
|
|
944
|
-
|
|
945
|
-
this.
|
|
1127
|
+
// Two per event, for the 256 one poll reports.
|
|
1128
|
+
this.readyFds = new Array<i32>(512)
|
|
1129
|
+
this.probe = serveI32s(serveParamSlots(router.maxParams))
|
|
946
1130
|
const contentType: u8[] = serveBytes("content-type")
|
|
947
1131
|
const problemType: u8[] = serveBytes("application/problem+json")
|
|
948
1132
|
this.problemNames = [contentType]
|
|
@@ -979,14 +1163,21 @@ class ServeConns {
|
|
|
979
1163
|
const now: i64 = monotonicNanos()
|
|
980
1164
|
this.turnAt = now
|
|
981
1165
|
let woken: boolean = false
|
|
982
|
-
|
|
983
|
-
|
|
1166
|
+
const ready: i32[] = this.readyFds
|
|
1167
|
+
// 256 events at most bound `k`, so `k + k` cannot overflow.
|
|
1168
|
+
for (let k: i32 = 0; k < n && k < 256; k++) {
|
|
1169
|
+
const at: i32 = k + k
|
|
1170
|
+
if (at < 0 || at >= toI32(ready.length) - 1) {
|
|
1171
|
+
break
|
|
1172
|
+
}
|
|
1173
|
+
const token: i32 = ready[at]
|
|
1174
|
+
const bits: i32 = ready[at + 1]
|
|
984
1175
|
if (token === SERVE_LISTENER) {
|
|
985
1176
|
this.acceptAll()
|
|
986
1177
|
} else if (token === SERVE_WAKE) {
|
|
987
1178
|
woken = true
|
|
988
1179
|
} else if (token >= 0 && this.server.holds(token)) {
|
|
989
|
-
if ((
|
|
1180
|
+
if ((bits & SERVE_READ_BITS) !== 0) {
|
|
990
1181
|
this.server.readable(token)
|
|
991
1182
|
} else {
|
|
992
1183
|
this.server.writable(token)
|
|
@@ -996,17 +1187,21 @@ class ServeConns {
|
|
|
996
1187
|
}
|
|
997
1188
|
if (this.server.expire(now) > 0) {
|
|
998
1189
|
// The carrier closed idle or late connections: forget them.
|
|
999
|
-
|
|
1000
|
-
|
|
1190
|
+
const open: boolean[] = this.open
|
|
1191
|
+
for (let s: i32 = 0; s < toI32(open.length); s++) {
|
|
1192
|
+
if (open[s] && !this.server.holds(s)) {
|
|
1001
1193
|
this.forget(s)
|
|
1002
1194
|
}
|
|
1003
1195
|
}
|
|
1004
1196
|
}
|
|
1005
1197
|
if (now >= this.bodyCheckAt) {
|
|
1006
|
-
this.bodyCheckAt = now
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1198
|
+
this.bodyCheckAt = serveLater(now, SERVE_BODY_SWEEP)
|
|
1199
|
+
const open: boolean[] = this.open
|
|
1200
|
+
const ex: ServeExchange[] = this.ex
|
|
1201
|
+
for (let s: i32 = 0; s < toI32(open.length) && s < toI32(ex.length); s++) {
|
|
1202
|
+
const e: ServeExchange = ex[s]
|
|
1203
|
+
if (open[s] && e.state === SERVE_READING && e.holds) {
|
|
1204
|
+
if (serveSince(e.rec.since, now) > SERVE_BODY_TIMEOUT) {
|
|
1010
1205
|
this.closeSlot(s)
|
|
1011
1206
|
}
|
|
1012
1207
|
}
|
|
@@ -1024,12 +1219,15 @@ class ServeConns {
|
|
|
1024
1219
|
if (this.acceptRestUntil === 0) {
|
|
1025
1220
|
pollRemove(this.loop, this.listener)
|
|
1026
1221
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1222
|
+
const open: boolean[] = this.open
|
|
1223
|
+
const ex: ServeExchange[] = this.ex
|
|
1224
|
+
for (let s: i32 = 0; s < toI32(open.length) && s < toI32(ex.length); s++) {
|
|
1225
|
+
const e: ServeExchange = ex[s]
|
|
1226
|
+
if (open[s] && this.server.holds(s)) {
|
|
1227
|
+
if (e.state === SERVE_IDLE && !e.conn.wantsWrite()) {
|
|
1030
1228
|
this.closeSlot(s)
|
|
1031
1229
|
} else {
|
|
1032
|
-
|
|
1230
|
+
e.conn.closeAfter = true
|
|
1033
1231
|
}
|
|
1034
1232
|
}
|
|
1035
1233
|
}
|
|
@@ -1049,17 +1247,24 @@ class ServeConns {
|
|
|
1049
1247
|
if (slot >= toI32(this.ex.length)) {
|
|
1050
1248
|
this.growSlots(slot)
|
|
1051
1249
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1250
|
+
// The carrier hands out fewer slots than `open` has, and `growSlots`
|
|
1251
|
+
// covers `slot`: anything else is a slot it cannot serve.
|
|
1252
|
+
const open: boolean[] = this.open
|
|
1253
|
+
const ex: ServeExchange[] = this.ex
|
|
1254
|
+
if (slot < toI32(open.length) && slot < toI32(ex.length)) {
|
|
1255
|
+
open[slot] = true
|
|
1256
|
+
const e: ServeExchange = ex[slot]
|
|
1257
|
+
e.state = SERVE_IDLE
|
|
1258
|
+
pollAdd(this.loop, this.server.fd(slot), H1_WANT_READ, slot)
|
|
1259
|
+
} else {
|
|
1260
|
+
this.server.close(slot)
|
|
1261
|
+
}
|
|
1057
1262
|
}
|
|
1058
1263
|
slot = this.server.accept()
|
|
1059
1264
|
}
|
|
1060
1265
|
if (slot !== SERVE_ACCEPT_NONE) {
|
|
1061
1266
|
pollRemove(this.loop, this.listener)
|
|
1062
|
-
this.acceptRestUntil = monotonicNanos()
|
|
1267
|
+
this.acceptRestUntil = serveLater(monotonicNanos(), SERVE_ACCEPT_REST)
|
|
1063
1268
|
}
|
|
1064
1269
|
}
|
|
1065
1270
|
|
|
@@ -1067,23 +1272,37 @@ class ServeConns {
|
|
|
1067
1272
|
flush(): void {
|
|
1068
1273
|
const n: i32 = this.owedCount
|
|
1069
1274
|
this.owedCount = 0
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1275
|
+
const owed: i32[] = this.owed
|
|
1276
|
+
for (let k: i32 = 0; k < n && k < toI32(owed.length); k++) {
|
|
1277
|
+
const slot: i32 = owed[k]
|
|
1278
|
+
const owing: boolean[] = this.owing
|
|
1279
|
+
if (slot >= 0 && slot < toI32(owing.length)) {
|
|
1280
|
+
owing[slot] = false
|
|
1281
|
+
}
|
|
1282
|
+
const open: boolean[] = this.open
|
|
1283
|
+
if (slot >= 0 && slot < toI32(open.length) && open[slot] && this.server.holds(slot)) {
|
|
1284
|
+
const ex: ServeExchange[] = this.ex
|
|
1285
|
+
if (slot < toI32(ex.length)) {
|
|
1286
|
+
this.settle(slot, ex[slot].conn)
|
|
1287
|
+
}
|
|
1075
1288
|
}
|
|
1076
1289
|
}
|
|
1077
1290
|
}
|
|
1078
1291
|
|
|
1079
1292
|
/** Runs slot `s`'s events until it needs bytes, then owes or makes its send. */
|
|
1080
1293
|
drive(s: i32): void {
|
|
1081
|
-
|
|
1294
|
+
const ex: ServeExchange[] = this.ex
|
|
1295
|
+
if (s >= 0 && s < toI32(ex.length)) {
|
|
1296
|
+
this.driveOn(s, ex[s])
|
|
1297
|
+
}
|
|
1082
1298
|
}
|
|
1083
1299
|
|
|
1084
1300
|
/** `drive` for a caller that holds slot `s`'s exchange `e`. */
|
|
1085
1301
|
driveOn(s: i32, e: ServeExchange): void {
|
|
1086
|
-
|
|
1302
|
+
// A slot's connection is lent to it while it is busy and given back when
|
|
1303
|
+
// it rests, so the exchange takes the one it holds now.
|
|
1304
|
+
const conn: Http1Connection = this.server.connection(s)
|
|
1305
|
+
e.conn = conn
|
|
1087
1306
|
let event: i32 = this.server.next(s)
|
|
1088
1307
|
while (event !== H1_NEED_MORE && event !== H1_ERROR) {
|
|
1089
1308
|
if (event === H1_REQUEST) {
|
|
@@ -1091,9 +1310,15 @@ class ServeConns {
|
|
|
1091
1310
|
} else if (event === H1_BODY && e.state === SERVE_READING && e.holds) {
|
|
1092
1311
|
const r: HttpRequest = e.rec
|
|
1093
1312
|
const n: i32 = conn.dataLength
|
|
1094
|
-
const
|
|
1313
|
+
const sum: i64 = toI64(r.bodyLength) + toI64(n)
|
|
1314
|
+
if (sum > toI64(I32_MOST)) {
|
|
1315
|
+
// The carrier caps a body at `limits.bodyBytes`, an `i32`.
|
|
1316
|
+
this.closeSlot(s)
|
|
1317
|
+
return
|
|
1318
|
+
}
|
|
1319
|
+
const need: i32 = toI32(sum)
|
|
1095
1320
|
if (toI32(r.body.length) < need) {
|
|
1096
|
-
const grown: u8[] =
|
|
1321
|
+
const grown: u8[] = serveU8s(serveGrown(toI32(r.body.length), need))
|
|
1097
1322
|
copyBytes(grown, 0, r.body, 0, r.bodyLength)
|
|
1098
1323
|
r.body = grown
|
|
1099
1324
|
}
|
|
@@ -1124,12 +1349,19 @@ class ServeConns {
|
|
|
1124
1349
|
e.state !== SERVE_PARKED
|
|
1125
1350
|
) {
|
|
1126
1351
|
// The send waits for the turn's end, to go out with the answers after it.
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1352
|
+
const owing: boolean[] = this.owing
|
|
1353
|
+
const owed: i32[] = this.owed
|
|
1354
|
+
const at: i32 = this.owedCount
|
|
1355
|
+
if (s >= 0 && s < toI32(owing.length) && owing[s]) {
|
|
1356
|
+
return
|
|
1357
|
+
}
|
|
1358
|
+
// A slot is owed once, so `owed` has room for every slot `owing` has.
|
|
1359
|
+
if (s >= 0 && s < toI32(owing.length) && at >= 0 && at < toI32(owed.length)) {
|
|
1360
|
+
owing[s] = true
|
|
1361
|
+
owed[at] = s
|
|
1362
|
+
this.owedCount = at + 1
|
|
1363
|
+
return
|
|
1131
1364
|
}
|
|
1132
|
-
return
|
|
1133
1365
|
}
|
|
1134
1366
|
this.settle(s, conn)
|
|
1135
1367
|
}
|
|
@@ -1137,10 +1369,14 @@ class ServeConns {
|
|
|
1137
1369
|
/** Sends slot `s`'s output and rearms it; a parked exchange is queued once there is room. */
|
|
1138
1370
|
settle(s: i32, conn: Http1Connection): void {
|
|
1139
1371
|
if (conn.wantsWrite()) {
|
|
1140
|
-
this.sent
|
|
1372
|
+
this.sent = serveLater(this.sent, toI64(1))
|
|
1141
1373
|
}
|
|
1142
1374
|
const wants: i32 = this.server.flush(s)
|
|
1143
|
-
const
|
|
1375
|
+
const ex: ServeExchange[] = this.ex
|
|
1376
|
+
if (s < 0 || s >= toI32(ex.length)) {
|
|
1377
|
+
return
|
|
1378
|
+
}
|
|
1379
|
+
const e: ServeExchange = ex[s]
|
|
1144
1380
|
if ((wants & H1_DONE) !== 0) {
|
|
1145
1381
|
this.closeSlot(s)
|
|
1146
1382
|
return
|
|
@@ -1159,7 +1395,7 @@ class ServeConns {
|
|
|
1159
1395
|
|
|
1160
1396
|
/** Whether the output is at most half full: room for an answer's head behind what is there. */
|
|
1161
1397
|
roomy(conn: Http1Connection): boolean {
|
|
1162
|
-
return (conn.outputEnd - conn.outputStart)
|
|
1398
|
+
return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(this.outputSize) / toI64(2)
|
|
1163
1399
|
}
|
|
1164
1400
|
|
|
1165
1401
|
/**
|
|
@@ -1169,17 +1405,18 @@ class ServeConns {
|
|
|
1169
1405
|
* A server sized for many connections pays for the ones it has had.
|
|
1170
1406
|
*/
|
|
1171
1407
|
growSlots(slot: i32): void {
|
|
1172
|
-
// An exchange's connection is set
|
|
1173
|
-
// names the
|
|
1174
|
-
const conn: Http1Connection = this.server.connection(
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1408
|
+
// An exchange's connection is set each time its slot is driven; until
|
|
1409
|
+
// then it names the pool's first, which this does not lend the slot.
|
|
1410
|
+
const conn: Http1Connection = this.server.connection(toI32(-1))
|
|
1411
|
+
const have: i32 = toI32(this.ex.length)
|
|
1412
|
+
let n: i32 = this.slotLimit
|
|
1413
|
+
if (have <= 512) {
|
|
1414
|
+
n = have < 32 ? 64 : have + have
|
|
1415
|
+
if (n > this.slotLimit) {
|
|
1416
|
+
n = this.slotLimit
|
|
1417
|
+
}
|
|
1181
1418
|
}
|
|
1182
|
-
if (n <= slot) {
|
|
1419
|
+
if (n <= slot && slot < 2147483647) {
|
|
1183
1420
|
n = slot + 1
|
|
1184
1421
|
}
|
|
1185
1422
|
while (toI32(this.ex.length) < n) {
|
|
@@ -1189,30 +1426,87 @@ class ServeConns {
|
|
|
1189
1426
|
|
|
1190
1427
|
/** Takes a spare record, building one only when every built record is held. */
|
|
1191
1428
|
acquire(): HttpRequest {
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1429
|
+
const spare: HttpRequest[] = this.spare
|
|
1430
|
+
const top: i32 = this.spareCount
|
|
1431
|
+
if (top > 0 && top <= toI32(spare.length)) {
|
|
1432
|
+
this.spareCount = top - 1
|
|
1433
|
+
return spare[top - 1]
|
|
1195
1434
|
}
|
|
1196
1435
|
const r: HttpRequest = new HttpRequest(-1)
|
|
1197
|
-
r.params =
|
|
1198
|
-
|
|
1436
|
+
r.params = serveI32s(serveParamSlots(this.router.maxParams))
|
|
1437
|
+
spare.push(r)
|
|
1438
|
+
this.spareCount = 0
|
|
1199
1439
|
return r
|
|
1200
1440
|
}
|
|
1201
1441
|
|
|
1202
|
-
/**
|
|
1442
|
+
/** Keeps `r` as a spare for `acquire`. */
|
|
1443
|
+
keepSpare(r: HttpRequest): void {
|
|
1444
|
+
const spare: HttpRequest[] = this.spare
|
|
1445
|
+
const at: i32 = this.spareCount
|
|
1446
|
+
// Every record was made by `acquire`, which keeps it in `spare`, so the
|
|
1447
|
+
// ones given back fit: the push only covers a count that went wrong.
|
|
1448
|
+
if (at >= 0 && at < toI32(spare.length)) {
|
|
1449
|
+
spare[at] = r
|
|
1450
|
+
this.spareCount = at + 1
|
|
1451
|
+
} else {
|
|
1452
|
+
spare.push(r)
|
|
1453
|
+
this.spareCount = toI32(spare.length)
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
/** Gives slot `s`'s record back, if it holds one: nobody will read it. */
|
|
1203
1458
|
release(s: i32): void {
|
|
1204
|
-
const
|
|
1459
|
+
const ex: ServeExchange[] = this.ex
|
|
1460
|
+
if (s < 0 || s >= toI32(ex.length)) {
|
|
1461
|
+
return
|
|
1462
|
+
}
|
|
1463
|
+
const e: ServeExchange = ex[s]
|
|
1205
1464
|
if (e.holds) {
|
|
1206
1465
|
e.holds = false
|
|
1207
|
-
this.
|
|
1208
|
-
this.spareCount += 1
|
|
1466
|
+
this.keepSpare(e.rec)
|
|
1209
1467
|
e.rec = this.none
|
|
1210
1468
|
}
|
|
1211
1469
|
}
|
|
1212
1470
|
|
|
1213
|
-
/**
|
|
1214
|
-
|
|
1215
|
-
|
|
1471
|
+
/**
|
|
1472
|
+
* Hands slot `s`'s record to a task that reads it in place: the slot holds
|
|
1473
|
+
* it no more, and its connection lends the parser's head and spans, which
|
|
1474
|
+
* the record points at, until `giveBack`. A restart meanwhile takes `head`
|
|
1475
|
+
* and `spans` for the next peer and leaves the record its own.
|
|
1476
|
+
*
|
|
1477
|
+
* @returns the record, or null when `s` is no slot or holds no record:
|
|
1478
|
+
* `take` gives only a slot `begin` routed, so null is this module's own
|
|
1479
|
+
* fault, and the caller answers it 500
|
|
1480
|
+
*/
|
|
1481
|
+
lendOut(s: i32, head: u8[], spans: i32[]): HttpRequest | null {
|
|
1482
|
+
const ex: ServeExchange[] = this.ex
|
|
1483
|
+
if (s < 0 || s >= toI32(ex.length)) {
|
|
1484
|
+
return null
|
|
1485
|
+
}
|
|
1486
|
+
const e: ServeExchange = ex[s]
|
|
1487
|
+
if (!e.holds) {
|
|
1488
|
+
return null
|
|
1489
|
+
}
|
|
1490
|
+
const r: HttpRequest = e.rec
|
|
1491
|
+
e.holds = false
|
|
1492
|
+
e.rec = this.none
|
|
1493
|
+
e.conn.lendParsed(head, spans)
|
|
1494
|
+
return r
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
/**
|
|
1498
|
+
* Takes back record `r` that `lendOut` gave for slot `s` at generation
|
|
1499
|
+
* `gen`, once its task has ended, into the spares.
|
|
1500
|
+
*
|
|
1501
|
+
* @returns true when the connection restarted meanwhile: `r`'s head and spans are then nobody else's, the spares for the next `lendOut`
|
|
1502
|
+
*/
|
|
1503
|
+
giveBack(r: HttpRequest, s: i32, gen: i32): boolean {
|
|
1504
|
+
this.keepSpare(r)
|
|
1505
|
+
if (!this.live(s, gen)) {
|
|
1506
|
+
return true
|
|
1507
|
+
}
|
|
1508
|
+
const ex: ServeExchange[] = this.ex
|
|
1509
|
+
return s < 0 || s >= toI32(ex.length) || !ex[s].conn.unlendParsed()
|
|
1216
1510
|
}
|
|
1217
1511
|
|
|
1218
1512
|
/** Points a record for slot `s` at the head the parser read, and routes it. */
|
|
@@ -1221,7 +1515,16 @@ class ServeConns {
|
|
|
1221
1515
|
// A head read during the drain is the connection's last.
|
|
1222
1516
|
conn.closeAfter = true
|
|
1223
1517
|
}
|
|
1224
|
-
|
|
1518
|
+
// `s` is a slot the server made, inside both per-slot arrays; a slot
|
|
1519
|
+
// outside them holds no connection, so there is nothing to route.
|
|
1520
|
+
const ex: ServeExchange[] = this.ex
|
|
1521
|
+
const gens: i32[] = this.slotGen
|
|
1522
|
+
if (s < 0 || s >= toI32(ex.length) || s >= toI32(gens.length)) {
|
|
1523
|
+
return
|
|
1524
|
+
}
|
|
1525
|
+
// Read before `acquire`, a call after which the prover no longer knows `gens`'s length.
|
|
1526
|
+
const gen: i32 = gens[s]
|
|
1527
|
+
const e: ServeExchange = ex[s]
|
|
1225
1528
|
if (!e.holds) {
|
|
1226
1529
|
e.rec = this.acquire()
|
|
1227
1530
|
e.holds = true
|
|
@@ -1238,7 +1541,7 @@ class ServeConns {
|
|
|
1238
1541
|
r.targetEnd = p.targetEnd
|
|
1239
1542
|
r.bodyLength = 0
|
|
1240
1543
|
r.slot = s
|
|
1241
|
-
r.gen =
|
|
1544
|
+
r.gen = gen
|
|
1242
1545
|
r.since = this.turnAt
|
|
1243
1546
|
e.state = SERVE_READING
|
|
1244
1547
|
e.pendingStart = 0
|
|
@@ -1255,11 +1558,13 @@ class ServeConns {
|
|
|
1255
1558
|
)
|
|
1256
1559
|
if (route === ROUTER_METHOD_NOT_ALLOWED && r.methodIs("HEAD")) {
|
|
1257
1560
|
const get: i32 = router.methodIndex("GET")
|
|
1258
|
-
|
|
1561
|
+
const starts: i32[] = router.methodStart
|
|
1562
|
+
const ends: i32[] = router.methodEnd
|
|
1563
|
+
if (get >= 0 && get < toI32(starts.length) && get < toI32(ends.length)) {
|
|
1259
1564
|
const viaGet: i32 = router.match(
|
|
1260
1565
|
router.methodBytes,
|
|
1261
|
-
|
|
1262
|
-
|
|
1566
|
+
starts[get],
|
|
1567
|
+
ends[get],
|
|
1263
1568
|
r.head,
|
|
1264
1569
|
r.targetStart,
|
|
1265
1570
|
r.targetEnd,
|
|
@@ -1279,55 +1584,108 @@ class ServeConns {
|
|
|
1279
1584
|
|
|
1280
1585
|
/** Queues slot `s`'s whole exchange for a task. */
|
|
1281
1586
|
enqueue(s: i32): void {
|
|
1282
|
-
|
|
1587
|
+
const ex: ServeExchange[] = this.ex
|
|
1588
|
+
const gens: i32[] = this.slotGen
|
|
1589
|
+
if (s < 0 || s >= toI32(ex.length) || s >= toI32(gens.length)) {
|
|
1590
|
+
return
|
|
1591
|
+
}
|
|
1592
|
+
ex[s].state = SERVE_QUEUED
|
|
1593
|
+
const gen: i32 = gens[s]
|
|
1283
1594
|
const size: i32 = toI32(this.queue.length)
|
|
1284
|
-
if (this.queueCount
|
|
1285
|
-
|
|
1286
|
-
|
|
1595
|
+
if (this.queueCount >= size) {
|
|
1596
|
+
// The queue holds each slot once, so it never needs more than 2^30.
|
|
1597
|
+
if (size < 1 || size > 1073741823) {
|
|
1598
|
+
this.closeSlot(s)
|
|
1599
|
+
return
|
|
1600
|
+
}
|
|
1601
|
+
const queue: i32[] = this.queue
|
|
1602
|
+
const queueGen: i32[] = this.queueGen
|
|
1603
|
+
const grown: i32[] = serveI32s(size + size)
|
|
1604
|
+
const grownGen: i32[] = serveI32s(size + size)
|
|
1605
|
+
const start: i32 = this.queueStart
|
|
1287
1606
|
for (let k: i32 = 0; k < size && k < toI32(grown.length) && k < toI32(grownGen.length); k++) {
|
|
1288
|
-
const from: i32 = (
|
|
1289
|
-
|
|
1290
|
-
|
|
1607
|
+
const from: i32 = toI32(toI64(start) + toI64(k)) & (size - 1)
|
|
1608
|
+
if (from >= 0 && from < toI32(queue.length) && from < toI32(queueGen.length)) {
|
|
1609
|
+
grown[k] = queue[from]
|
|
1610
|
+
grownGen[k] = queueGen[from]
|
|
1611
|
+
}
|
|
1291
1612
|
}
|
|
1292
1613
|
this.queue = grown
|
|
1293
1614
|
this.queueGen = grownGen
|
|
1294
1615
|
this.queueStart = 0
|
|
1295
1616
|
}
|
|
1296
|
-
const
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
this.
|
|
1617
|
+
const queue: i32[] = this.queue
|
|
1618
|
+
const queueGen: i32[] = this.queueGen
|
|
1619
|
+
const count: i32 = this.queueCount
|
|
1620
|
+
const at: i32 = toI32(toI64(this.queueStart) + toI64(count)) & (toI32(queue.length) - 1)
|
|
1621
|
+
if (
|
|
1622
|
+
at >= 0 &&
|
|
1623
|
+
at < toI32(queue.length) &&
|
|
1624
|
+
at < toI32(queueGen.length) &&
|
|
1625
|
+
count >= 0 &&
|
|
1626
|
+
count < I32_MOST
|
|
1627
|
+
) {
|
|
1628
|
+
queue[at] = s
|
|
1629
|
+
queueGen[at] = gen
|
|
1630
|
+
this.queueCount = count + 1
|
|
1631
|
+
}
|
|
1300
1632
|
}
|
|
1301
1633
|
|
|
1302
1634
|
/** Whether a queued exchange is ready, dropping those whose connection closed. */
|
|
1303
1635
|
ready(): boolean {
|
|
1304
1636
|
while (this.queueCount > 0) {
|
|
1305
|
-
|
|
1637
|
+
const queue: i32[] = this.queue
|
|
1638
|
+
const queueGen: i32[] = this.queueGen
|
|
1639
|
+
const start: i32 = this.queueStart
|
|
1640
|
+
if (start < 0 || start >= toI32(queue.length) || start >= toI32(queueGen.length)) {
|
|
1641
|
+
return false
|
|
1642
|
+
}
|
|
1643
|
+
if (this.live(queue[start], queueGen[start])) {
|
|
1306
1644
|
return true
|
|
1307
1645
|
}
|
|
1308
|
-
this.queueStart = (
|
|
1309
|
-
this.queueCount
|
|
1646
|
+
this.queueStart = toI32(toI64(start) + toI64(1)) & (toI32(queue.length) - 1)
|
|
1647
|
+
const count: i32 = this.queueCount
|
|
1648
|
+
this.queueCount = count > 0 ? count - 1 : 0
|
|
1310
1649
|
}
|
|
1311
1650
|
return false
|
|
1312
1651
|
}
|
|
1313
1652
|
|
|
1314
|
-
/**
|
|
1653
|
+
/**
|
|
1654
|
+
* The next queued exchange's slot, now held; call after `ready`.
|
|
1655
|
+
*
|
|
1656
|
+
* @returns the slot, or -1 when nothing is queued or the queue's next entry is no slot the server made; the caller stops taking then
|
|
1657
|
+
*/
|
|
1315
1658
|
take(): i32 {
|
|
1316
|
-
const
|
|
1317
|
-
|
|
1318
|
-
this.queueCount
|
|
1319
|
-
|
|
1659
|
+
const queue: i32[] = this.queue
|
|
1660
|
+
const start: i32 = this.queueStart
|
|
1661
|
+
const count: i32 = this.queueCount
|
|
1662
|
+
if (count <= 0 || start < 0 || start >= toI32(queue.length)) {
|
|
1663
|
+
return -1
|
|
1664
|
+
}
|
|
1665
|
+
const s: i32 = queue[start]
|
|
1666
|
+
this.queueStart = toI32(toI64(start) + toI64(1)) & (toI32(queue.length) - 1)
|
|
1667
|
+
this.queueCount = count - 1
|
|
1668
|
+
const ex: ServeExchange[] = this.ex
|
|
1669
|
+
if (s < 0 || s >= toI32(ex.length)) {
|
|
1670
|
+
return -1
|
|
1671
|
+
}
|
|
1672
|
+
ex[s].state = SERVE_HELD
|
|
1320
1673
|
return s
|
|
1321
1674
|
}
|
|
1322
1675
|
|
|
1323
1676
|
/** Whether slot `s` still holds the connection it had at generation `gen`. */
|
|
1324
1677
|
live(s: i32, gen: i32): boolean {
|
|
1325
|
-
|
|
1678
|
+
const gens: i32[] = this.slotGen
|
|
1679
|
+
return s >= 0 && s < toI32(gens.length) && gens[s] === gen && this.server.holds(s)
|
|
1326
1680
|
}
|
|
1327
1681
|
|
|
1328
1682
|
/** Whether nobody waits for the answer of slot `s`'s exchange at generation `gen`. */
|
|
1329
1683
|
hungUp(s: i32, gen: i32): boolean {
|
|
1330
|
-
|
|
1684
|
+
if (!this.live(s, gen)) {
|
|
1685
|
+
return true
|
|
1686
|
+
}
|
|
1687
|
+
const ex: ServeExchange[] = this.ex
|
|
1688
|
+
return s < 0 || s >= toI32(ex.length) || ex[s].conn.ended()
|
|
1331
1689
|
}
|
|
1332
1690
|
|
|
1333
1691
|
closeSlot(s: i32): void {
|
|
@@ -1337,9 +1695,24 @@ class ServeConns {
|
|
|
1337
1695
|
|
|
1338
1696
|
/** Marks slot `s` closed: its exchange is nobody's and its generation moves on. */
|
|
1339
1697
|
forget(s: i32): void {
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1698
|
+
const open: boolean[] = this.open
|
|
1699
|
+
if (s >= 0 && s < toI32(open.length)) {
|
|
1700
|
+
open[s] = false
|
|
1701
|
+
}
|
|
1702
|
+
const gens: i32[] = this.slotGen
|
|
1703
|
+
if (s >= 0 && s < toI32(gens.length)) {
|
|
1704
|
+
const gen: i32 = gens[s]
|
|
1705
|
+
// A generation only tells this slot's turns apart, so after the largest it starts again at 0.
|
|
1706
|
+
if (gen < 2147483647) {
|
|
1707
|
+
gens[s] = gen + 1
|
|
1708
|
+
} else {
|
|
1709
|
+
gens[s] = 0
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
const ex: ServeExchange[] = this.ex
|
|
1713
|
+
if (s >= 0 && s < toI32(ex.length)) {
|
|
1714
|
+
ex[s].state = SERVE_IDLE
|
|
1715
|
+
}
|
|
1343
1716
|
this.release(s)
|
|
1344
1717
|
}
|
|
1345
1718
|
|
|
@@ -1347,13 +1720,19 @@ class ServeConns {
|
|
|
1347
1720
|
* Answers slot `s`'s held exchange at generation `gen`, unless its
|
|
1348
1721
|
* connection closed: the head, `len` bytes of `body` unless the status has
|
|
1349
1722
|
* none, the end. What the output cannot take waits in the slot's
|
|
1350
|
-
* `pending` for `H1_WRITE`.
|
|
1723
|
+
* `pending` for `H1_WRITE`. A head or body the carrier refuses (a head
|
|
1724
|
+
* larger than the output, say, from a long `ServeLimits.server`) cannot
|
|
1725
|
+
* be answered any more: the connection is closed, as when the output has
|
|
1726
|
+
* no room for the head.
|
|
1727
|
+
*
|
|
1728
|
+
* @returns false when the carrier refused the answer and the connection was closed
|
|
1351
1729
|
*/
|
|
1352
|
-
send(s: i32, gen: i32, status: i32, names: u8[][], values: u8[][], body: u8[], len: i32):
|
|
1353
|
-
|
|
1354
|
-
|
|
1730
|
+
send(s: i32, gen: i32, status: i32, names: u8[][], values: u8[][], body: u8[], len: i32): boolean {
|
|
1731
|
+
const ex: ServeExchange[] = this.ex
|
|
1732
|
+
if (!this.live(s, gen) || s < 0 || s >= toI32(ex.length)) {
|
|
1733
|
+
return true
|
|
1355
1734
|
}
|
|
1356
|
-
const e: ServeExchange =
|
|
1735
|
+
const e: ServeExchange = ex[s]
|
|
1357
1736
|
const conn: Http1Connection = e.conn
|
|
1358
1737
|
const bodiless: boolean = status === 204 || status === 304
|
|
1359
1738
|
const sent: i32 = conn.respond(status, names, values, bodiless ? HTTP1_NO_BODY : len)
|
|
@@ -1361,12 +1740,11 @@ class ServeConns {
|
|
|
1361
1740
|
// The output still holds more of the answer before this one than a
|
|
1362
1741
|
// head can fit behind: ending the connection is the one answer left.
|
|
1363
1742
|
this.closeSlot(s)
|
|
1364
|
-
return
|
|
1743
|
+
return true
|
|
1365
1744
|
}
|
|
1366
1745
|
if (sent !== 0) {
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
)
|
|
1746
|
+
this.closeSlot(s)
|
|
1747
|
+
return false
|
|
1370
1748
|
}
|
|
1371
1749
|
if (len > 0 && !bodiless) {
|
|
1372
1750
|
const start: i32 = 0
|
|
@@ -1374,12 +1752,14 @@ class ServeConns {
|
|
|
1374
1752
|
if (taken === H1_AGAIN) {
|
|
1375
1753
|
taken = 0
|
|
1376
1754
|
} else if (taken < 0) {
|
|
1377
|
-
|
|
1755
|
+
// The head is out: the rest of the answer cannot follow it.
|
|
1756
|
+
this.closeSlot(s)
|
|
1757
|
+
return false
|
|
1378
1758
|
}
|
|
1379
1759
|
if (taken < len) {
|
|
1380
1760
|
const rest: i32 = len - taken
|
|
1381
1761
|
if (toI32(e.pending.length) < rest) {
|
|
1382
|
-
e.pending =
|
|
1762
|
+
e.pending = serveU8s(serveGrown(toI32(e.pending.length), rest))
|
|
1383
1763
|
}
|
|
1384
1764
|
copyBytes(e.pending, 0, body, taken, rest)
|
|
1385
1765
|
e.pendingEnd = rest
|
|
@@ -1387,19 +1767,28 @@ class ServeConns {
|
|
|
1387
1767
|
}
|
|
1388
1768
|
this.finish(e)
|
|
1389
1769
|
this.driveOn(s, e)
|
|
1770
|
+
return true
|
|
1390
1771
|
}
|
|
1391
1772
|
|
|
1392
1773
|
/** Writes what the answer still has pending and ends it; the exchange is over once both are done. */
|
|
1393
1774
|
finish(e: ServeExchange): void {
|
|
1394
1775
|
const conn: Http1Connection = e.conn
|
|
1395
1776
|
e.state = SERVE_FLUSHING
|
|
1396
|
-
const
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1777
|
+
const start: i32 = e.pendingStart
|
|
1778
|
+
const end: i32 = e.pendingEnd
|
|
1779
|
+
if (start >= 0 && end >= 0 && end > start) {
|
|
1780
|
+
const left: i32 = end - start
|
|
1781
|
+
const taken: i32 = conn.write(e.pending, start, left)
|
|
1782
|
+
let at: i32 = start
|
|
1783
|
+
if (taken > 0 && taken <= left && left >= 0) {
|
|
1784
|
+
// What is still unwritten; never negative, as `taken <= left`.
|
|
1785
|
+
const rest: i32 = left - taken
|
|
1786
|
+
if (rest >= 0) {
|
|
1787
|
+
at = end - rest
|
|
1788
|
+
e.pendingStart = at
|
|
1789
|
+
}
|
|
1401
1790
|
}
|
|
1402
|
-
if (
|
|
1791
|
+
if (at < end) {
|
|
1403
1792
|
return
|
|
1404
1793
|
}
|
|
1405
1794
|
e.pendingStart = 0
|
|
@@ -1413,14 +1802,21 @@ class ServeConns {
|
|
|
1413
1802
|
|
|
1414
1803
|
/** Answers slot `s`'s exchange at `gen` with 404, or 405 and the methods its path has. */
|
|
1415
1804
|
refuse(s: i32, gen: i32): void {
|
|
1416
|
-
|
|
1805
|
+
const ex: ServeExchange[] = this.ex
|
|
1806
|
+
if (!this.live(s, gen) || s < 0 || s >= toI32(ex.length)) {
|
|
1417
1807
|
return
|
|
1418
1808
|
}
|
|
1419
|
-
|
|
1809
|
+
const e: ServeExchange = ex[s]
|
|
1810
|
+
if (e.route === ROUTER_METHOD_NOT_ALLOWED) {
|
|
1420
1811
|
// The parser still keeps this exchange's head: it reads the next only
|
|
1421
1812
|
// once this one is answered.
|
|
1422
|
-
const p =
|
|
1423
|
-
|
|
1813
|
+
const p = e.conn.parser
|
|
1814
|
+
const allow: u8[] = this.allowFor(p.head, p.targetStart, p.targetEnd)
|
|
1815
|
+
// The constructor makes `allowValues` two long: content-type, allow.
|
|
1816
|
+
const values: u8[][] = this.allowValues
|
|
1817
|
+
if (toI32(values.length) > 1) {
|
|
1818
|
+
values[1] = allow
|
|
1819
|
+
}
|
|
1424
1820
|
this.send(
|
|
1425
1821
|
s,
|
|
1426
1822
|
gen,
|
|
@@ -1451,13 +1847,15 @@ class ServeConns {
|
|
|
1451
1847
|
/** The `allow` value for the path `bytes[targetStart .. targetEnd)`: the methods with a route for it, and HEAD where GET has one. */
|
|
1452
1848
|
allowFor(bytes: u8[], targetStart: i32, targetEnd: i32): u8[] {
|
|
1453
1849
|
const router: Router = this.router
|
|
1454
|
-
const
|
|
1850
|
+
const starts: i32[] = router.methodStart
|
|
1851
|
+
const ends: i32[] = router.methodEnd
|
|
1852
|
+
const methods: i32 = toI32(starts.length) < toI32(ends.length) ? toI32(starts.length) : toI32(ends.length)
|
|
1455
1853
|
let mask: i64 = 0
|
|
1456
|
-
for (let m: i32 = 0; m <
|
|
1854
|
+
for (let m: i32 = 0; m < toI32(starts.length) && m < toI32(ends.length); m++) {
|
|
1457
1855
|
const route: i32 = router.match(
|
|
1458
1856
|
router.methodBytes,
|
|
1459
|
-
|
|
1460
|
-
|
|
1857
|
+
starts[m],
|
|
1858
|
+
ends[m],
|
|
1461
1859
|
bytes,
|
|
1462
1860
|
targetStart,
|
|
1463
1861
|
targetEnd,
|
|
@@ -1467,18 +1865,20 @@ class ServeConns {
|
|
|
1467
1865
|
mask = mask | (toI64(1) << toI64(m))
|
|
1468
1866
|
}
|
|
1469
1867
|
}
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1868
|
+
const masks: i64[] = this.allowMasks
|
|
1869
|
+
const known: u8[][] = this.allowBytes
|
|
1870
|
+
for (let k: i32 = 0; k < toI32(masks.length) && k < toI32(known.length); k++) {
|
|
1871
|
+
if (masks[k] === mask) {
|
|
1872
|
+
return known[k]
|
|
1473
1873
|
}
|
|
1474
1874
|
}
|
|
1475
1875
|
let get: boolean = false
|
|
1476
1876
|
let head: boolean = false
|
|
1477
1877
|
const value: u8[] = []
|
|
1478
|
-
for (let m: i32 = 0; m < methods; m++) {
|
|
1878
|
+
for (let m: i32 = 0; m < methods && m < toI32(starts.length) && m < toI32(ends.length); m++) {
|
|
1479
1879
|
if ((mask & (toI64(1) << toI64(m))) !== toI64(0)) {
|
|
1480
|
-
const start: i32 =
|
|
1481
|
-
const end: i32 =
|
|
1880
|
+
const start: i32 = starts[m]
|
|
1881
|
+
const end: i32 = ends[m]
|
|
1482
1882
|
get = get || serveSpanIs(router.methodBytes, start, end, "GET")
|
|
1483
1883
|
head = head || serveSpanIs(router.methodBytes, start, end, "HEAD")
|
|
1484
1884
|
serveAllowAppend(value, router.methodBytes, start, end)
|
|
@@ -1555,6 +1955,11 @@ export const serveOn = async (
|
|
|
1555
1955
|
for (let i: i32 = 0; i < st.finishedCount && i < toI32(st.finished.length); i++) {
|
|
1556
1956
|
const k: i32 = st.finished[i]
|
|
1557
1957
|
serveAnswer(c, st, k, stats)
|
|
1958
|
+
const r: HttpRequest = st.reqs[k]
|
|
1959
|
+
if (c.giveBack(r, st.slots[k], st.gens[k])) {
|
|
1960
|
+
st.spareHeads[k] = r.head
|
|
1961
|
+
st.spareSpans[k] = r.spans
|
|
1962
|
+
}
|
|
1558
1963
|
st.busy[k] = false
|
|
1559
1964
|
st.done[k] = false
|
|
1560
1965
|
st.cancelled[k] = false
|
|
@@ -1567,7 +1972,11 @@ export const serveOn = async (
|
|
|
1567
1972
|
st.finishedCount = 0
|
|
1568
1973
|
while (c.ready()) {
|
|
1569
1974
|
const s: i32 = c.take()
|
|
1570
|
-
|
|
1975
|
+
if (s < 0) {
|
|
1976
|
+
break
|
|
1977
|
+
}
|
|
1978
|
+
// `lendOut` sets the slot's record for each request, the first too.
|
|
1979
|
+
const k: i32 = serveFreeSlot(st, limits, c.none)
|
|
1571
1980
|
moved = true
|
|
1572
1981
|
if (k < 0) {
|
|
1573
1982
|
c.release(s)
|
|
@@ -1576,7 +1985,21 @@ export const serveOn = async (
|
|
|
1576
1985
|
stats.answered += 1
|
|
1577
1986
|
continue
|
|
1578
1987
|
}
|
|
1579
|
-
st.
|
|
1988
|
+
const lent: HttpRequest | null = c.lendOut(s, st.spareHeads[k], st.spareSpans[k])
|
|
1989
|
+
if (lent === null) {
|
|
1990
|
+
// No record to hand a task: the slot goes back unused, and the
|
|
1991
|
+
// client gets a 500 rather than a task reading a record nobody made.
|
|
1992
|
+
const free: i32[] = st.free
|
|
1993
|
+
if (st.freeCount >= 0 && st.freeCount < toI32(free.length)) {
|
|
1994
|
+
free[st.freeCount] = k
|
|
1995
|
+
st.freeCount += 1
|
|
1996
|
+
}
|
|
1997
|
+
c.problem(s, c.slotGen[s], SERVE_FAILED, c.failed)
|
|
1998
|
+
serveCountFailed(stats)
|
|
1999
|
+
stats.answered += 1
|
|
2000
|
+
continue
|
|
2001
|
+
}
|
|
2002
|
+
st.reqs[k] = lent
|
|
1580
2003
|
st.slots[k] = s
|
|
1581
2004
|
st.gens[k] = c.slotGen[s]
|
|
1582
2005
|
st.busy[k] = true
|
|
@@ -1588,11 +2011,9 @@ export const serveOn = async (
|
|
|
1588
2011
|
st.running[st.runningCount] = k
|
|
1589
2012
|
st.runningCount += 1
|
|
1590
2013
|
}
|
|
1591
|
-
//
|
|
1592
|
-
//
|
|
1593
|
-
// keep-alive connection holds none.
|
|
2014
|
+
// The task reads the record in place, and its connection's parser
|
|
2015
|
+
// head and spans with it: `giveBack` takes them back once it ends.
|
|
1594
2016
|
spawn(serveTask(handle, st, k))
|
|
1595
|
-
c.release(s)
|
|
1596
2017
|
}
|
|
1597
2018
|
}
|
|
1598
2019
|
// A task still running whose client hung up, or any once the server is
|