@amritk/nish-x86_64-linux 0.44.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 +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- 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 +84 -61
- 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 +3 -4
- package/std/crypto/sha1.ts +107 -65
- 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/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 +1275 -217
- 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 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- 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 +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- 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 +50 -18
- 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 +225 -124
- package/std/net/quic-listener.ts +197 -89
- 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 +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- 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 +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/serve.ts
CHANGED
|
@@ -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
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,16 +1247,24 @@ class ServeConns {
|
|
|
1049
1247
|
if (slot >= toI32(this.ex.length)) {
|
|
1050
1248
|
this.growSlots(slot)
|
|
1051
1249
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
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
|
+
}
|
|
1056
1262
|
}
|
|
1057
1263
|
slot = this.server.accept()
|
|
1058
1264
|
}
|
|
1059
1265
|
if (slot !== SERVE_ACCEPT_NONE) {
|
|
1060
1266
|
pollRemove(this.loop, this.listener)
|
|
1061
|
-
this.acceptRestUntil = monotonicNanos()
|
|
1267
|
+
this.acceptRestUntil = serveLater(monotonicNanos(), SERVE_ACCEPT_REST)
|
|
1062
1268
|
}
|
|
1063
1269
|
}
|
|
1064
1270
|
|
|
@@ -1066,18 +1272,29 @@ class ServeConns {
|
|
|
1066
1272
|
flush(): void {
|
|
1067
1273
|
const n: i32 = this.owedCount
|
|
1068
1274
|
this.owedCount = 0
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
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
|
+
}
|
|
1074
1288
|
}
|
|
1075
1289
|
}
|
|
1076
1290
|
}
|
|
1077
1291
|
|
|
1078
1292
|
/** Runs slot `s`'s events until it needs bytes, then owes or makes its send. */
|
|
1079
1293
|
drive(s: i32): void {
|
|
1080
|
-
|
|
1294
|
+
const ex: ServeExchange[] = this.ex
|
|
1295
|
+
if (s >= 0 && s < toI32(ex.length)) {
|
|
1296
|
+
this.driveOn(s, ex[s])
|
|
1297
|
+
}
|
|
1081
1298
|
}
|
|
1082
1299
|
|
|
1083
1300
|
/** `drive` for a caller that holds slot `s`'s exchange `e`. */
|
|
@@ -1093,9 +1310,15 @@ class ServeConns {
|
|
|
1093
1310
|
} else if (event === H1_BODY && e.state === SERVE_READING && e.holds) {
|
|
1094
1311
|
const r: HttpRequest = e.rec
|
|
1095
1312
|
const n: i32 = conn.dataLength
|
|
1096
|
-
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)
|
|
1097
1320
|
if (toI32(r.body.length) < need) {
|
|
1098
|
-
const grown: u8[] =
|
|
1321
|
+
const grown: u8[] = serveU8s(serveGrown(toI32(r.body.length), need))
|
|
1099
1322
|
copyBytes(grown, 0, r.body, 0, r.bodyLength)
|
|
1100
1323
|
r.body = grown
|
|
1101
1324
|
}
|
|
@@ -1126,12 +1349,19 @@ class ServeConns {
|
|
|
1126
1349
|
e.state !== SERVE_PARKED
|
|
1127
1350
|
) {
|
|
1128
1351
|
// The send waits for the turn's end, to go out with the answers after it.
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
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
|
|
1133
1364
|
}
|
|
1134
|
-
return
|
|
1135
1365
|
}
|
|
1136
1366
|
this.settle(s, conn)
|
|
1137
1367
|
}
|
|
@@ -1139,10 +1369,14 @@ class ServeConns {
|
|
|
1139
1369
|
/** Sends slot `s`'s output and rearms it; a parked exchange is queued once there is room. */
|
|
1140
1370
|
settle(s: i32, conn: Http1Connection): void {
|
|
1141
1371
|
if (conn.wantsWrite()) {
|
|
1142
|
-
this.sent
|
|
1372
|
+
this.sent = serveLater(this.sent, toI64(1))
|
|
1143
1373
|
}
|
|
1144
1374
|
const wants: i32 = this.server.flush(s)
|
|
1145
|
-
const
|
|
1375
|
+
const ex: ServeExchange[] = this.ex
|
|
1376
|
+
if (s < 0 || s >= toI32(ex.length)) {
|
|
1377
|
+
return
|
|
1378
|
+
}
|
|
1379
|
+
const e: ServeExchange = ex[s]
|
|
1146
1380
|
if ((wants & H1_DONE) !== 0) {
|
|
1147
1381
|
this.closeSlot(s)
|
|
1148
1382
|
return
|
|
@@ -1161,7 +1395,7 @@ class ServeConns {
|
|
|
1161
1395
|
|
|
1162
1396
|
/** Whether the output is at most half full: room for an answer's head behind what is there. */
|
|
1163
1397
|
roomy(conn: Http1Connection): boolean {
|
|
1164
|
-
return (conn.outputEnd - conn.outputStart)
|
|
1398
|
+
return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(this.outputSize) / toI64(2)
|
|
1165
1399
|
}
|
|
1166
1400
|
|
|
1167
1401
|
/**
|
|
@@ -1174,14 +1408,15 @@ class ServeConns {
|
|
|
1174
1408
|
// An exchange's connection is set each time its slot is driven; until
|
|
1175
1409
|
// then it names the pool's first, which this does not lend the slot.
|
|
1176
1410
|
const conn: Http1Connection = this.server.connection(toI32(-1))
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
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
|
+
}
|
|
1183
1418
|
}
|
|
1184
|
-
if (n <= slot) {
|
|
1419
|
+
if (n <= slot && slot < 2147483647) {
|
|
1185
1420
|
n = slot + 1
|
|
1186
1421
|
}
|
|
1187
1422
|
while (toI32(this.ex.length) < n) {
|
|
@@ -1191,30 +1426,87 @@ class ServeConns {
|
|
|
1191
1426
|
|
|
1192
1427
|
/** Takes a spare record, building one only when every built record is held. */
|
|
1193
1428
|
acquire(): HttpRequest {
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
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]
|
|
1197
1434
|
}
|
|
1198
1435
|
const r: HttpRequest = new HttpRequest(-1)
|
|
1199
|
-
r.params =
|
|
1200
|
-
|
|
1436
|
+
r.params = serveI32s(serveParamSlots(this.router.maxParams))
|
|
1437
|
+
spare.push(r)
|
|
1438
|
+
this.spareCount = 0
|
|
1201
1439
|
return r
|
|
1202
1440
|
}
|
|
1203
1441
|
|
|
1204
|
-
/**
|
|
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. */
|
|
1205
1458
|
release(s: i32): void {
|
|
1206
|
-
const
|
|
1459
|
+
const ex: ServeExchange[] = this.ex
|
|
1460
|
+
if (s < 0 || s >= toI32(ex.length)) {
|
|
1461
|
+
return
|
|
1462
|
+
}
|
|
1463
|
+
const e: ServeExchange = ex[s]
|
|
1207
1464
|
if (e.holds) {
|
|
1208
1465
|
e.holds = false
|
|
1209
|
-
this.
|
|
1210
|
-
this.spareCount += 1
|
|
1466
|
+
this.keepSpare(e.rec)
|
|
1211
1467
|
e.rec = this.none
|
|
1212
1468
|
}
|
|
1213
1469
|
}
|
|
1214
1470
|
|
|
1215
|
-
/**
|
|
1216
|
-
|
|
1217
|
-
|
|
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()
|
|
1218
1510
|
}
|
|
1219
1511
|
|
|
1220
1512
|
/** Points a record for slot `s` at the head the parser read, and routes it. */
|
|
@@ -1223,7 +1515,16 @@ class ServeConns {
|
|
|
1223
1515
|
// A head read during the drain is the connection's last.
|
|
1224
1516
|
conn.closeAfter = true
|
|
1225
1517
|
}
|
|
1226
|
-
|
|
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]
|
|
1227
1528
|
if (!e.holds) {
|
|
1228
1529
|
e.rec = this.acquire()
|
|
1229
1530
|
e.holds = true
|
|
@@ -1240,7 +1541,7 @@ class ServeConns {
|
|
|
1240
1541
|
r.targetEnd = p.targetEnd
|
|
1241
1542
|
r.bodyLength = 0
|
|
1242
1543
|
r.slot = s
|
|
1243
|
-
r.gen =
|
|
1544
|
+
r.gen = gen
|
|
1244
1545
|
r.since = this.turnAt
|
|
1245
1546
|
e.state = SERVE_READING
|
|
1246
1547
|
e.pendingStart = 0
|
|
@@ -1257,11 +1558,13 @@ class ServeConns {
|
|
|
1257
1558
|
)
|
|
1258
1559
|
if (route === ROUTER_METHOD_NOT_ALLOWED && r.methodIs("HEAD")) {
|
|
1259
1560
|
const get: i32 = router.methodIndex("GET")
|
|
1260
|
-
|
|
1561
|
+
const starts: i32[] = router.methodStart
|
|
1562
|
+
const ends: i32[] = router.methodEnd
|
|
1563
|
+
if (get >= 0 && get < toI32(starts.length) && get < toI32(ends.length)) {
|
|
1261
1564
|
const viaGet: i32 = router.match(
|
|
1262
1565
|
router.methodBytes,
|
|
1263
|
-
|
|
1264
|
-
|
|
1566
|
+
starts[get],
|
|
1567
|
+
ends[get],
|
|
1265
1568
|
r.head,
|
|
1266
1569
|
r.targetStart,
|
|
1267
1570
|
r.targetEnd,
|
|
@@ -1281,55 +1584,108 @@ class ServeConns {
|
|
|
1281
1584
|
|
|
1282
1585
|
/** Queues slot `s`'s whole exchange for a task. */
|
|
1283
1586
|
enqueue(s: i32): void {
|
|
1284
|
-
|
|
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]
|
|
1285
1594
|
const size: i32 = toI32(this.queue.length)
|
|
1286
|
-
if (this.queueCount
|
|
1287
|
-
|
|
1288
|
-
|
|
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
|
|
1289
1606
|
for (let k: i32 = 0; k < size && k < toI32(grown.length) && k < toI32(grownGen.length); k++) {
|
|
1290
|
-
const from: i32 = (
|
|
1291
|
-
|
|
1292
|
-
|
|
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
|
+
}
|
|
1293
1612
|
}
|
|
1294
1613
|
this.queue = grown
|
|
1295
1614
|
this.queueGen = grownGen
|
|
1296
1615
|
this.queueStart = 0
|
|
1297
1616
|
}
|
|
1298
|
-
const
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
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
|
+
}
|
|
1302
1632
|
}
|
|
1303
1633
|
|
|
1304
1634
|
/** Whether a queued exchange is ready, dropping those whose connection closed. */
|
|
1305
1635
|
ready(): boolean {
|
|
1306
1636
|
while (this.queueCount > 0) {
|
|
1307
|
-
|
|
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])) {
|
|
1308
1644
|
return true
|
|
1309
1645
|
}
|
|
1310
|
-
this.queueStart = (
|
|
1311
|
-
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
|
|
1312
1649
|
}
|
|
1313
1650
|
return false
|
|
1314
1651
|
}
|
|
1315
1652
|
|
|
1316
|
-
/**
|
|
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
|
+
*/
|
|
1317
1658
|
take(): i32 {
|
|
1318
|
-
const
|
|
1319
|
-
|
|
1320
|
-
this.queueCount
|
|
1321
|
-
|
|
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
|
|
1322
1673
|
return s
|
|
1323
1674
|
}
|
|
1324
1675
|
|
|
1325
1676
|
/** Whether slot `s` still holds the connection it had at generation `gen`. */
|
|
1326
1677
|
live(s: i32, gen: i32): boolean {
|
|
1327
|
-
|
|
1678
|
+
const gens: i32[] = this.slotGen
|
|
1679
|
+
return s >= 0 && s < toI32(gens.length) && gens[s] === gen && this.server.holds(s)
|
|
1328
1680
|
}
|
|
1329
1681
|
|
|
1330
1682
|
/** Whether nobody waits for the answer of slot `s`'s exchange at generation `gen`. */
|
|
1331
1683
|
hungUp(s: i32, gen: i32): boolean {
|
|
1332
|
-
|
|
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()
|
|
1333
1689
|
}
|
|
1334
1690
|
|
|
1335
1691
|
closeSlot(s: i32): void {
|
|
@@ -1339,9 +1695,24 @@ class ServeConns {
|
|
|
1339
1695
|
|
|
1340
1696
|
/** Marks slot `s` closed: its exchange is nobody's and its generation moves on. */
|
|
1341
1697
|
forget(s: i32): void {
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
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
|
+
}
|
|
1345
1716
|
this.release(s)
|
|
1346
1717
|
}
|
|
1347
1718
|
|
|
@@ -1349,13 +1720,19 @@ class ServeConns {
|
|
|
1349
1720
|
* Answers slot `s`'s held exchange at generation `gen`, unless its
|
|
1350
1721
|
* connection closed: the head, `len` bytes of `body` unless the status has
|
|
1351
1722
|
* none, the end. What the output cannot take waits in the slot's
|
|
1352
|
-
* `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
|
|
1353
1729
|
*/
|
|
1354
|
-
send(s: i32, gen: i32, status: i32, names: u8[][], values: u8[][], body: u8[], len: i32):
|
|
1355
|
-
|
|
1356
|
-
|
|
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
|
|
1357
1734
|
}
|
|
1358
|
-
const e: ServeExchange =
|
|
1735
|
+
const e: ServeExchange = ex[s]
|
|
1359
1736
|
const conn: Http1Connection = e.conn
|
|
1360
1737
|
const bodiless: boolean = status === 204 || status === 304
|
|
1361
1738
|
const sent: i32 = conn.respond(status, names, values, bodiless ? HTTP1_NO_BODY : len)
|
|
@@ -1363,12 +1740,11 @@ class ServeConns {
|
|
|
1363
1740
|
// The output still holds more of the answer before this one than a
|
|
1364
1741
|
// head can fit behind: ending the connection is the one answer left.
|
|
1365
1742
|
this.closeSlot(s)
|
|
1366
|
-
return
|
|
1743
|
+
return true
|
|
1367
1744
|
}
|
|
1368
1745
|
if (sent !== 0) {
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
)
|
|
1746
|
+
this.closeSlot(s)
|
|
1747
|
+
return false
|
|
1372
1748
|
}
|
|
1373
1749
|
if (len > 0 && !bodiless) {
|
|
1374
1750
|
const start: i32 = 0
|
|
@@ -1376,12 +1752,14 @@ class ServeConns {
|
|
|
1376
1752
|
if (taken === H1_AGAIN) {
|
|
1377
1753
|
taken = 0
|
|
1378
1754
|
} else if (taken < 0) {
|
|
1379
|
-
|
|
1755
|
+
// The head is out: the rest of the answer cannot follow it.
|
|
1756
|
+
this.closeSlot(s)
|
|
1757
|
+
return false
|
|
1380
1758
|
}
|
|
1381
1759
|
if (taken < len) {
|
|
1382
1760
|
const rest: i32 = len - taken
|
|
1383
1761
|
if (toI32(e.pending.length) < rest) {
|
|
1384
|
-
e.pending =
|
|
1762
|
+
e.pending = serveU8s(serveGrown(toI32(e.pending.length), rest))
|
|
1385
1763
|
}
|
|
1386
1764
|
copyBytes(e.pending, 0, body, taken, rest)
|
|
1387
1765
|
e.pendingEnd = rest
|
|
@@ -1389,19 +1767,28 @@ class ServeConns {
|
|
|
1389
1767
|
}
|
|
1390
1768
|
this.finish(e)
|
|
1391
1769
|
this.driveOn(s, e)
|
|
1770
|
+
return true
|
|
1392
1771
|
}
|
|
1393
1772
|
|
|
1394
1773
|
/** Writes what the answer still has pending and ends it; the exchange is over once both are done. */
|
|
1395
1774
|
finish(e: ServeExchange): void {
|
|
1396
1775
|
const conn: Http1Connection = e.conn
|
|
1397
1776
|
e.state = SERVE_FLUSHING
|
|
1398
|
-
const
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
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
|
+
}
|
|
1403
1790
|
}
|
|
1404
|
-
if (
|
|
1791
|
+
if (at < end) {
|
|
1405
1792
|
return
|
|
1406
1793
|
}
|
|
1407
1794
|
e.pendingStart = 0
|
|
@@ -1415,14 +1802,21 @@ class ServeConns {
|
|
|
1415
1802
|
|
|
1416
1803
|
/** Answers slot `s`'s exchange at `gen` with 404, or 405 and the methods its path has. */
|
|
1417
1804
|
refuse(s: i32, gen: i32): void {
|
|
1418
|
-
|
|
1805
|
+
const ex: ServeExchange[] = this.ex
|
|
1806
|
+
if (!this.live(s, gen) || s < 0 || s >= toI32(ex.length)) {
|
|
1419
1807
|
return
|
|
1420
1808
|
}
|
|
1421
|
-
|
|
1809
|
+
const e: ServeExchange = ex[s]
|
|
1810
|
+
if (e.route === ROUTER_METHOD_NOT_ALLOWED) {
|
|
1422
1811
|
// The parser still keeps this exchange's head: it reads the next only
|
|
1423
1812
|
// once this one is answered.
|
|
1424
|
-
const p =
|
|
1425
|
-
|
|
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
|
+
}
|
|
1426
1820
|
this.send(
|
|
1427
1821
|
s,
|
|
1428
1822
|
gen,
|
|
@@ -1453,13 +1847,15 @@ class ServeConns {
|
|
|
1453
1847
|
/** The `allow` value for the path `bytes[targetStart .. targetEnd)`: the methods with a route for it, and HEAD where GET has one. */
|
|
1454
1848
|
allowFor(bytes: u8[], targetStart: i32, targetEnd: i32): u8[] {
|
|
1455
1849
|
const router: Router = this.router
|
|
1456
|
-
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)
|
|
1457
1853
|
let mask: i64 = 0
|
|
1458
|
-
for (let m: i32 = 0; m <
|
|
1854
|
+
for (let m: i32 = 0; m < toI32(starts.length) && m < toI32(ends.length); m++) {
|
|
1459
1855
|
const route: i32 = router.match(
|
|
1460
1856
|
router.methodBytes,
|
|
1461
|
-
|
|
1462
|
-
|
|
1857
|
+
starts[m],
|
|
1858
|
+
ends[m],
|
|
1463
1859
|
bytes,
|
|
1464
1860
|
targetStart,
|
|
1465
1861
|
targetEnd,
|
|
@@ -1469,18 +1865,20 @@ class ServeConns {
|
|
|
1469
1865
|
mask = mask | (toI64(1) << toI64(m))
|
|
1470
1866
|
}
|
|
1471
1867
|
}
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
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]
|
|
1475
1873
|
}
|
|
1476
1874
|
}
|
|
1477
1875
|
let get: boolean = false
|
|
1478
1876
|
let head: boolean = false
|
|
1479
1877
|
const value: u8[] = []
|
|
1480
|
-
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++) {
|
|
1481
1879
|
if ((mask & (toI64(1) << toI64(m))) !== toI64(0)) {
|
|
1482
|
-
const start: i32 =
|
|
1483
|
-
const end: i32 =
|
|
1880
|
+
const start: i32 = starts[m]
|
|
1881
|
+
const end: i32 = ends[m]
|
|
1484
1882
|
get = get || serveSpanIs(router.methodBytes, start, end, "GET")
|
|
1485
1883
|
head = head || serveSpanIs(router.methodBytes, start, end, "HEAD")
|
|
1486
1884
|
serveAllowAppend(value, router.methodBytes, start, end)
|
|
@@ -1557,6 +1955,11 @@ export const serveOn = async (
|
|
|
1557
1955
|
for (let i: i32 = 0; i < st.finishedCount && i < toI32(st.finished.length); i++) {
|
|
1558
1956
|
const k: i32 = st.finished[i]
|
|
1559
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
|
+
}
|
|
1560
1963
|
st.busy[k] = false
|
|
1561
1964
|
st.done[k] = false
|
|
1562
1965
|
st.cancelled[k] = false
|
|
@@ -1569,7 +1972,11 @@ export const serveOn = async (
|
|
|
1569
1972
|
st.finishedCount = 0
|
|
1570
1973
|
while (c.ready()) {
|
|
1571
1974
|
const s: i32 = c.take()
|
|
1572
|
-
|
|
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)
|
|
1573
1980
|
moved = true
|
|
1574
1981
|
if (k < 0) {
|
|
1575
1982
|
c.release(s)
|
|
@@ -1578,7 +1985,21 @@ export const serveOn = async (
|
|
|
1578
1985
|
stats.answered += 1
|
|
1579
1986
|
continue
|
|
1580
1987
|
}
|
|
1581
|
-
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
|
|
1582
2003
|
st.slots[k] = s
|
|
1583
2004
|
st.gens[k] = c.slotGen[s]
|
|
1584
2005
|
st.busy[k] = true
|
|
@@ -1590,11 +2011,9 @@ export const serveOn = async (
|
|
|
1590
2011
|
st.running[st.runningCount] = k
|
|
1591
2012
|
st.runningCount += 1
|
|
1592
2013
|
}
|
|
1593
|
-
//
|
|
1594
|
-
//
|
|
1595
|
-
// 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.
|
|
1596
2016
|
spawn(serveTask(handle, st, k))
|
|
1597
|
-
c.release(s)
|
|
1598
2017
|
}
|
|
1599
2018
|
}
|
|
1600
2019
|
// A task still running whose client hung up, or any once the server is
|