@amritk/nish-x86_64-linux 0.44.0 → 0.46.0
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/INSTALL.md +3 -1
- 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 +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- 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 +1315 -222
- 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/sqlite.ts +171 -0
- 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/handler.ts
CHANGED
|
@@ -65,11 +65,23 @@
|
|
|
65
65
|
* buffer grows to the body it holds, so a handler holds at most its slots
|
|
66
66
|
* times the cap in bodies. `docs/security/handler.md` is the record.
|
|
67
67
|
*
|
|
68
|
+
* **A compressed body is decoded under its own cap.** A body sent with one
|
|
69
|
+
* `Content-Encoding` of `gzip`, `x-gzip` or `deflate` is decoded before the
|
|
70
|
+
* program takes it, into at most `HandlerLimits.maxDecoded` bytes (one MiB):
|
|
71
|
+
* `req.body` holds the decoded bytes and `req.decodedFrom` the coding they
|
|
72
|
+
* came in. A body that decodes past the cap — a decompression bomb — is
|
|
73
|
+
* answered 413 by the handler, and one that does not decode 400; the program
|
|
74
|
+
* never takes either. Any other coding, or two codings stacked, reaches the
|
|
75
|
+
* program as it arrived, with `decodedFrom` `HTTP_CODING_IDENTITY`.
|
|
76
|
+
*
|
|
68
77
|
* Misuse — a status outside 200 to 599, a reserved field (`Content-Length`,
|
|
69
78
|
* `Transfer-Encoding`, `Connection`, `Upgrade`, `Keep-Alive`), more body than
|
|
70
79
|
* the length said, a second `start` — panics: it is a bug in the program,
|
|
71
|
-
* never something a peer can cause.
|
|
72
|
-
*
|
|
80
|
+
* never something a peer can cause. Each answering method has a `try` twin
|
|
81
|
+
* (`tryWrite`, `tryEnd`, `trySend`, ...) that answers the same misuse as an
|
|
82
|
+
* `Err` instead, for a program built without panics; the class doc says
|
|
83
|
+
* what each refusal leaves of the request. What a peer can cause answers a
|
|
84
|
+
* code the program may ignore, so no answer needs its result bound.
|
|
73
85
|
*
|
|
74
86
|
* Private helpers share the importing program's flat symbol namespace
|
|
75
87
|
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
@@ -112,15 +124,35 @@ import {
|
|
|
112
124
|
pollModify,
|
|
113
125
|
pollRemove,
|
|
114
126
|
pollWait,
|
|
127
|
+
pollWaitBatch,
|
|
115
128
|
tcpListenFlags,
|
|
116
129
|
} from "nish:net"
|
|
117
|
-
|
|
130
|
+
import {
|
|
131
|
+
HTTP_CODING_DEFLATE,
|
|
132
|
+
HTTP_CODING_GZIP,
|
|
133
|
+
HTTP_CODING_IDENTITY,
|
|
134
|
+
HTTP_CODING_NONE,
|
|
135
|
+
HTTP_DECODE_TOO_LARGE,
|
|
136
|
+
HttpBodyDecoder,
|
|
137
|
+
httpContentEncoding,
|
|
138
|
+
} from "nish/net/http-compress"
|
|
139
|
+
|
|
140
|
+
/** `HandlerLimits.maxWaitMs`' most: a second, the interval `bodyTimeout` is checked at. */
|
|
141
|
+
const HANDLER_MAX_WAIT_MS: i32 = 1000
|
|
142
|
+
/** A core's answer when the call broke its contract: `refusal` says how, and the public method panics with it. */
|
|
143
|
+
const HANDLER_REFUSED: i32 = -24
|
|
118
144
|
/** An answer sent, or queued to be sent. */
|
|
119
145
|
export const HANDLER_SENT: i32 = 0
|
|
120
146
|
/** An answer for a request whose connection closed first: nothing was sent. */
|
|
121
147
|
export const HANDLER_DROPPED: i32 = -21
|
|
122
148
|
/** `HttpRequest.query` and `header`: no such name. */
|
|
123
149
|
export const HANDLER_ABSENT: i32 = -20
|
|
150
|
+
/** `before` and `after`: every step bound to the request's route has run, and the request is still the program's to answer. */
|
|
151
|
+
export const HANDLER_NEXT: i32 = -22
|
|
152
|
+
/** `before` and `after`: a step answered the request, so the route body must not. */
|
|
153
|
+
export const HANDLER_ANSWERED: i32 = -23
|
|
154
|
+
/** The most fields `field` adds to one answer. */
|
|
155
|
+
export const HANDLER_MAX_ADDED: i32 = 16
|
|
124
156
|
|
|
125
157
|
/** The most descriptors `watch` takes beside the handler's own. */
|
|
126
158
|
export const HANDLER_MAX_WATCHED: i32 = 16
|
|
@@ -193,6 +225,24 @@ export class HandlerLimits {
|
|
|
193
225
|
jsonBytes: i32 = 65536
|
|
194
226
|
/** The most readiness events one `wait` takes. */
|
|
195
227
|
maxEvents: i32 = 256
|
|
228
|
+
/**
|
|
229
|
+
* The events one `wait` gathers before it returns, once the first is in:
|
|
230
|
+
* above 1, a busy ring server takes its connections' events in fewer,
|
|
231
|
+
* larger batches (`pollWaitBatch`), waiting up to `maxWaitMs` for them. A
|
|
232
|
+
* batch is at most `maxEvents` and at most 64 events, whatever this says.
|
|
233
|
+
* It acts only on a listener `listen` opened with `ring` set, on Linux;
|
|
234
|
+
* elsewhere, and at 1 (the default), `wait` is `pollWait` as before. At
|
|
235
|
+
* least 1.
|
|
236
|
+
*/
|
|
237
|
+
minEvents: i32 = 1
|
|
238
|
+
/**
|
|
239
|
+
* The most milliseconds one `wait` waits for `minEvents`, after its first
|
|
240
|
+
* event and on top of its own timeout. It is latency added to the batch's
|
|
241
|
+
* first request, and a timer or other descriptor the handler watches may
|
|
242
|
+
* be heard up to this late. 0 (the default) waits no longer than
|
|
243
|
+
* `pollWait` does. From 0 to 1000.
|
|
244
|
+
*/
|
|
245
|
+
maxWaitMs: i32 = 0
|
|
196
246
|
/**
|
|
197
247
|
* Milliseconds from a request's head to the end of its body. A request
|
|
198
248
|
* still reading its body past it has its connection closed: the carrier's
|
|
@@ -202,11 +252,19 @@ export class HandlerLimits {
|
|
|
202
252
|
* to a second, or until `wait` returns.
|
|
203
253
|
*/
|
|
204
254
|
bodyTimeout: i32 = 30000
|
|
255
|
+
/**
|
|
256
|
+
* The most bytes a `gzip` or `deflate` request body may decode to; past it
|
|
257
|
+
* the request is answered 413 and never taken. 0 decodes nothing, so every
|
|
258
|
+
* body reaches the program as it arrived. Each slot's body buffer may grow
|
|
259
|
+
* to this as well as to `http.maxBody`.
|
|
260
|
+
*/
|
|
261
|
+
maxDecoded: i32 = 1048576
|
|
205
262
|
|
|
206
263
|
/**
|
|
207
|
-
* The defaults: 256 slots, a 64 KiB JSON buffer, 256 events a turn
|
|
208
|
-
*
|
|
209
|
-
*
|
|
264
|
+
* The defaults: 256 slots, a 64 KiB JSON buffer, 256 events a turn and no
|
|
265
|
+
* wait for a batch of them, a 30 second body timeout, a one MiB cap on a
|
|
266
|
+
* decoded body, and the carrier's own defaults with the body cap lowered to
|
|
267
|
+
* one MiB. Set a field to tune it.
|
|
210
268
|
*/
|
|
211
269
|
constructor() {
|
|
212
270
|
this.http = new Http1Config()
|
|
@@ -290,6 +348,17 @@ export class HttpRequest {
|
|
|
290
348
|
headerCount: i32 = 0
|
|
291
349
|
/** How many bytes of `body` are the request's body, 0 for none; read the body as `body[0 .. bodyLength)`. */
|
|
292
350
|
bodyLength: i32 = 0
|
|
351
|
+
/**
|
|
352
|
+
* The content coding the handler decoded `body` from: `HTTP_CODING_GZIP`
|
|
353
|
+
* or `HTTP_CODING_DEFLATE`, or `HTTP_CODING_IDENTITY` for a body that is
|
|
354
|
+
* as it arrived. Its `Content-Encoding` field is kept in `head` either way.
|
|
355
|
+
*/
|
|
356
|
+
decodedFrom: i32 = 0
|
|
357
|
+
/**
|
|
358
|
+
* The status the handler answers in the program's place, 413 or 400, or 0.
|
|
359
|
+
* @internal Set by `HttpHandler` in this module when a body does not decode, and answered by `ready`.
|
|
360
|
+
*/
|
|
361
|
+
refusal: i32 = 0
|
|
293
362
|
/** The connection's slot, and its generation when this request was read. */
|
|
294
363
|
slot: i32 = 0
|
|
295
364
|
/**
|
|
@@ -314,6 +383,36 @@ export class HttpRequest {
|
|
|
314
383
|
* @internal Advanced by `HttpHandler` in this module as `write` keeps what the carrier had no room for.
|
|
315
384
|
*/
|
|
316
385
|
pendingEnd: i32 = 0
|
|
386
|
+
/**
|
|
387
|
+
* The steps `before` has still to hand out, one bit each; loaded from the router by the first `before`.
|
|
388
|
+
* @internal Moved only by `HttpHandler.before` in this module.
|
|
389
|
+
*/
|
|
390
|
+
stepsLeft: i32 = 0
|
|
391
|
+
/**
|
|
392
|
+
* The steps `before` handed out that `after` has still to hand out again.
|
|
393
|
+
* @internal Moved by `HttpHandler.before` and `after` in this module.
|
|
394
|
+
*/
|
|
395
|
+
stepsRan: i32 = 0
|
|
396
|
+
/**
|
|
397
|
+
* Whether `before` has loaded the route's steps into `stepsLeft`.
|
|
398
|
+
* @internal Set by `HttpHandler.before` and cleared when the request is read, in this module.
|
|
399
|
+
*/
|
|
400
|
+
stepped: boolean = false
|
|
401
|
+
/**
|
|
402
|
+
* How many fields `field` added: `addedNames[0 .. added)` beside `addedValues`.
|
|
403
|
+
* @internal Counted by `HttpHandler.field` in this module, read by `start`, and cleared when the request is read.
|
|
404
|
+
*/
|
|
405
|
+
added: i32 = 0
|
|
406
|
+
/**
|
|
407
|
+
* The names `field` added, kept by reference until the answer starts.
|
|
408
|
+
* @internal Grown by `HttpHandler.field` in this module, to at most `HANDLER_MAX_ADDED`, and kept for the requests after.
|
|
409
|
+
*/
|
|
410
|
+
addedNames: u8[][]
|
|
411
|
+
/**
|
|
412
|
+
* The values `field` added, `addedValues[k]` for `addedNames[k]`.
|
|
413
|
+
* @internal Grown beside `addedNames` by `HttpHandler.field` in this module.
|
|
414
|
+
*/
|
|
415
|
+
addedValues: u8[][]
|
|
317
416
|
|
|
318
417
|
/**
|
|
319
418
|
* An empty request for the handler's pool; its buffers grow to the first
|
|
@@ -327,6 +426,8 @@ export class HttpRequest {
|
|
|
327
426
|
this.params = []
|
|
328
427
|
this.body = []
|
|
329
428
|
this.pending = []
|
|
429
|
+
this.addedNames = []
|
|
430
|
+
this.addedValues = []
|
|
330
431
|
}
|
|
331
432
|
|
|
332
433
|
/**
|
|
@@ -348,63 +449,143 @@ export class HttpRequest {
|
|
|
348
449
|
}
|
|
349
450
|
|
|
350
451
|
/**
|
|
351
|
-
* Where parameter `k` starts in `head`. Panics on a parameter the route does not have.
|
|
452
|
+
* Where parameter `k` starts in `head`. Panics on a parameter the route does not have, a caller contract; `tryParam...` answers `Err` instead.
|
|
352
453
|
* @param k the parameter's position in the route pattern, from 0
|
|
353
454
|
* @returns the byte offset in `head` of the parameter's first raw byte
|
|
354
455
|
*/
|
|
355
456
|
paramStart(k: i32): i32 {
|
|
356
|
-
this.
|
|
357
|
-
return this.params[2 * k]
|
|
457
|
+
return this.paramBound(k, HANDLER_ZERO)
|
|
358
458
|
}
|
|
359
459
|
|
|
360
460
|
/**
|
|
361
|
-
* Where parameter `k` ends in `head`.
|
|
461
|
+
* Where parameter `k` ends in `head`. Panics on a parameter the route does not have, a caller contract; `tryParam...` answers `Err` instead.
|
|
362
462
|
* @param k the parameter's position in the route pattern, from 0
|
|
363
463
|
* @returns the byte offset in `head` just past the parameter's last raw byte
|
|
364
464
|
*/
|
|
365
465
|
paramEnd(k: i32): i32 {
|
|
366
|
-
this.
|
|
367
|
-
return this.params[2 * k + 1]
|
|
466
|
+
return this.paramBound(k, toI32(1))
|
|
368
467
|
}
|
|
369
468
|
|
|
370
469
|
/**
|
|
371
|
-
* Whether parameter `k`, raw, is exactly `text`.
|
|
470
|
+
* Whether parameter `k`, raw, is exactly `text`. Panics on a parameter the route does not have, a caller contract; `tryParam...` answers `Err` instead.
|
|
372
471
|
* @param k the parameter's position in the route pattern, from 0
|
|
373
472
|
* @param text the value to compare with, as it would appear still percent-encoded
|
|
374
473
|
* @returns true when the raw parameter bytes are exactly `text`
|
|
375
474
|
*/
|
|
376
475
|
paramIs(k: i32, text: string): boolean {
|
|
377
|
-
this.
|
|
378
|
-
return handlerSpanIs(this.head, this.params[2 * k], this.params[2 * k + 1], text)
|
|
476
|
+
return handlerSpanIs(this.head, this.paramBound(k, HANDLER_ZERO), this.paramBound(k, toI32(1)), text)
|
|
379
477
|
}
|
|
380
478
|
|
|
381
479
|
/**
|
|
382
480
|
* Parameter `k` percent-decoded into `out` from 0 (`routerDecode`): its
|
|
383
481
|
* length, `ROUTER_BAD_ESCAPE` for a malformed escape, or `ROUTER_NO_ROOM`
|
|
384
|
-
* when `out` is too short. Nothing is written on a code.
|
|
482
|
+
* when `out` is too short. Nothing is written on a code. Panics on a parameter the route does not have, a caller contract; `tryParam...` answers `Err` instead.
|
|
385
483
|
* @param k the parameter's position in the route pattern, from 0
|
|
386
484
|
* @param out where the decoded bytes go, from index 0; the caller's buffer
|
|
387
485
|
* @returns how many decoded bytes were written, or a negative `ROUTER_*` code
|
|
388
486
|
*/
|
|
389
487
|
param(k: i32, out: u8[]): i32 {
|
|
390
|
-
this.
|
|
391
|
-
return routerDecode(this.head, this.params[2 * k], this.params[2 * k + 1], out)
|
|
488
|
+
return routerDecode(this.head, this.paramBound(k, HANDLER_ZERO), this.paramBound(k, toI32(1)), out)
|
|
392
489
|
}
|
|
393
490
|
|
|
394
491
|
/**
|
|
395
|
-
* Parameter `k` read as a decimal number that is not negative and fits an
|
|
492
|
+
* Parameter `k` read as a decimal number that is not negative and fits an
|
|
493
|
+
* `i32`, or -1. Panics on a parameter the route does not have, a caller contract; `tryParam...` answers `Err` instead.
|
|
396
494
|
* @param k the parameter's position in the route pattern, from 0
|
|
397
495
|
* @returns the number the parameter's digits spell, or -1 when it is empty, holds a non-digit or overflows
|
|
398
496
|
*/
|
|
399
497
|
paramDecimal(k: i32): i32 {
|
|
400
|
-
this.
|
|
401
|
-
|
|
498
|
+
return handlerDecimal(this.head, this.paramBound(k, HANDLER_ZERO), this.paramBound(k, toI32(1)))
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Where parameter `k` starts in `head`, or an error naming the parameter the route does not have.
|
|
503
|
+
* @param k the parameter's position in the route pattern, from 0
|
|
504
|
+
* @returns `Ok` with the byte offset in `head` of the parameter's first raw byte, or `Err` when the route has no parameter `k`
|
|
505
|
+
*/
|
|
506
|
+
tryParamStart(k: i32): Result<i32, string> {
|
|
507
|
+
return this.tryParamBound(k, HANDLER_ZERO)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Where parameter `k` ends in `head`, or an error naming the parameter the route does not have.
|
|
512
|
+
* @param k the parameter's position in the route pattern, from 0
|
|
513
|
+
* @returns `Ok` with the byte offset in `head` just past the parameter's last raw byte, or `Err` when the route has no parameter `k`
|
|
514
|
+
*/
|
|
515
|
+
tryParamEnd(k: i32): Result<i32, string> {
|
|
516
|
+
return this.tryParamBound(k, toI32(1))
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Parameter `k` read as a decimal number, as `paramDecimal` reads it, or an
|
|
521
|
+
* error naming the parameter the route does not have.
|
|
522
|
+
* @param k the parameter's position in the route pattern, from 0
|
|
523
|
+
* @returns `Ok` with the number, or -1 when the parameter is empty, holds a non-digit or overflows; `Err` when the route has no parameter `k`
|
|
524
|
+
*/
|
|
525
|
+
tryParamDecimal(k: i32): Result<i32, string> {
|
|
526
|
+
const start = this.tryParamBound(k, HANDLER_ZERO)
|
|
527
|
+
if (start.isErr()) {
|
|
528
|
+
return Err(start.error)
|
|
529
|
+
}
|
|
530
|
+
const end = this.tryParamBound(k, toI32(1))
|
|
531
|
+
if (end.isErr()) {
|
|
532
|
+
return Err(end.error)
|
|
533
|
+
}
|
|
534
|
+
return Ok(handlerDecimal(this.head, start.value, end.value))
|
|
402
535
|
}
|
|
403
536
|
|
|
404
|
-
|
|
537
|
+
/**
|
|
538
|
+
* Parameter `k`'s start (`side` 0) or end (`side` 1) in `head`: the one
|
|
539
|
+
* place the parameter contract panics.
|
|
540
|
+
*/
|
|
541
|
+
private paramBound(k: i32, side: i32): i32 {
|
|
542
|
+
const bound: i32 = this.paramAt(k, side)
|
|
543
|
+
if (bound < 0) {
|
|
544
|
+
// A parameter the route does not have: the program's own mistake.
|
|
545
|
+
panic(this.paramFault(k))
|
|
546
|
+
}
|
|
547
|
+
return bound
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Parameter `k`'s start (`side` 0) or end (`side` 1) in `head`, or why the
|
|
552
|
+
* route has no parameter `k`.
|
|
553
|
+
*/
|
|
554
|
+
private tryParamBound(k: i32, side: i32): Result<i32, string> {
|
|
555
|
+
const bound: i32 = this.paramAt(k, side)
|
|
556
|
+
if (bound < 0) {
|
|
557
|
+
return Err(this.paramFault(k))
|
|
558
|
+
}
|
|
559
|
+
return Ok(bound)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Parameter `k`'s start (`side` 0) or end (`side` 1) in `head`, or -1 when
|
|
564
|
+
* the route has no parameter `k`: an offset in `head` is never negative, so
|
|
565
|
+
* -1 is no answer a span gives. It builds no `Result`, so the hot path
|
|
566
|
+
* allocates nothing. The literal bound on `k` comes first, so `2 * k + 1`
|
|
567
|
+
* is seen not to overflow; a route holds far fewer parameters.
|
|
568
|
+
*/
|
|
569
|
+
private paramAt(k: i32, side: i32): i32 {
|
|
570
|
+
if (k < 0 || k >= this.paramCount || k > 65535) {
|
|
571
|
+
return -1
|
|
572
|
+
}
|
|
573
|
+
const at: i32 = 2 * k
|
|
574
|
+
if (at + 1 >= toI32(this.params.length)) {
|
|
575
|
+
return -1
|
|
576
|
+
}
|
|
577
|
+
return side === 0 ? this.params[at] : this.params[at + 1]
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** Why `paramAt` gave -1 for parameter `k`. */
|
|
581
|
+
private paramFault(k: i32): string {
|
|
405
582
|
if (k < 0 || k >= this.paramCount) {
|
|
406
|
-
|
|
583
|
+
return `HttpRequest.param: the route has ${this.paramCount} parameter(s), not one at ${k}`
|
|
407
584
|
}
|
|
585
|
+
if (k > 65535) {
|
|
586
|
+
return `HttpRequest.param: parameter ${k} is past the 65536 a route can hold`
|
|
587
|
+
}
|
|
588
|
+
return `HttpRequest.param: the request holds no span for parameter ${k}`
|
|
408
589
|
}
|
|
409
590
|
|
|
410
591
|
/**
|
|
@@ -419,11 +600,7 @@ export class HttpRequest {
|
|
|
419
600
|
* @returns how many decoded bytes were written, or `HANDLER_ABSENT` or a negative `ROUTER_*` code
|
|
420
601
|
*/
|
|
421
602
|
query(name: string, out: u8[]): i32 {
|
|
422
|
-
|
|
423
|
-
if (at < 0) {
|
|
424
|
-
return HANDLER_ABSENT
|
|
425
|
-
}
|
|
426
|
-
return handlerFormDecode(this.head, this.queryValueStart, this.queryValueEnd, out)
|
|
603
|
+
return this.queryScan(name, out, true)
|
|
427
604
|
}
|
|
428
605
|
|
|
429
606
|
/**
|
|
@@ -446,25 +623,63 @@ export class HttpRequest {
|
|
|
446
623
|
* @returns the byte offset in `head` where the matching `name=value` pair starts, or -1 when none matches
|
|
447
624
|
*/
|
|
448
625
|
queryIndex(name: string): i32 {
|
|
449
|
-
|
|
450
|
-
|
|
626
|
+
// Without `decode` the walk writes nothing to `out`.
|
|
627
|
+
return this.queryScan(name, this.head, false)
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* `queryIndex`'s walk: with `decode`, the matching value decoded into `out`
|
|
632
|
+
* as `query` answers it (`HANDLER_ABSENT` when none matches), else the
|
|
633
|
+
* pair's start or -1. The decode reads the span the walk proved inside
|
|
634
|
+
* `head`, so it needs no check of its own.
|
|
635
|
+
*/
|
|
636
|
+
private queryScan(name: string, out: u8[], decode: boolean): i32 {
|
|
637
|
+
const none: i32 = decode ? HANDLER_ABSENT : -1
|
|
638
|
+
// The target lies inside `head`; the bound says so to the prover.
|
|
639
|
+
const length: i32 = toI32(this.head.length)
|
|
640
|
+
const end: i32 = this.targetEnd < length ? this.targetEnd : length
|
|
641
|
+
// A path that runs to the target's end has no query.
|
|
642
|
+
const pathEnd: i32 = this.pathEnd
|
|
643
|
+
if (pathEnd < 0 || pathEnd >= end || pathEnd >= length) {
|
|
644
|
+
return none
|
|
645
|
+
}
|
|
646
|
+
let at: i32 = pathEnd + 1
|
|
451
647
|
while (at < end) {
|
|
452
648
|
let pairEnd: i32 = at
|
|
453
|
-
while (
|
|
649
|
+
while (
|
|
650
|
+
pairEnd >= 0 &&
|
|
651
|
+
pairEnd < end &&
|
|
652
|
+
pairEnd < toI32(this.head.length) &&
|
|
653
|
+
toI32(this.head[pairEnd]) !== 38
|
|
654
|
+
) {
|
|
454
655
|
pairEnd += 1
|
|
455
656
|
}
|
|
456
657
|
let keyEnd: i32 = at
|
|
457
|
-
while (
|
|
658
|
+
while (
|
|
659
|
+
keyEnd >= 0 &&
|
|
660
|
+
keyEnd < pairEnd &&
|
|
661
|
+
keyEnd < toI32(this.head.length) &&
|
|
662
|
+
toI32(this.head[keyEnd]) !== 61
|
|
663
|
+
) {
|
|
458
664
|
keyEnd += 1
|
|
459
665
|
}
|
|
460
666
|
if (handlerFormIs(this.head, at, keyEnd, name)) {
|
|
461
|
-
|
|
667
|
+
// A pair with no `=` has an empty value, at its end. The length is
|
|
668
|
+
// worked in 64 bits: both ends lie inside `head`, so it is exact, but
|
|
669
|
+
// the prover keeps no lower bound on `keyEnd` across the loop.
|
|
670
|
+
const valueStart: i32 = keyEnd < pairEnd ? keyEnd + 1 : pairEnd
|
|
671
|
+
const valueLength: i32 = keyEnd < pairEnd ? toI32(toI64(pairEnd) - toI64(keyEnd) - 1) : 0
|
|
672
|
+
this.queryValueStart = valueStart
|
|
462
673
|
this.queryValueEnd = pairEnd
|
|
463
|
-
return at
|
|
674
|
+
return decode ? handlerFormDecode(this.head, valueStart, valueLength, out) : at
|
|
675
|
+
}
|
|
676
|
+
// The last pair ends the query.
|
|
677
|
+
if (pairEnd >= end) {
|
|
678
|
+
return none
|
|
464
679
|
}
|
|
465
680
|
at = pairEnd + 1
|
|
466
681
|
}
|
|
467
|
-
return
|
|
682
|
+
return none
|
|
468
683
|
}
|
|
469
684
|
|
|
470
685
|
/**
|
|
@@ -473,30 +688,95 @@ export class HttpRequest {
|
|
|
473
688
|
* @returns the field's index for `valueStart` and `valueEnd`, or `HANDLER_ABSENT` when no field has the name
|
|
474
689
|
*/
|
|
475
690
|
header(name: string): i32 {
|
|
476
|
-
|
|
477
|
-
|
|
691
|
+
// `at` steps four slots a field, so it is `4 * i` without the product;
|
|
692
|
+
// the literal bound keeps the step from overflowing.
|
|
693
|
+
let at: i32 = 0
|
|
694
|
+
for (let i: i32 = 0; i < this.headerCount && at < 1048576 && at + 1 < toI32(this.spans.length); i++) {
|
|
695
|
+
if (handlerSpanIs(this.head, this.spans[at], this.spans[at + 1], name)) {
|
|
478
696
|
return i
|
|
479
697
|
}
|
|
698
|
+
at += 4
|
|
480
699
|
}
|
|
481
700
|
return HANDLER_ABSENT
|
|
482
701
|
}
|
|
483
702
|
|
|
484
703
|
/**
|
|
485
|
-
* Where field `i`'s value starts in `head`.
|
|
704
|
+
* Where field `i`'s value starts in `head`. Panics on an `i` that is not a
|
|
705
|
+
* field index, a caller contract: pass only what `header` answered, after
|
|
706
|
+
* checking it is not `HANDLER_ABSENT`.
|
|
486
707
|
* @param i a field index, as `header` answered it
|
|
487
708
|
* @returns the byte offset in `head` of the value's first byte
|
|
488
709
|
*/
|
|
489
710
|
valueStart(i: i32): i32 {
|
|
490
|
-
return this.
|
|
711
|
+
return this.valueBound(i, toI32(2))
|
|
491
712
|
}
|
|
492
713
|
|
|
493
714
|
/**
|
|
494
|
-
* Where field `i`'s value ends in `head`.
|
|
715
|
+
* Where field `i`'s value ends in `head`. Panics on an `i` that is not a
|
|
716
|
+
* field index, a caller contract: pass only what `header` answered, after
|
|
717
|
+
* checking it is not `HANDLER_ABSENT`.
|
|
495
718
|
* @param i a field index, as `header` answered it
|
|
496
719
|
* @returns the byte offset in `head` just past the value's last byte
|
|
497
720
|
*/
|
|
498
721
|
valueEnd(i: i32): i32 {
|
|
499
|
-
return this.
|
|
722
|
+
return this.valueBound(i, toI32(3))
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Where field `i`'s value starts in `head`, or an error when `i` is not a
|
|
727
|
+
* field index `header` answered.
|
|
728
|
+
* @param i a field index, as `header` answered it
|
|
729
|
+
* @returns `Ok` with the byte offset in `head` of the value's first byte, or `Err` naming `i`
|
|
730
|
+
*/
|
|
731
|
+
tryValueStart(i: i32): Result<i32, string> {
|
|
732
|
+
return this.tryValueBound(i, toI32(2))
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Where field `i`'s value ends in `head`, or an error when `i` is not a
|
|
737
|
+
* field index `header` answered.
|
|
738
|
+
* @param i a field index, as `header` answered it
|
|
739
|
+
* @returns `Ok` with the byte offset in `head` just past the value's last byte, or `Err` naming `i`
|
|
740
|
+
*/
|
|
741
|
+
tryValueEnd(i: i32): Result<i32, string> {
|
|
742
|
+
return this.tryValueBound(i, toI32(3))
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** Field `i`'s value start (`part` 2) or end (`part` 3): the one place the field contract panics. */
|
|
746
|
+
private valueBound(i: i32, part: i32): i32 {
|
|
747
|
+
const bound: i32 = this.valueAt(i, part)
|
|
748
|
+
if (bound < 0) {
|
|
749
|
+
// A field index `header` did not answer: the program's own mistake.
|
|
750
|
+
panic(this.valueFault(i))
|
|
751
|
+
}
|
|
752
|
+
return bound
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/** Field `i`'s value start (`part` 2) or end (`part` 3) in `head`, or why `i` is no field index. */
|
|
756
|
+
private tryValueBound(i: i32, part: i32): Result<i32, string> {
|
|
757
|
+
const bound: i32 = this.valueAt(i, part)
|
|
758
|
+
if (bound < 0) {
|
|
759
|
+
return Err(this.valueFault(i))
|
|
760
|
+
}
|
|
761
|
+
return Ok(bound)
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Field `i`'s value start (`part` 2) or end (`part` 3) in `head`, or -1 when
|
|
766
|
+
* `i` is no field index: an offset in `head` is never negative. It builds
|
|
767
|
+
* no `Result`, so the hot path allocates nothing.
|
|
768
|
+
*/
|
|
769
|
+
private valueAt(i: i32, part: i32): i32 {
|
|
770
|
+
const at: i32 = handlerSpanSlot(i, part)
|
|
771
|
+
if (i < 0 || i >= this.headerCount || at < 0 || at >= toI32(this.spans.length)) {
|
|
772
|
+
return -1
|
|
773
|
+
}
|
|
774
|
+
return this.spans[at]
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** Why `valueAt` gave -1 for field `i`. */
|
|
778
|
+
private valueFault(i: i32): string {
|
|
779
|
+
return `HttpRequest.value: ${i} is not a field index of the ${this.headerCount} \`header\` answers`
|
|
500
780
|
}
|
|
501
781
|
|
|
502
782
|
/**
|
|
@@ -506,14 +786,37 @@ export class HttpRequest {
|
|
|
506
786
|
* @returns true when the field is present and its first occurrence has exactly that value
|
|
507
787
|
*/
|
|
508
788
|
headerIs(name: string, value: string): boolean {
|
|
789
|
+
// An absent field (`HANDLER_ABSENT`) is no field index: both answer -1.
|
|
509
790
|
const i: i32 = this.header(name)
|
|
510
|
-
|
|
791
|
+
const start: i32 = this.valueAt(i, toI32(2))
|
|
792
|
+
const end: i32 = this.valueAt(i, toI32(3))
|
|
793
|
+
if (start < 0 || end < 0) {
|
|
794
|
+
return false
|
|
795
|
+
}
|
|
796
|
+
return handlerSpanIs(this.head, start, end, value)
|
|
511
797
|
}
|
|
512
798
|
}
|
|
513
799
|
|
|
514
800
|
/**
|
|
515
801
|
* The HTTP/1.1 server: a carrier, its readiness loop, a router and a pool of
|
|
516
802
|
* requests. One per thread; nothing in it is shared.
|
|
803
|
+
*
|
|
804
|
+
* A `try` twin refuses a broken contract with an `Err`, and what it leaves
|
|
805
|
+
* depends on whether the response's head went out:
|
|
806
|
+
*
|
|
807
|
+
* - **Refused before the head** — a request not taken or already answered,
|
|
808
|
+
* a status outside 200 to 599, a one-shot answer's body window outside its
|
|
809
|
+
* buffer (`trySend`): nothing is sent and `r` is as it was, so a request
|
|
810
|
+
* still owed an answer can be answered again (a bare 500, say).
|
|
811
|
+
* - **Abandoned after the head** — a head the carrier refuses (a reserved
|
|
812
|
+
* field, a name or value that is not field text), a `tryWrite` window
|
|
813
|
+
* outside its buffer or more body than the declared length (the head went
|
|
814
|
+
* out with `tryStart`, so these fall here, not above), or a body short of
|
|
815
|
+
* it (`tryEnd`): the response cannot be completed, so the connection is
|
|
816
|
+
* closed and `r` is the handler's again. Nothing more is owed for `r`; a
|
|
817
|
+
* further answer for it is refused too.
|
|
818
|
+
*
|
|
819
|
+
* The plain methods panic with the same message where their twins refuse.
|
|
517
820
|
*/
|
|
518
821
|
export class HttpHandler {
|
|
519
822
|
/**
|
|
@@ -535,6 +838,10 @@ export class HttpHandler {
|
|
|
535
838
|
private owing: boolean[]
|
|
536
839
|
/** The slots whose send `flush` still owes, `owedCount` of them. */
|
|
537
840
|
private owed: i32[]
|
|
841
|
+
/** `HandlerLimits.minEvents`: above 1 with `maxWaitMs` above 0, `wait` calls `pollWaitBatch`. */
|
|
842
|
+
private minEvents: i32 = 1
|
|
843
|
+
/** `HandlerLimits.maxWaitMs`. */
|
|
844
|
+
private maxWaitMs: i32 = 0
|
|
538
845
|
/** `pollWait`'s output. */
|
|
539
846
|
private readyFds: i32[]
|
|
540
847
|
/** Per watched id: whether its descriptor was ready in the last `wait`. */
|
|
@@ -558,7 +865,12 @@ export class HttpHandler {
|
|
|
558
865
|
private allowBytes: u8[][]
|
|
559
866
|
private jsonType: u8[]
|
|
560
867
|
private problemType: u8[]
|
|
868
|
+
/** Decodes `gzip` and `deflate` bodies under `HandlerLimits.maxDecoded`. */
|
|
869
|
+
private decoder: HttpBodyDecoder
|
|
561
870
|
private noFields: u8[][]
|
|
871
|
+
/** Per length: the array `start` gathers an answer's fields and the fields `field` added into, kept for the answers after. */
|
|
872
|
+
private namesBy: u8[][][]
|
|
873
|
+
private valuesBy: u8[][][]
|
|
562
874
|
/**
|
|
563
875
|
* When an accept failed for want of a resource (EMFILE for descriptors,
|
|
564
876
|
* ENFILE for ring slots, ENOMEM): the listener is out of the loop until
|
|
@@ -584,6 +896,8 @@ export class HttpHandler {
|
|
|
584
896
|
private queueStart: i32 = 0
|
|
585
897
|
private queueCount: i32 = 0
|
|
586
898
|
private owedCount: i32 = 0
|
|
899
|
+
/** `HandlerLimits.maxDecoded`, 0 to decode nothing. */
|
|
900
|
+
private maxDecoded: i32 = 0
|
|
587
901
|
private sent: i64 = 0
|
|
588
902
|
private outputSize: i32 = 0
|
|
589
903
|
/** Set by `drain`: accept nothing more and close each connection once it is idle. */
|
|
@@ -592,6 +906,8 @@ export class HttpHandler {
|
|
|
592
906
|
private deferring: boolean = false
|
|
593
907
|
/** Whether `json` gave out the writer for a value `sendJson` has not yet sent. */
|
|
594
908
|
private jsonBegun: boolean = false
|
|
909
|
+
/** Why the last core answered `HANDLER_REFUSED`. */
|
|
910
|
+
private refusal: string = ""
|
|
595
911
|
|
|
596
912
|
/**
|
|
597
913
|
* A handler over `listener`, a listening socket the program made
|
|
@@ -600,20 +916,28 @@ export class HttpHandler {
|
|
|
600
916
|
* before the first `wait`. `limits.ring` acts only through the listener:
|
|
601
917
|
* open it with `limits.listen`, since a listener from `tcpListen` stays on
|
|
602
918
|
* descriptors whatever `ring` says.
|
|
603
|
-
* @param limits the caps and timeouts. `slots`, `jsonBytes`, `maxEvents
|
|
604
|
-
* and `
|
|
605
|
-
* effect; `http` is kept by the carrier, which reads its timeouts and
|
|
919
|
+
* @param limits the caps and timeouts. `slots`, `jsonBytes`, `maxEvents`,
|
|
920
|
+
* `bodyTimeout`, `minEvents` and `maxWaitMs` are read once here, so
|
|
921
|
+
* changing them later has no effect; `http` is kept by the carrier, which reads its timeouts and
|
|
606
922
|
* caps live
|
|
607
923
|
* @param listener the listening socket's descriptor, still the program's to close
|
|
608
924
|
* @param router the routes, which the handler keeps and consults at each request head
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
925
|
+
* @param loop a readiness loop from `pollCreate` for the handler to own, or
|
|
926
|
+
* -1 (the default) to make one here; any other negative is a failed
|
|
927
|
+
* `pollCreate` passed through. Panics on limits below their least and on
|
|
928
|
+
* a failed loop, each a contract of this constructor: use
|
|
929
|
+
* `openHttpHandler` for a `Result`.
|
|
930
|
+
*/
|
|
931
|
+
constructor(limits: HandlerLimits, listener: i32, router: Router, loop: i32 = -1) {
|
|
932
|
+
const fault: string = handlerLimitsFault(limits)
|
|
933
|
+
if (fault.length > 0) {
|
|
934
|
+
panic(fault)
|
|
935
|
+
}
|
|
936
|
+
this.maxDecoded = limits.maxDecoded
|
|
937
|
+
this.decoder = new HttpBodyDecoder(limits.maxDecoded > 0 ? limits.maxDecoded : 1)
|
|
616
938
|
this.bodyTimeout = toI64(limits.bodyTimeout) * HANDLER_NANOS_PER_MS
|
|
939
|
+
this.minEvents = limits.minEvents
|
|
940
|
+
this.maxWaitMs = limits.maxWaitMs
|
|
617
941
|
this.server = new Http1Server(limits.http, listener, limits.slots)
|
|
618
942
|
this.router = router
|
|
619
943
|
this.listener = listener
|
|
@@ -621,19 +945,16 @@ export class HttpHandler {
|
|
|
621
945
|
this.requests = []
|
|
622
946
|
this.free = []
|
|
623
947
|
this.queue = new Array<i32>(16)
|
|
624
|
-
this.slotGen =
|
|
625
|
-
this.current =
|
|
626
|
-
this.open =
|
|
627
|
-
this.owing =
|
|
628
|
-
this.owed =
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
this.readyFds = new Array<i32>(2 * limits.maxEvents)
|
|
633
|
-
this.watchedFired = new Array<boolean>(HANDLER_MAX_WATCHED)
|
|
634
|
-
this.scratch = new Array<u8>(limits.jsonBytes)
|
|
948
|
+
this.slotGen = handlerInts(limits.slots, HANDLER_ZERO)
|
|
949
|
+
this.current = handlerInts(limits.slots, -1)
|
|
950
|
+
this.open = handlerFlags(limits.slots)
|
|
951
|
+
this.owing = handlerFlags(limits.slots)
|
|
952
|
+
this.owed = handlerInts(limits.slots, HANDLER_ZERO)
|
|
953
|
+
this.readyFds = handlerInts(handlerAdd(limits.maxEvents, limits.maxEvents), HANDLER_ZERO)
|
|
954
|
+
this.watchedFired = handlerFlags(HANDLER_MAX_WATCHED)
|
|
955
|
+
this.scratch = handlerBytes(limits.jsonBytes)
|
|
635
956
|
this.writer = new JsonWriter(this.scratch, HANDLER_ZERO)
|
|
636
|
-
this.probe =
|
|
957
|
+
this.probe = handlerInts(handlerProbeLength(router.maxParams), HANDLER_ZERO)
|
|
637
958
|
this.jsonType = httpFieldBytes("application/json")
|
|
638
959
|
this.problemType = httpFieldBytes("application/problem+json")
|
|
639
960
|
const contentType: u8[] = httpFieldBytes("content-type")
|
|
@@ -645,7 +966,9 @@ export class HttpHandler {
|
|
|
645
966
|
this.allowMasks = []
|
|
646
967
|
this.allowBytes = []
|
|
647
968
|
this.noFields = []
|
|
648
|
-
this.
|
|
969
|
+
this.namesBy = []
|
|
970
|
+
this.valuesBy = []
|
|
971
|
+
this.loop = loop === -1 ? pollCreate() : loop
|
|
649
972
|
if (this.loop < 0) {
|
|
650
973
|
panic(`HttpHandler: pollCreate failed: ${this.loop}`)
|
|
651
974
|
}
|
|
@@ -669,13 +992,29 @@ export class HttpHandler {
|
|
|
669
992
|
pollAdd(this.loop, fd, H1_WANT_READ, -2 - id)
|
|
670
993
|
}
|
|
671
994
|
|
|
995
|
+
/**
|
|
996
|
+
* `watch`, with an id outside the range answered as an `Err` instead of a panic.
|
|
997
|
+
* @param fd the program's descriptor to watch for reading; it stays the program's to read and close
|
|
998
|
+
* @param id the name `fired` asks about it by, from 0 to `HANDLER_MAX_WATCHED - 1`
|
|
999
|
+
* @returns `Ok(true)` once `fd` is watched, or `Err` naming the id outside the range
|
|
1000
|
+
*/
|
|
1001
|
+
tryWatch(fd: i32, id: i32): Result<boolean, string> {
|
|
1002
|
+
if (id < 0 || id >= HANDLER_MAX_WATCHED) {
|
|
1003
|
+
return Err(`HttpHandler.watch: id ${id} is outside 0 to ${HANDLER_MAX_WATCHED - 1}`)
|
|
1004
|
+
}
|
|
1005
|
+
pollAdd(this.loop, fd, H1_WANT_READ, -2 - id)
|
|
1006
|
+
return Ok(true)
|
|
1007
|
+
}
|
|
1008
|
+
|
|
672
1009
|
/**
|
|
673
1010
|
* Whether watched descriptor `id` was ready in the last `wait`.
|
|
674
1011
|
* @param id the name the descriptor was given to `watch`
|
|
675
1012
|
* @returns true when it was readable in the last turn; false for an id outside the range
|
|
676
1013
|
*/
|
|
677
1014
|
fired(id: i32): boolean {
|
|
678
|
-
return
|
|
1015
|
+
return (
|
|
1016
|
+
id >= 0 && id < HANDLER_MAX_WATCHED && id < toI32(this.watchedFired.length) && this.watchedFired[id]
|
|
1017
|
+
)
|
|
679
1018
|
}
|
|
680
1019
|
|
|
681
1020
|
/**
|
|
@@ -688,8 +1027,9 @@ export class HttpHandler {
|
|
|
688
1027
|
*/
|
|
689
1028
|
wait(timeoutMs: i32): i32 {
|
|
690
1029
|
this.flush()
|
|
691
|
-
|
|
692
|
-
|
|
1030
|
+
const fired: boolean[] = this.watchedFired
|
|
1031
|
+
for (let id: i32 = 0; id < HANDLER_MAX_WATCHED && id < toI32(fired.length); id++) {
|
|
1032
|
+
fired[id] = false
|
|
693
1033
|
}
|
|
694
1034
|
if (this.acceptRestUntil > 0 && monotonicNanos() >= this.acceptRestUntil) {
|
|
695
1035
|
this.endAcceptRest()
|
|
@@ -697,7 +1037,10 @@ export class HttpHandler {
|
|
|
697
1037
|
let timeout: i32 = timeoutMs
|
|
698
1038
|
if (this.acceptRestUntil > 0 && !this.draining) {
|
|
699
1039
|
// Wake for the listener's return, whatever the program asked.
|
|
700
|
-
const rest: i32 =
|
|
1040
|
+
const rest: i32 = handlerAdd(
|
|
1041
|
+
toI32(handlerSince(monotonicNanos(), this.acceptRestUntil) / toI64(1000000)),
|
|
1042
|
+
1
|
|
1043
|
+
)
|
|
701
1044
|
if (timeout < 0 || rest < timeout) {
|
|
702
1045
|
timeout = rest
|
|
703
1046
|
}
|
|
@@ -708,20 +1051,31 @@ export class HttpHandler {
|
|
|
708
1051
|
// is the program's to take, and the turn blocks as it always has.
|
|
709
1052
|
timeout = 0
|
|
710
1053
|
}
|
|
711
|
-
|
|
1054
|
+
// A turn that must not block (`timeout` 0) never waits for a batch.
|
|
1055
|
+
const n: i32 =
|
|
1056
|
+
this.minEvents > 1 && this.maxWaitMs > 0 && timeout !== 0
|
|
1057
|
+
? pollWaitBatch(this.loop, this.readyFds, timeout, this.minEvents, this.maxWaitMs)
|
|
1058
|
+
: pollWait(this.loop, this.readyFds, timeout)
|
|
712
1059
|
const now: i64 = monotonicNanos()
|
|
713
1060
|
this.turnAt = now
|
|
714
|
-
|
|
715
|
-
|
|
1061
|
+
const fds: i32[] = this.readyFds
|
|
1062
|
+
// Two numbers an event; the literal bound keeps `2 * k + 1` in an `i32`.
|
|
1063
|
+
for (let k: i32 = 0; k < n && k < 1048576; k++) {
|
|
1064
|
+
const at: i32 = 2 * k
|
|
1065
|
+
if (at + 1 >= toI32(fds.length)) {
|
|
1066
|
+
break
|
|
1067
|
+
}
|
|
1068
|
+
const token: i32 = fds[at]
|
|
1069
|
+
const bits: i32 = fds[at + 1]
|
|
716
1070
|
if (token === HANDLER_LISTENER) {
|
|
717
1071
|
this.acceptAll()
|
|
718
1072
|
} else if (token <= -2) {
|
|
719
|
-
const id: i32 = -2
|
|
720
|
-
if (id < HANDLER_MAX_WATCHED) {
|
|
721
|
-
|
|
1073
|
+
const id: i32 = handlerSub(-2, token)
|
|
1074
|
+
if (id >= 0 && id < HANDLER_MAX_WATCHED && id < toI32(fired.length)) {
|
|
1075
|
+
fired[id] = true
|
|
722
1076
|
}
|
|
723
1077
|
} else if (token >= 0 && this.server.holds(token)) {
|
|
724
|
-
if ((
|
|
1078
|
+
if ((bits & HANDLER_READ_BITS) !== 0) {
|
|
725
1079
|
this.server.readable(token)
|
|
726
1080
|
} else {
|
|
727
1081
|
this.server.writable(token)
|
|
@@ -733,7 +1087,7 @@ export class HttpHandler {
|
|
|
733
1087
|
this.sweep()
|
|
734
1088
|
}
|
|
735
1089
|
if (now >= this.bodyCheckAt) {
|
|
736
|
-
this.bodyCheckAt = now
|
|
1090
|
+
this.bodyCheckAt = handlerLater(now, HANDLER_BODY_SWEEP)
|
|
737
1091
|
this.expireBodies(now)
|
|
738
1092
|
}
|
|
739
1093
|
return this.queueCount
|
|
@@ -765,15 +1119,19 @@ export class HttpHandler {
|
|
|
765
1119
|
if (this.acceptRestUntil === 0) {
|
|
766
1120
|
pollRemove(this.loop, this.listener)
|
|
767
1121
|
}
|
|
768
|
-
|
|
1122
|
+
const open: boolean[] = this.open
|
|
1123
|
+
const current: i32[] = this.current
|
|
1124
|
+
for (let s: i32 = 0; s < toI32(open.length) && s < toI32(current.length); s++) {
|
|
1125
|
+
const isOpen: boolean = open[s]
|
|
1126
|
+
const idle: boolean = current[s] === -1
|
|
769
1127
|
if (
|
|
770
|
-
|
|
1128
|
+
isOpen &&
|
|
771
1129
|
this.server.holds(s) &&
|
|
772
|
-
|
|
1130
|
+
idle &&
|
|
773
1131
|
(this.server.resting(s) || !this.server.connection(s).wantsWrite())
|
|
774
1132
|
) {
|
|
775
1133
|
this.closeSlot(s)
|
|
776
|
-
} else if (
|
|
1134
|
+
} else if (isOpen && this.server.holds(s)) {
|
|
777
1135
|
// Its exchange is its last: the answer says `Connection: close` if
|
|
778
1136
|
// it is not yet started, and no pipelined request is read behind it.
|
|
779
1137
|
this.server.connection(s).closeAfter = true
|
|
@@ -788,9 +1146,12 @@ export class HttpHandler {
|
|
|
788
1146
|
}
|
|
789
1147
|
let slot: i32 = this.server.accept()
|
|
790
1148
|
while (slot >= 0 || slot === H1_POOL_FULL || slot === HANDLER_ACCEPT_ABORTED) {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
1149
|
+
const open: boolean[] = this.open
|
|
1150
|
+
const current: i32[] = this.current
|
|
1151
|
+
// The carrier hands only its own slots, each with a place in both.
|
|
1152
|
+
if (slot >= 0 && slot < toI32(open.length) && slot < toI32(current.length)) {
|
|
1153
|
+
open[slot] = true
|
|
1154
|
+
current[slot] = -1
|
|
794
1155
|
pollAdd(this.loop, this.server.fd(slot), H1_WANT_READ, slot)
|
|
795
1156
|
}
|
|
796
1157
|
slot = this.server.accept()
|
|
@@ -800,7 +1161,7 @@ export class HttpHandler {
|
|
|
800
1161
|
// stays in the backlog and the listener stays readable, so rest it
|
|
801
1162
|
// rather than spin, until a close frees one or the rest ends.
|
|
802
1163
|
pollRemove(this.loop, this.listener)
|
|
803
|
-
this.acceptRestUntil = monotonicNanos()
|
|
1164
|
+
this.acceptRestUntil = handlerLater(monotonicNanos(), HANDLER_ACCEPT_REST)
|
|
804
1165
|
}
|
|
805
1166
|
}
|
|
806
1167
|
|
|
@@ -812,10 +1173,16 @@ export class HttpHandler {
|
|
|
812
1173
|
flush(): void {
|
|
813
1174
|
const n: i32 = this.owedCount
|
|
814
1175
|
this.owedCount = 0
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
1176
|
+
const owed: i32[] = this.owed
|
|
1177
|
+
const owing: boolean[] = this.owing
|
|
1178
|
+
const open: boolean[] = this.open
|
|
1179
|
+
for (let k: i32 = 0; k < n && k < toI32(owed.length); k++) {
|
|
1180
|
+
const slot: i32 = owed[k]
|
|
1181
|
+
if (slot < 0 || slot >= toI32(owing.length) || slot >= toI32(open.length)) {
|
|
1182
|
+
continue
|
|
1183
|
+
}
|
|
1184
|
+
owing[slot] = false
|
|
1185
|
+
if (open[slot] && this.server.holds(slot)) {
|
|
819
1186
|
this.settle(slot, this.server.connection(slot))
|
|
820
1187
|
}
|
|
821
1188
|
}
|
|
@@ -854,13 +1221,18 @@ export class HttpHandler {
|
|
|
854
1221
|
// The carrier has written its refusal and ends the connection; a
|
|
855
1222
|
// request it was reading is nobody's.
|
|
856
1223
|
this.releaseReading(slot)
|
|
857
|
-
} else if (this.owable(slot, conn)) {
|
|
858
|
-
if (
|
|
1224
|
+
} else if (this.owable(slot, conn) && slot >= 0 && slot < toI32(this.owing.length)) {
|
|
1225
|
+
if (this.owing[slot]) {
|
|
1226
|
+
return
|
|
1227
|
+
}
|
|
1228
|
+
// `owed` has a place for every slot and holds each at most once, so `n` is in range.
|
|
1229
|
+
const n: i32 = this.owedCount
|
|
1230
|
+
if (n >= 0 && n < toI32(this.owed.length)) {
|
|
859
1231
|
this.owing[slot] = true
|
|
860
|
-
this.owed[
|
|
861
|
-
this.owedCount
|
|
1232
|
+
this.owed[n] = slot
|
|
1233
|
+
this.owedCount = n + 1
|
|
1234
|
+
return
|
|
862
1235
|
}
|
|
863
|
-
return
|
|
864
1236
|
}
|
|
865
1237
|
this.settle(slot, conn)
|
|
866
1238
|
}
|
|
@@ -877,21 +1249,48 @@ export class HttpHandler {
|
|
|
877
1249
|
* the handler waits.
|
|
878
1250
|
*/
|
|
879
1251
|
private owable(slot: i32, conn: Http1Connection): boolean {
|
|
880
|
-
const
|
|
1252
|
+
const r: HttpRequest | null = this.requestAt(this.currentOf(slot))
|
|
881
1253
|
return (
|
|
882
1254
|
this.deferring &&
|
|
883
1255
|
!this.draining &&
|
|
884
1256
|
conn.wantsWrite() &&
|
|
885
1257
|
!conn.writeHeld &&
|
|
886
1258
|
this.roomy(conn) &&
|
|
887
|
-
(
|
|
1259
|
+
(r === null || r.state !== HANDLER_PARKED)
|
|
888
1260
|
)
|
|
889
1261
|
}
|
|
890
1262
|
|
|
1263
|
+
/** The index of the request `slot` is reading or answering, or -1 for none (or a slot out of range). */
|
|
1264
|
+
private currentOf(slot: i32): i32 {
|
|
1265
|
+
return slot >= 0 && slot < toI32(this.current.length) ? this.current[slot] : -1
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
/** The request at index `at`, or null for -1 (or an index out of range). */
|
|
1269
|
+
private requestAt(at: i32): HttpRequest | null {
|
|
1270
|
+
return at >= 0 && at < toI32(this.requests.length) ? this.requests[at] : null
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
/** The request at the head of the queue, or null when the queue is empty. */
|
|
1274
|
+
private queued(): HttpRequest | null {
|
|
1275
|
+
const start: i32 = this.queueStart
|
|
1276
|
+
if (this.queueCount <= 0 || start < 0 || start >= toI32(this.queue.length)) {
|
|
1277
|
+
return null
|
|
1278
|
+
}
|
|
1279
|
+
return this.requestAt(this.queue[start])
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/** Drops the request at the head of the queue. */
|
|
1283
|
+
private dequeue(): void {
|
|
1284
|
+
this.queueStart = handlerRing(this.queueStart, 1, toI32(this.queue.length))
|
|
1285
|
+
this.queueCount = handlerSub(this.queueCount, 1)
|
|
1286
|
+
}
|
|
1287
|
+
|
|
891
1288
|
/** Sends what `slot` holds and re-arms it, or closes it once it is over; enqueues a parked request once there is room. */
|
|
892
1289
|
private settle(slot: i32, conn: Http1Connection): void {
|
|
893
1290
|
if (conn.wantsWrite()) {
|
|
894
|
-
|
|
1291
|
+
// Worked in `u64`, whose wrapping sum has the bits of the signed one: a
|
|
1292
|
+
// count of sends is centuries short of 2^63.
|
|
1293
|
+
this.sent = toI64(toU64(this.sent) + toU64(1))
|
|
895
1294
|
}
|
|
896
1295
|
// `flush` arms a held write for writing even once its send emptied the output.
|
|
897
1296
|
const wants: i32 = this.server.flush(slot)
|
|
@@ -899,26 +1298,34 @@ export class HttpHandler {
|
|
|
899
1298
|
this.closeSlot(slot)
|
|
900
1299
|
return
|
|
901
1300
|
}
|
|
902
|
-
if (this.draining && this.
|
|
1301
|
+
if (this.draining && this.currentOf(slot) === -1 && !conn.wantsWrite()) {
|
|
903
1302
|
this.closeSlot(slot)
|
|
904
1303
|
return
|
|
905
1304
|
}
|
|
906
1305
|
if (this.server.rearm(slot, wants)) {
|
|
907
1306
|
pollModify(this.loop, this.server.fd(slot), wants & (H1_WANT_READ | H1_WANT_WRITE), slot)
|
|
908
1307
|
}
|
|
909
|
-
const
|
|
910
|
-
if (
|
|
911
|
-
this.enqueue(
|
|
1308
|
+
const r: HttpRequest | null = this.requestAt(this.currentOf(slot))
|
|
1309
|
+
if (r !== null && r.state === HANDLER_PARKED && this.roomy(conn)) {
|
|
1310
|
+
this.enqueue(r)
|
|
912
1311
|
}
|
|
913
1312
|
}
|
|
914
1313
|
|
|
915
1314
|
/** Whether `conn`'s output holds at most half its size unsent, so a response head fits behind it. */
|
|
916
1315
|
private roomy(conn: Http1Connection): boolean {
|
|
917
|
-
|
|
1316
|
+
// `2 * unsent <= size` is `unsent <= size / 2` for whole numbers, without the doubling.
|
|
1317
|
+
return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(this.outputSize) / toI64(2)
|
|
918
1318
|
}
|
|
919
1319
|
|
|
920
1320
|
/** A head: a request from the free list, the head copied into it, and its route. */
|
|
921
1321
|
private begin(slot: i32, conn: Http1Connection): void {
|
|
1322
|
+
const current: i32[] = this.current
|
|
1323
|
+
const slotGen: i32[] = this.slotGen
|
|
1324
|
+
// `drive` hands only the carrier's slots, each with a place in both.
|
|
1325
|
+
if (slot < 0 || slot >= toI32(current.length) || slot >= toI32(slotGen.length)) {
|
|
1326
|
+
return
|
|
1327
|
+
}
|
|
1328
|
+
const gen: i32 = slotGen[slot]
|
|
922
1329
|
if (this.draining) {
|
|
923
1330
|
// A head read during the drain is the connection's last.
|
|
924
1331
|
conn.closeAfter = true
|
|
@@ -927,15 +1334,18 @@ export class HttpHandler {
|
|
|
927
1334
|
const p = conn.parser
|
|
928
1335
|
const length: i32 = p.headLength
|
|
929
1336
|
if (toI32(r.head.length) < length) {
|
|
930
|
-
r.head =
|
|
1337
|
+
r.head = handlerBytes(handlerGrown(toI32(r.head.length), length))
|
|
931
1338
|
}
|
|
932
1339
|
copyBytes(r.head, HANDLER_ZERO, p.head, HANDLER_ZERO, length)
|
|
933
|
-
//
|
|
934
|
-
//
|
|
935
|
-
|
|
936
|
-
|
|
1340
|
+
// Only the first `4 * headerCount` spans are this head's, and only those
|
|
1341
|
+
// are ever read, so only those are copied.
|
|
1342
|
+
const spanCount: i32 = handlerSpanSlot(p.headerCount, 0)
|
|
1343
|
+
if (toI32(r.spans.length) < spanCount) {
|
|
1344
|
+
r.spans = handlerInts(handlerGrown(toI32(r.spans.length), spanCount), HANDLER_ZERO)
|
|
1345
|
+
}
|
|
1346
|
+
for (let k: i32 = 0; k < spanCount && k < toI32(r.spans.length) && k < toI32(p.spans.length); k++) {
|
|
1347
|
+
r.spans[k] = p.spans[k]
|
|
937
1348
|
}
|
|
938
|
-
r.spans.set(p.spans)
|
|
939
1349
|
r.headLength = length
|
|
940
1350
|
r.headerCount = p.headerCount
|
|
941
1351
|
r.methodStart = p.methodStart
|
|
@@ -943,16 +1353,22 @@ export class HttpHandler {
|
|
|
943
1353
|
r.targetStart = p.targetStart
|
|
944
1354
|
r.targetEnd = p.targetEnd
|
|
945
1355
|
r.bodyLength = 0
|
|
1356
|
+
r.decodedFrom = HTTP_CODING_IDENTITY
|
|
1357
|
+
r.refusal = 0
|
|
946
1358
|
r.slot = slot
|
|
947
|
-
r.gen =
|
|
1359
|
+
r.gen = gen
|
|
948
1360
|
r.since = this.turnAt
|
|
949
1361
|
r.state = HANDLER_READING
|
|
950
1362
|
r.pendingStart = 0
|
|
951
1363
|
r.pendingEnd = 0
|
|
1364
|
+
r.stepped = false
|
|
1365
|
+
r.added = 0
|
|
952
1366
|
this.route(r)
|
|
953
1367
|
// The router found where the path ends, before any query, as it matched.
|
|
954
1368
|
r.pathEnd = this.router.pathEnd
|
|
955
|
-
|
|
1369
|
+
if (slot < toI32(current.length)) {
|
|
1370
|
+
current[slot] = r.index
|
|
1371
|
+
}
|
|
956
1372
|
}
|
|
957
1373
|
|
|
958
1374
|
/**
|
|
@@ -973,7 +1389,11 @@ export class HttpHandler {
|
|
|
973
1389
|
)
|
|
974
1390
|
if (route === ROUTER_METHOD_NOT_ALLOWED && r.methodIs("HEAD")) {
|
|
975
1391
|
const get: i32 = this.router.methodIndex("GET")
|
|
976
|
-
if (
|
|
1392
|
+
if (
|
|
1393
|
+
get >= 0 &&
|
|
1394
|
+
get < toI32(this.router.methodStart.length) &&
|
|
1395
|
+
get < toI32(this.router.methodEnd.length)
|
|
1396
|
+
) {
|
|
977
1397
|
const viaGet: i32 = this.router.match(
|
|
978
1398
|
this.router.methodBytes,
|
|
979
1399
|
this.router.methodStart[get],
|
|
@@ -994,15 +1414,18 @@ export class HttpHandler {
|
|
|
994
1414
|
|
|
995
1415
|
/** Body bytes: appended to the request's body, which the carrier's cap bounds. */
|
|
996
1416
|
private gather(slot: i32, conn: Http1Connection): void {
|
|
997
|
-
const
|
|
998
|
-
if (
|
|
1417
|
+
const r: HttpRequest | null = this.requestAt(this.currentOf(slot))
|
|
1418
|
+
if (r === null) {
|
|
999
1419
|
return
|
|
1000
1420
|
}
|
|
1001
|
-
const r: HttpRequest = this.requests[at]
|
|
1002
1421
|
const n: i32 = conn.dataLength
|
|
1003
|
-
|
|
1422
|
+
// The carrier's cap keeps a body far below 2^31 bytes.
|
|
1423
|
+
const need: i32 = handlerAdd(r.bodyLength, n)
|
|
1424
|
+
if (need < 0) {
|
|
1425
|
+
return
|
|
1426
|
+
}
|
|
1004
1427
|
if (toI32(r.body.length) < need) {
|
|
1005
|
-
const grown: u8[] =
|
|
1428
|
+
const grown: u8[] = handlerBytes(handlerGrown(toI32(r.body.length), need))
|
|
1006
1429
|
copyBytes(grown, HANDLER_ZERO, r.body, HANDLER_ZERO, r.bodyLength)
|
|
1007
1430
|
r.body = grown
|
|
1008
1431
|
}
|
|
@@ -1012,11 +1435,11 @@ export class HttpHandler {
|
|
|
1012
1435
|
|
|
1013
1436
|
/** The request is whole: queued for `take`, or parked while the response before it is still mostly unsent. */
|
|
1014
1437
|
private whole(slot: i32, conn: Http1Connection): void {
|
|
1015
|
-
const
|
|
1016
|
-
if (
|
|
1438
|
+
const r: HttpRequest | null = this.requestAt(this.currentOf(slot))
|
|
1439
|
+
if (r === null) {
|
|
1017
1440
|
return
|
|
1018
1441
|
}
|
|
1019
|
-
|
|
1442
|
+
this.decode(r)
|
|
1020
1443
|
if (this.roomy(conn)) {
|
|
1021
1444
|
this.enqueue(r)
|
|
1022
1445
|
} else {
|
|
@@ -1024,28 +1447,77 @@ export class HttpHandler {
|
|
|
1024
1447
|
}
|
|
1025
1448
|
}
|
|
1026
1449
|
|
|
1450
|
+
/**
|
|
1451
|
+
* Decodes `r`'s body when its one `Content-Encoding` is gzip or deflate,
|
|
1452
|
+
* swapping the decoded bytes in as its body; a body that decodes past
|
|
1453
|
+
* `maxDecoded`, or not at all, is marked for `ready` to refuse.
|
|
1454
|
+
*/
|
|
1455
|
+
private decode(r: HttpRequest): void {
|
|
1456
|
+
if (this.maxDecoded === 0 || r.bodyLength === 0) {
|
|
1457
|
+
return
|
|
1458
|
+
}
|
|
1459
|
+
let coding: i32 = HTTP_CODING_IDENTITY
|
|
1460
|
+
const spans: i32[] = r.spans
|
|
1461
|
+
for (let i: i32 = 0; i < r.headerCount; i++) {
|
|
1462
|
+
// A head's spans are four a field; the literal bound keeps `at + 3` in an `i32`.
|
|
1463
|
+
const at: i32 = handlerSpanSlot(i, 0)
|
|
1464
|
+
if (at < 0 || at > 1048576 || at + 3 >= toI32(spans.length)) {
|
|
1465
|
+
break
|
|
1466
|
+
}
|
|
1467
|
+
const valueStart: i32 = spans[at + 2]
|
|
1468
|
+
const valueEnd: i32 = spans[at + 3]
|
|
1469
|
+
if (handlerSpanIs(r.head, spans[at], spans[at + 1], "content-encoding")) {
|
|
1470
|
+
const named: i32 = httpContentEncoding(r.head, valueStart, valueEnd)
|
|
1471
|
+
// A second field stacks its codings on the first's (RFC 9110 §5.3).
|
|
1472
|
+
coding = coding === HTTP_CODING_IDENTITY ? named : HTTP_CODING_NONE
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
if (coding !== HTTP_CODING_GZIP && coding !== HTTP_CODING_DEFLATE) {
|
|
1476
|
+
return
|
|
1477
|
+
}
|
|
1478
|
+
const got: i32 = this.decoder.decode(coding, r.body, HANDLER_ZERO, r.bodyLength)
|
|
1479
|
+
if (got < 0) {
|
|
1480
|
+
r.refusal = got === HTTP_DECODE_TOO_LARGE ? toI32(413) : toI32(400)
|
|
1481
|
+
return
|
|
1482
|
+
}
|
|
1483
|
+
// The buffers change hands, so neither is copied or grown twice.
|
|
1484
|
+
const decoded: u8[] = this.decoder.output
|
|
1485
|
+
this.decoder.output = r.body
|
|
1486
|
+
r.body = decoded
|
|
1487
|
+
r.bodyLength = got
|
|
1488
|
+
r.decodedFrom = coding
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1027
1491
|
private enqueue(r: HttpRequest): void {
|
|
1028
1492
|
r.state = HANDLER_QUEUED
|
|
1029
|
-
const
|
|
1030
|
-
|
|
1031
|
-
|
|
1493
|
+
const old: i32[] = this.queue
|
|
1494
|
+
const size: i32 = toI32(old.length)
|
|
1495
|
+
if (this.queueCount >= size) {
|
|
1496
|
+
const grown: i32[] = handlerInts(handlerAdd(size, size), -1)
|
|
1032
1497
|
for (let k: i32 = 0; k < size && k < toI32(grown.length); k++) {
|
|
1033
|
-
|
|
1498
|
+
const from: i32 = handlerRing(this.queueStart, k, size)
|
|
1499
|
+
if (from >= 0 && from < toI32(old.length)) {
|
|
1500
|
+
grown[k] = old[from]
|
|
1501
|
+
}
|
|
1034
1502
|
}
|
|
1035
1503
|
this.queue = grown
|
|
1036
1504
|
this.queueStart = 0
|
|
1037
1505
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
1506
|
+
const queue: i32[] = this.queue
|
|
1507
|
+
// `handlerRing` answers in `[0, size)` for a queue of `size` places.
|
|
1508
|
+
const end: i32 = handlerRing(this.queueStart, this.queueCount, toI32(queue.length))
|
|
1509
|
+
if (end >= 0 && end < toI32(queue.length)) {
|
|
1510
|
+
queue[end] = r.index
|
|
1511
|
+
this.queueCount = handlerAdd(this.queueCount, 1)
|
|
1512
|
+
}
|
|
1040
1513
|
}
|
|
1041
1514
|
|
|
1042
1515
|
/** `H1_WRITE`: the carrier has room again for the response under way. */
|
|
1043
1516
|
private resume(slot: i32, conn: Http1Connection): void {
|
|
1044
|
-
const
|
|
1045
|
-
if (
|
|
1517
|
+
const r: HttpRequest | null = this.requestAt(this.currentOf(slot))
|
|
1518
|
+
if (r === null) {
|
|
1046
1519
|
return
|
|
1047
1520
|
}
|
|
1048
|
-
const r: HttpRequest = this.requests[at]
|
|
1049
1521
|
if (r.state !== HANDLER_STREAMING && r.state !== HANDLER_FLUSHING) {
|
|
1050
1522
|
return
|
|
1051
1523
|
}
|
|
@@ -1057,13 +1529,13 @@ export class HttpHandler {
|
|
|
1057
1529
|
|
|
1058
1530
|
/** Hands the carrier as much of `r`'s pending bytes as it takes. */
|
|
1059
1531
|
private pushPending(r: HttpRequest, conn: Http1Connection): void {
|
|
1060
|
-
const left: i32 = r.pendingEnd
|
|
1532
|
+
const left: i32 = handlerSub(r.pendingEnd, r.pendingStart)
|
|
1061
1533
|
if (left === 0) {
|
|
1062
1534
|
return
|
|
1063
1535
|
}
|
|
1064
1536
|
const taken: i32 = conn.write(r.pending, r.pendingStart, left)
|
|
1065
1537
|
if (taken > 0) {
|
|
1066
|
-
r.pendingStart
|
|
1538
|
+
r.pendingStart = handlerAdd(r.pendingStart, taken)
|
|
1067
1539
|
}
|
|
1068
1540
|
if (r.pendingStart === r.pendingEnd) {
|
|
1069
1541
|
r.pendingStart = 0
|
|
@@ -1081,20 +1553,30 @@ export class HttpHandler {
|
|
|
1081
1553
|
if (conn.end() === H1_AGAIN) {
|
|
1082
1554
|
return
|
|
1083
1555
|
}
|
|
1084
|
-
|
|
1556
|
+
const current: i32[] = this.current
|
|
1557
|
+
const slot: i32 = r.slot
|
|
1558
|
+
if (slot >= 0 && slot < toI32(current.length)) {
|
|
1559
|
+
current[slot] = -1
|
|
1560
|
+
}
|
|
1085
1561
|
this.release(r)
|
|
1086
1562
|
}
|
|
1087
1563
|
|
|
1088
1564
|
/** A free request, or a new one. */
|
|
1089
1565
|
private obtain(): HttpRequest {
|
|
1090
|
-
const
|
|
1566
|
+
const free: i32[] = this.free
|
|
1567
|
+
const count: i32 = toI32(free.length)
|
|
1091
1568
|
if (count > 0) {
|
|
1092
|
-
const at: i32 =
|
|
1093
|
-
|
|
1094
|
-
|
|
1569
|
+
const at: i32 = free[count - 1]
|
|
1570
|
+
if (free.length > 0) {
|
|
1571
|
+
free.pop()
|
|
1572
|
+
}
|
|
1573
|
+
const reused: HttpRequest | null = this.requestAt(at)
|
|
1574
|
+
if (reused !== null) {
|
|
1575
|
+
return reused
|
|
1576
|
+
}
|
|
1095
1577
|
}
|
|
1096
1578
|
const r: HttpRequest = new HttpRequest(toI32(this.requests.length))
|
|
1097
|
-
r.params =
|
|
1579
|
+
r.params = handlerInts(handlerProbeLength(this.router.maxParams), HANDLER_ZERO)
|
|
1098
1580
|
this.requests.push(r)
|
|
1099
1581
|
return r
|
|
1100
1582
|
}
|
|
@@ -1109,10 +1591,11 @@ export class HttpHandler {
|
|
|
1109
1591
|
|
|
1110
1592
|
/** Hands back the request `slot` was reading when the carrier refused it. */
|
|
1111
1593
|
private releaseReading(slot: i32): void {
|
|
1112
|
-
const
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1594
|
+
const r: HttpRequest | null = this.requestAt(this.currentOf(slot))
|
|
1595
|
+
const current: i32[] = this.current
|
|
1596
|
+
if (r !== null && r.state === HANDLER_READING && slot >= 0 && slot < toI32(current.length)) {
|
|
1597
|
+
current[slot] = -1
|
|
1598
|
+
this.release(r)
|
|
1116
1599
|
}
|
|
1117
1600
|
}
|
|
1118
1601
|
|
|
@@ -1142,12 +1625,23 @@ export class HttpHandler {
|
|
|
1142
1625
|
if (this.acceptRestUntil > 0 && !this.draining) {
|
|
1143
1626
|
this.endAcceptRest()
|
|
1144
1627
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
const
|
|
1148
|
-
|
|
1149
|
-
if (
|
|
1150
|
-
|
|
1628
|
+
const open: boolean[] = this.open
|
|
1629
|
+
const slotGen: i32[] = this.slotGen
|
|
1630
|
+
const current: i32[] = this.current
|
|
1631
|
+
// Every slot the carrier hands has a place in all three.
|
|
1632
|
+
if (
|
|
1633
|
+
slot < 0 ||
|
|
1634
|
+
slot >= toI32(open.length) ||
|
|
1635
|
+
slot >= toI32(slotGen.length) ||
|
|
1636
|
+
slot >= toI32(current.length)
|
|
1637
|
+
) {
|
|
1638
|
+
return
|
|
1639
|
+
}
|
|
1640
|
+
open[slot] = false
|
|
1641
|
+
slotGen[slot] = handlerAdd(slotGen[slot], 1)
|
|
1642
|
+
const r: HttpRequest | null = this.requestAt(current[slot])
|
|
1643
|
+
current[slot] = -1
|
|
1644
|
+
if (r !== null) {
|
|
1151
1645
|
if (r.state === HANDLER_READING || r.state === HANDLER_PARKED || r.state === HANDLER_FLUSHING) {
|
|
1152
1646
|
this.release(r)
|
|
1153
1647
|
}
|
|
@@ -1160,7 +1654,8 @@ export class HttpHandler {
|
|
|
1160
1654
|
const at: i32 = this.current[s]
|
|
1161
1655
|
if (this.open[s] && at >= 0 && at < toI32(this.requests.length)) {
|
|
1162
1656
|
const r: HttpRequest = this.requests[at]
|
|
1163
|
-
|
|
1657
|
+
const since: i64 = r.since
|
|
1658
|
+
if (r.state === HANDLER_READING && handlerSince(since, now) > this.bodyTimeout) {
|
|
1164
1659
|
this.closeSlot(s)
|
|
1165
1660
|
}
|
|
1166
1661
|
}
|
|
@@ -1179,18 +1674,50 @@ export class HttpHandler {
|
|
|
1179
1674
|
// ---- Requests -------------------------------------------------------------------
|
|
1180
1675
|
|
|
1181
1676
|
/**
|
|
1182
|
-
* Whether a whole request is waiting for `take`. Requests whose connection
|
|
1677
|
+
* Whether a whole request is waiting for `take`. Requests whose connection
|
|
1678
|
+
* closed while queued are handed back here, and a body that decoded past
|
|
1679
|
+
* `maxDecoded` (413) or did not decode (400) is answered here.
|
|
1183
1680
|
* @returns true when `take` will give out a request whose connection is still open
|
|
1184
1681
|
*/
|
|
1185
1682
|
ready(): boolean {
|
|
1186
1683
|
while (this.queueCount > 0) {
|
|
1187
|
-
const r: HttpRequest = this.
|
|
1188
|
-
if (
|
|
1684
|
+
const r: HttpRequest | null = this.queued()
|
|
1685
|
+
if (r === null) {
|
|
1686
|
+
// A place that names no request: none is ever queued, but it is passed over all the same.
|
|
1687
|
+
this.dequeue()
|
|
1688
|
+
continue
|
|
1689
|
+
}
|
|
1690
|
+
const live: boolean = this.live(r)
|
|
1691
|
+
if (live && r.refusal === 0) {
|
|
1189
1692
|
return true
|
|
1190
1693
|
}
|
|
1191
|
-
this.
|
|
1192
|
-
|
|
1193
|
-
|
|
1694
|
+
this.dequeue()
|
|
1695
|
+
if (live) {
|
|
1696
|
+
r.state = HANDLER_HELD
|
|
1697
|
+
let sent: i32 = HANDLER_REFUSED
|
|
1698
|
+
if (r.refusal === 413) {
|
|
1699
|
+
sent = this.problemCore(
|
|
1700
|
+
r,
|
|
1701
|
+
toI32(413),
|
|
1702
|
+
"Content Too Large",
|
|
1703
|
+
"the body decodes to more than this server takes"
|
|
1704
|
+
)
|
|
1705
|
+
} else {
|
|
1706
|
+
sent = this.problemCore(
|
|
1707
|
+
r,
|
|
1708
|
+
toI32(400),
|
|
1709
|
+
"Bad Request",
|
|
1710
|
+
"the body is not in the coding its Content-Encoding names"
|
|
1711
|
+
)
|
|
1712
|
+
}
|
|
1713
|
+
// Held a line above, with a status in range, so it is not refused;
|
|
1714
|
+
// were it, the request goes back rather than staying held.
|
|
1715
|
+
if (sent === HANDLER_REFUSED) {
|
|
1716
|
+
this.release(r)
|
|
1717
|
+
}
|
|
1718
|
+
} else {
|
|
1719
|
+
this.release(r)
|
|
1720
|
+
}
|
|
1194
1721
|
}
|
|
1195
1722
|
return false
|
|
1196
1723
|
}
|
|
@@ -1200,16 +1727,114 @@ export class HttpHandler {
|
|
|
1200
1727
|
* @returns the oldest queued request, the program's until it answers it
|
|
1201
1728
|
*/
|
|
1202
1729
|
take(): HttpRequest {
|
|
1203
|
-
|
|
1730
|
+
const r: HttpRequest | null = this.next()
|
|
1731
|
+
if (r === null) {
|
|
1204
1732
|
panic("HttpHandler.take: no request is ready")
|
|
1205
1733
|
}
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1734
|
+
return r
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
/**
|
|
1738
|
+
* The next whole request, as `take` hands it over, or null when none is `ready`.
|
|
1739
|
+
* @returns the oldest queued request, the program's until it answers it, or null
|
|
1740
|
+
*/
|
|
1741
|
+
next(): HttpRequest | null {
|
|
1742
|
+
const r: HttpRequest | null = this.ready() ? this.queued() : null
|
|
1743
|
+
if (r === null) {
|
|
1744
|
+
return null
|
|
1745
|
+
}
|
|
1746
|
+
this.dequeue()
|
|
1209
1747
|
r.state = HANDLER_HELD
|
|
1210
1748
|
return r
|
|
1211
1749
|
}
|
|
1212
1750
|
|
|
1751
|
+
/**
|
|
1752
|
+
* The next step to run before `r`'s route body, lowest id first, or
|
|
1753
|
+
* `HANDLER_NEXT` once every step `Router.use` bound to `r.route` has run,
|
|
1754
|
+
* or `HANDLER_ANSWERED` once a step has answered `r`. A step reads `r`,
|
|
1755
|
+
* and may answer it — `refuse`, `problem`, `empty` or any other answer —
|
|
1756
|
+
* or add answer fields with `field`; call `before` again straight after
|
|
1757
|
+
* each step, before the next `take`, until it answers a negative code.
|
|
1758
|
+
* Only a request `take` handed over runs steps: what the handler answers
|
|
1759
|
+
* itself, to a bad request, a timeout or a full pool, runs none.
|
|
1760
|
+
*
|
|
1761
|
+
* let s = handler.before(req)
|
|
1762
|
+
* while (s >= 0) { switch (s) { case AUTH: ... } s = handler.before(req) }
|
|
1763
|
+
* if (s === HANDLER_NEXT) { switch (req.route) { ... } }
|
|
1764
|
+
*
|
|
1765
|
+
* @param r a request `take` handed over
|
|
1766
|
+
* @returns the next step's id, `HANDLER_NEXT`, or `HANDLER_ANSWERED`
|
|
1767
|
+
*/
|
|
1768
|
+
before(r: HttpRequest): i32 {
|
|
1769
|
+
if (r.state !== HANDLER_HELD) {
|
|
1770
|
+
return HANDLER_ANSWERED
|
|
1771
|
+
}
|
|
1772
|
+
if (!r.stepped) {
|
|
1773
|
+
r.stepped = true
|
|
1774
|
+
r.stepsLeft = this.router.steps(r.route)
|
|
1775
|
+
r.stepsRan = 0
|
|
1776
|
+
}
|
|
1777
|
+
const left: i32 = r.stepsLeft
|
|
1778
|
+
if (left === 0) {
|
|
1779
|
+
return HANDLER_NEXT
|
|
1780
|
+
}
|
|
1781
|
+
// The lowest bit set, `left & -left`, negated in wrapping unsigned arithmetic.
|
|
1782
|
+
const low: i32 = toI32(toU32(left) & (toU32(0) - toU32(left)))
|
|
1783
|
+
r.stepsLeft = left ^ low
|
|
1784
|
+
r.stepsRan = r.stepsRan | low
|
|
1785
|
+
return toI32(31) - toI32(Math.clz32(toU32(low)))
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
/**
|
|
1789
|
+
* The next step to run after `r`'s route body has chosen its answer and
|
|
1790
|
+
* before it sends it: the steps `before` handed out, highest id first, then
|
|
1791
|
+
* `HANDLER_NEXT`; or `HANDLER_ANSWERED` once a step has answered `r`. A
|
|
1792
|
+
* step here may still `field` or answer in the body's place. A request
|
|
1793
|
+
* `before` was never called for runs no step here.
|
|
1794
|
+
* @param r a request `take` handed over, not yet answered
|
|
1795
|
+
* @returns the next step's id, `HANDLER_NEXT`, or `HANDLER_ANSWERED`
|
|
1796
|
+
*/
|
|
1797
|
+
after(r: HttpRequest): i32 {
|
|
1798
|
+
if (r.state !== HANDLER_HELD) {
|
|
1799
|
+
return HANDLER_ANSWERED
|
|
1800
|
+
}
|
|
1801
|
+
const ran: i32 = r.stepped ? r.stepsRan : 0
|
|
1802
|
+
if (ran === 0) {
|
|
1803
|
+
return HANDLER_NEXT
|
|
1804
|
+
}
|
|
1805
|
+
const step: i32 = toI32(31) - toI32(Math.clz32(toU32(ran)))
|
|
1806
|
+
r.stepsRan = ran ^ (1 << step)
|
|
1807
|
+
return step
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
/**
|
|
1811
|
+
* Adds the field `name: value` to `r`'s answer, after the fields the answer
|
|
1812
|
+
* names itself, whichever answer it is. Both are kept by reference, so they
|
|
1813
|
+
* stay the caller's bytes, unchanged, until the answer starts. Panics past
|
|
1814
|
+
* `HANDLER_MAX_ADDED` fields, or on a request not held; a name or value the
|
|
1815
|
+
* carrier refuses panics as the answer's own would.
|
|
1816
|
+
* @param r a request `take` handed over, not yet answered
|
|
1817
|
+
* @param name the field name, a lowercase token
|
|
1818
|
+
* @param value the field value, field text
|
|
1819
|
+
*/
|
|
1820
|
+
field(r: HttpRequest, name: u8[], value: u8[]): void {
|
|
1821
|
+
if (r.state !== HANDLER_HELD) {
|
|
1822
|
+
panic(`HttpHandler.field: the request is not one taken and unanswered (state ${r.state})`)
|
|
1823
|
+
}
|
|
1824
|
+
const at: i32 = r.added
|
|
1825
|
+
if (at >= HANDLER_MAX_ADDED) {
|
|
1826
|
+
panic(`HttpHandler.field: more than ${HANDLER_MAX_ADDED} fields added to one answer`)
|
|
1827
|
+
}
|
|
1828
|
+
if (at >= 0 && at < toI32(r.addedNames.length) && at < toI32(r.addedValues.length)) {
|
|
1829
|
+
r.addedNames[at] = name
|
|
1830
|
+
r.addedValues[at] = value
|
|
1831
|
+
} else {
|
|
1832
|
+
r.addedNames.push(name)
|
|
1833
|
+
r.addedValues.push(value)
|
|
1834
|
+
}
|
|
1835
|
+
r.added = at + 1
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1213
1838
|
/**
|
|
1214
1839
|
* Whether the client of `r` has hung up: its connection is closed or
|
|
1215
1840
|
* reused, or the peer ended its stream while `r` is held. A half-close
|
|
@@ -1224,7 +1849,9 @@ export class HttpHandler {
|
|
|
1224
1849
|
|
|
1225
1850
|
/** Whether `r`'s connection is the one it was read on, still open. */
|
|
1226
1851
|
private live(r: HttpRequest): boolean {
|
|
1227
|
-
|
|
1852
|
+
const gens: i32[] = this.slotGen
|
|
1853
|
+
const slot: i32 = r.slot
|
|
1854
|
+
return slot >= 0 && slot < toI32(gens.length) && gens[slot] === r.gen && this.server.holds(slot)
|
|
1228
1855
|
}
|
|
1229
1856
|
|
|
1230
1857
|
/**
|
|
@@ -1273,14 +1900,35 @@ export class HttpHandler {
|
|
|
1273
1900
|
* the connection was closed for it
|
|
1274
1901
|
*/
|
|
1275
1902
|
start(r: HttpRequest, status: i32, names: u8[][], values: u8[][], bodyLength: i32): i32 {
|
|
1903
|
+
return this.kept(this.startCore(r, status, names, values, bodyLength))
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
/**
|
|
1907
|
+
* `start`, with a broken contract answered as an `Err` instead of a panic:
|
|
1908
|
+
* a request not taken or already answered, a status outside 200 to 599 or
|
|
1909
|
+
* a body length below `HTTP1_CHUNKED` leave `r` as it was; a field the
|
|
1910
|
+
* carrier refuses closes the connection and hands `r` back.
|
|
1911
|
+
* @param r a request taken and not yet answered
|
|
1912
|
+
* @param status the response status, from 200 to 599
|
|
1913
|
+
* @param names the field names, as bytes, beside `values`
|
|
1914
|
+
* @param values each field's value, `values[k]` for `names[k]`
|
|
1915
|
+
* @param bodyLength how many body bytes will follow, or `HTTP1_CHUNKED` for a body of unknown length
|
|
1916
|
+
* @returns `Ok` with what `start` answers, or `Err` saying which contract the call broke
|
|
1917
|
+
*/
|
|
1918
|
+
tryStart(r: HttpRequest, status: i32, names: u8[][], values: u8[][], bodyLength: i32): Result<i32, string> {
|
|
1919
|
+
return this.checked(this.startCore(r, status, names, values, bodyLength))
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
/** `start`'s work, answering `HANDLER_REFUSED` where `start` panics. */
|
|
1923
|
+
private startCore(r: HttpRequest, status: i32, names: u8[][], values: u8[][], bodyLength: i32): i32 {
|
|
1276
1924
|
if (r.state !== HANDLER_HELD) {
|
|
1277
|
-
|
|
1925
|
+
return this.refused(`HttpHandler.start: the request is not one taken and unanswered (state ${r.state})`)
|
|
1278
1926
|
}
|
|
1279
1927
|
if (status < 200 || status > 599) {
|
|
1280
|
-
|
|
1928
|
+
return this.refused(`HttpHandler.start: status ${status} is outside 200 to 599`)
|
|
1281
1929
|
}
|
|
1282
1930
|
if (bodyLength < HTTP1_CHUNKED) {
|
|
1283
|
-
|
|
1931
|
+
return this.refused(`HttpHandler.start: body length ${bodyLength}`)
|
|
1284
1932
|
}
|
|
1285
1933
|
r.state = HANDLER_STREAMING
|
|
1286
1934
|
if (!this.live(r)) {
|
|
@@ -1288,7 +1936,13 @@ export class HttpHandler {
|
|
|
1288
1936
|
}
|
|
1289
1937
|
const conn: Http1Connection = this.server.connection(r.slot)
|
|
1290
1938
|
const framing: i32 = status === 204 || status === 304 ? HTTP1_NO_BODY : bodyLength
|
|
1291
|
-
|
|
1939
|
+
let outNames: u8[][] = names
|
|
1940
|
+
let outValues: u8[][] = values
|
|
1941
|
+
if (r.added > 0) {
|
|
1942
|
+
outNames = handlerWithAdded(this.namesBy, names, r.addedNames, r.added)
|
|
1943
|
+
outValues = handlerWithAdded(this.valuesBy, values, r.addedValues, r.added)
|
|
1944
|
+
}
|
|
1945
|
+
const sent: i32 = conn.respond(status, outNames, outValues, framing)
|
|
1292
1946
|
if (sent === H1_AGAIN) {
|
|
1293
1947
|
// The output still holds more of the response before this one than a
|
|
1294
1948
|
// head can fit behind — only for a head near half the output, since a
|
|
@@ -1298,7 +1952,8 @@ export class HttpHandler {
|
|
|
1298
1952
|
return HANDLER_DROPPED
|
|
1299
1953
|
}
|
|
1300
1954
|
if (sent !== 0) {
|
|
1301
|
-
|
|
1955
|
+
this.abandon(r)
|
|
1956
|
+
return this.refused(
|
|
1302
1957
|
`HttpHandler.start: the carrier refused the head (${sent}): a reserved field, or a name or value that is not a token or field text`
|
|
1303
1958
|
)
|
|
1304
1959
|
}
|
|
@@ -1308,7 +1963,9 @@ export class HttpHandler {
|
|
|
1308
1963
|
/**
|
|
1309
1964
|
* Writes `buf[off .. off + len)` of `r`'s body. What the carrier has no room
|
|
1310
1965
|
* for is copied and sent as room comes, so `buf` is the caller's again on
|
|
1311
|
-
* return. Answers `HANDLER_SENT` or `HANDLER_DROPPED`.
|
|
1966
|
+
* return. Answers `HANDLER_SENT` or `HANDLER_DROPPED`. A response not
|
|
1967
|
+
* started, a window outside `buf`, or more bytes than the declared length
|
|
1968
|
+
* leaves panics; `tryWrite` answers those instead, closing the connection.
|
|
1312
1969
|
* @param r a request whose response `start` began
|
|
1313
1970
|
* @param buf the bytes to send from; the caller's again on return
|
|
1314
1971
|
* @param off where in `buf` the body bytes start
|
|
@@ -1316,6 +1973,28 @@ export class HttpHandler {
|
|
|
1316
1973
|
* @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed first
|
|
1317
1974
|
*/
|
|
1318
1975
|
write(r: HttpRequest, buf: u8[], off: i32, len: i32): i32 {
|
|
1976
|
+
return this.kept(this.writeCore(r, buf, off, len))
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
/**
|
|
1980
|
+
* `write`, with a broken contract answered as an `Err` instead of a panic.
|
|
1981
|
+
* A response not started leaves `r` as it was. A window outside `buf`, or
|
|
1982
|
+
* more bytes than the declared length leaves (whether this check or the
|
|
1983
|
+
* carrier finds it), abandons the response: the head is out and the body
|
|
1984
|
+
* cannot follow it as declared, so the connection is closed and `r` is
|
|
1985
|
+
* the handler's again. The caller answers nothing more for `r`.
|
|
1986
|
+
* @param r a request whose response `start` began
|
|
1987
|
+
* @param buf the bytes to send from; the caller's again on return
|
|
1988
|
+
* @param off where in `buf` the body bytes start
|
|
1989
|
+
* @param len how many bytes to send
|
|
1990
|
+
* @returns `Ok` with what `write` answers, or `Err` saying which contract the call broke
|
|
1991
|
+
*/
|
|
1992
|
+
tryWrite(r: HttpRequest, buf: u8[], off: i32, len: i32): Result<i32, string> {
|
|
1993
|
+
return this.checked(this.writeCore(r, buf, off, len))
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
/** `write`'s work, answering `HANDLER_REFUSED` where `write` panics. */
|
|
1997
|
+
private writeCore(r: HttpRequest, buf: u8[], off: i32, len: i32): i32 {
|
|
1319
1998
|
const put: i32 = this.put(r, buf, off, len)
|
|
1320
1999
|
if (put === HANDLER_SENT) {
|
|
1321
2000
|
this.drive(r.slot)
|
|
@@ -1332,10 +2011,13 @@ export class HttpHandler {
|
|
|
1332
2011
|
*/
|
|
1333
2012
|
private put(r: HttpRequest, buf: u8[], off: i32, len: i32): i32 {
|
|
1334
2013
|
if (r.state !== HANDLER_STREAMING) {
|
|
1335
|
-
|
|
2014
|
+
return this.refused(`HttpHandler.write: the response is not started (state ${r.state})`)
|
|
1336
2015
|
}
|
|
1337
|
-
|
|
1338
|
-
|
|
2016
|
+
// From here a refusal abandons `r`: its head is out, so a body the
|
|
2017
|
+
// program cannot write as declared leaves no response to complete.
|
|
2018
|
+
if (handlerOutside(buf, off, len)) {
|
|
2019
|
+
this.abandon(r)
|
|
2020
|
+
return this.refused(`HttpHandler.write: the window ${off} + ${len} is outside its ${buf.length} bytes`)
|
|
1339
2021
|
}
|
|
1340
2022
|
if (!this.live(r)) {
|
|
1341
2023
|
return HANDLER_DROPPED
|
|
@@ -1343,9 +2025,10 @@ export class HttpHandler {
|
|
|
1343
2025
|
const conn: Http1Connection = this.server.connection(r.slot)
|
|
1344
2026
|
// The carrier checks the length of what it takes; what is pending it has
|
|
1345
2027
|
// not seen, so the check counts it here.
|
|
1346
|
-
const owed: i32 = conn.responseLeft
|
|
2028
|
+
const owed: i32 = handlerSub(conn.responseLeft, handlerSub(r.pendingEnd, r.pendingStart))
|
|
1347
2029
|
if (conn.responseFraming >= 0 && !conn.responseEmpty && len > owed) {
|
|
1348
|
-
|
|
2030
|
+
this.abandon(r)
|
|
2031
|
+
return this.refused(
|
|
1349
2032
|
`HttpHandler.write: ${len} byte(s) is more than the ${owed} the length the response declared leaves`
|
|
1350
2033
|
)
|
|
1351
2034
|
}
|
|
@@ -1355,25 +2038,29 @@ export class HttpHandler {
|
|
|
1355
2038
|
if (taken === H1_AGAIN) {
|
|
1356
2039
|
taken = 0
|
|
1357
2040
|
} else if (taken < 0) {
|
|
1358
|
-
|
|
2041
|
+
// The same contract as the length check above, as `write` has always
|
|
2042
|
+
// answered it, and abandoned the same way.
|
|
2043
|
+
this.abandon(r)
|
|
2044
|
+
return this.refused(
|
|
1359
2045
|
`HttpHandler.write: the carrier refused the body (${taken}): more than the length the response declared`
|
|
1360
2046
|
)
|
|
1361
2047
|
}
|
|
1362
2048
|
}
|
|
1363
2049
|
if (taken < len) {
|
|
1364
|
-
this.keep(r, buf, off
|
|
2050
|
+
this.keep(r, buf, handlerAdd(off, taken), handlerSub(len, taken))
|
|
1365
2051
|
}
|
|
1366
2052
|
return HANDLER_SENT
|
|
1367
2053
|
}
|
|
1368
2054
|
|
|
1369
2055
|
/** Appends bytes to `r`'s pending buffer, growing it once to the largest response it has held. */
|
|
1370
2056
|
private keep(r: HttpRequest, buf: u8[], off: i32, len: i32): void {
|
|
1371
|
-
const live: i32 = r.pendingEnd
|
|
1372
|
-
|
|
1373
|
-
|
|
2057
|
+
const live: i32 = handlerSub(r.pendingEnd, r.pendingStart)
|
|
2058
|
+
const want: i32 = handlerAdd(live, len)
|
|
2059
|
+
if (handlerSub(toI32(r.pending.length), r.pendingEnd) < len) {
|
|
2060
|
+
if (r.pendingStart > 0 || toI32(r.pending.length) < want) {
|
|
1374
2061
|
const target: u8[] =
|
|
1375
|
-
toI32(r.pending.length) <
|
|
1376
|
-
?
|
|
2062
|
+
toI32(r.pending.length) < want
|
|
2063
|
+
? handlerBytes(handlerGrown(toI32(r.pending.length), want))
|
|
1377
2064
|
: r.pending
|
|
1378
2065
|
copyBytes(target, HANDLER_ZERO, r.pending, r.pendingStart, live)
|
|
1379
2066
|
r.pending = target
|
|
@@ -1382,7 +2069,7 @@ export class HttpHandler {
|
|
|
1382
2069
|
}
|
|
1383
2070
|
}
|
|
1384
2071
|
copyBytes(r.pending, r.pendingEnd, buf, off, len)
|
|
1385
|
-
r.pendingEnd
|
|
2072
|
+
r.pendingEnd = handlerAdd(r.pendingEnd, len)
|
|
1386
2073
|
}
|
|
1387
2074
|
|
|
1388
2075
|
/**
|
|
@@ -1395,8 +2082,25 @@ export class HttpHandler {
|
|
|
1395
2082
|
* @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed first
|
|
1396
2083
|
*/
|
|
1397
2084
|
end(r: HttpRequest): i32 {
|
|
2085
|
+
return this.kept(this.endCore(r))
|
|
2086
|
+
}
|
|
2087
|
+
|
|
2088
|
+
/**
|
|
2089
|
+
* `end`, with a broken contract answered as an `Err` instead of a panic: a
|
|
2090
|
+
* response not started leaves `r` as it was; a body short of the declared
|
|
2091
|
+
* length closes the connection, since the response cannot be completed,
|
|
2092
|
+
* and hands `r` back.
|
|
2093
|
+
* @param r a request whose response `start` began; the handler's again after an `Ok`
|
|
2094
|
+
* @returns `Ok` with what `end` answers, or `Err` saying which contract the call broke
|
|
2095
|
+
*/
|
|
2096
|
+
tryEnd(r: HttpRequest): Result<i32, string> {
|
|
2097
|
+
return this.checked(this.endCore(r))
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
/** `end`'s work, answering `HANDLER_REFUSED` where `end` panics. */
|
|
2101
|
+
private endCore(r: HttpRequest): i32 {
|
|
1398
2102
|
if (r.state !== HANDLER_STREAMING) {
|
|
1399
|
-
|
|
2103
|
+
return this.refused(`HttpHandler.end: the response is not started (state ${r.state})`)
|
|
1400
2104
|
}
|
|
1401
2105
|
if (!this.live(r)) {
|
|
1402
2106
|
this.release(r)
|
|
@@ -1404,9 +2108,12 @@ export class HttpHandler {
|
|
|
1404
2108
|
}
|
|
1405
2109
|
const slot: i32 = r.slot
|
|
1406
2110
|
const conn: Http1Connection = this.server.connection(slot)
|
|
1407
|
-
const short: i32 = conn.responseLeft
|
|
2111
|
+
const short: i32 = handlerSub(conn.responseLeft, handlerSub(r.pendingEnd, r.pendingStart))
|
|
1408
2112
|
if (conn.responseFraming >= 0 && short > 0 && !conn.responseEmpty) {
|
|
1409
|
-
|
|
2113
|
+
this.abandon(r)
|
|
2114
|
+
return this.refused(
|
|
2115
|
+
`HttpHandler.end: the body is ${short} byte(s) short of the length the response declared`
|
|
2116
|
+
)
|
|
1410
2117
|
}
|
|
1411
2118
|
this.finish(r, conn)
|
|
1412
2119
|
this.drive(slot)
|
|
@@ -1426,8 +2133,39 @@ export class HttpHandler {
|
|
|
1426
2133
|
* the connection was closed for it
|
|
1427
2134
|
*/
|
|
1428
2135
|
send(r: HttpRequest, status: i32, contentType: u8[], body: u8[], off: i32, len: i32): i32 {
|
|
1429
|
-
this.
|
|
1430
|
-
|
|
2136
|
+
return this.kept(this.sendCore(r, status, contentType, body, off, len))
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
/**
|
|
2140
|
+
* `send`, with a broken contract answered as an `Err` instead of a panic,
|
|
2141
|
+
* as `trySendFields` answers it.
|
|
2142
|
+
* @param r a request taken and not yet answered; the handler's again after an `Ok`
|
|
2143
|
+
* @param status the response status, from 200 to 599
|
|
2144
|
+
* @param contentType the `Content-Type` value, as bytes
|
|
2145
|
+
* @param body the buffer holding the body; the caller's again on return
|
|
2146
|
+
* @param off where in `body` the body bytes start
|
|
2147
|
+
* @param len how many body bytes to send
|
|
2148
|
+
* @returns `Ok` with what `send` answers, or `Err` saying which contract the call broke
|
|
2149
|
+
*/
|
|
2150
|
+
trySend(
|
|
2151
|
+
r: HttpRequest,
|
|
2152
|
+
status: i32,
|
|
2153
|
+
contentType: u8[],
|
|
2154
|
+
body: u8[],
|
|
2155
|
+
off: i32,
|
|
2156
|
+
len: i32
|
|
2157
|
+
): Result<i32, string> {
|
|
2158
|
+
return this.checked(this.sendCore(r, status, contentType, body, off, len))
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
/** `send`'s work, answering `HANDLER_REFUSED` where `send` panics. */
|
|
2162
|
+
private sendCore(r: HttpRequest, status: i32, contentType: u8[], body: u8[], off: i32, len: i32): i32 {
|
|
2163
|
+
if (this.typeValue.length > 0) {
|
|
2164
|
+
this.typeValue[0] = contentType
|
|
2165
|
+
} else {
|
|
2166
|
+
this.typeValue.push(contentType)
|
|
2167
|
+
}
|
|
2168
|
+
return this.sendFieldsCore(r, status, this.typeName, this.typeValue, body, off, len)
|
|
1431
2169
|
}
|
|
1432
2170
|
|
|
1433
2171
|
/**
|
|
@@ -1452,16 +2190,70 @@ export class HttpHandler {
|
|
|
1452
2190
|
off: i32,
|
|
1453
2191
|
len: i32
|
|
1454
2192
|
): i32 {
|
|
1455
|
-
|
|
2193
|
+
return this.kept(this.sendFieldsCore(r, status, names, values, body, off, len))
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
/**
|
|
2197
|
+
* `sendFields`, with a broken contract answered as an `Err` instead of a
|
|
2198
|
+
* panic: a request not taken or already answered, a status outside 200 to
|
|
2199
|
+
* 599 or, when a body is sent (`len` above 0, a status other than 204 and
|
|
2200
|
+
* 304), a window outside `body` leave `r` as it was; a field the carrier
|
|
2201
|
+
* refuses closes the connection and hands `r` back.
|
|
2202
|
+
* @param r a request taken and not yet answered; the handler's again after an `Ok`
|
|
2203
|
+
* @param status the response status, from 200 to 599
|
|
2204
|
+
* @param names the field names, as bytes, beside `values`
|
|
2205
|
+
* @param values each field's value, `values[k]` for `names[k]`
|
|
2206
|
+
* @param body the buffer holding the body; the caller's again on return
|
|
2207
|
+
* @param off where in `body` the body bytes start
|
|
2208
|
+
* @param len how many body bytes to send
|
|
2209
|
+
* @returns `Ok` with what `sendFields` answers, or `Err` saying which contract the call broke
|
|
2210
|
+
*/
|
|
2211
|
+
trySendFields(
|
|
2212
|
+
r: HttpRequest,
|
|
2213
|
+
status: i32,
|
|
2214
|
+
names: u8[][],
|
|
2215
|
+
values: u8[][],
|
|
2216
|
+
body: u8[],
|
|
2217
|
+
off: i32,
|
|
2218
|
+
len: i32
|
|
2219
|
+
): Result<i32, string> {
|
|
2220
|
+
return this.checked(this.sendFieldsCore(r, status, names, values, body, off, len))
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
/** `sendFields`'s work, answering `HANDLER_REFUSED` where `sendFields` panics. */
|
|
2224
|
+
private sendFieldsCore(
|
|
2225
|
+
r: HttpRequest,
|
|
2226
|
+
status: i32,
|
|
2227
|
+
names: u8[][],
|
|
2228
|
+
values: u8[][],
|
|
2229
|
+
body: u8[],
|
|
2230
|
+
off: i32,
|
|
2231
|
+
len: i32
|
|
2232
|
+
): i32 {
|
|
2233
|
+
// Only a body that is sent has a window; it is checked before the head
|
|
2234
|
+
// goes, so a refused one leaves `r` untouched. A chunked length or a
|
|
2235
|
+
// 204 or 304 sends none, as before.
|
|
2236
|
+
const sends: boolean = len > 0 && status !== 204 && status !== 304
|
|
2237
|
+
if (sends && handlerOutside(body, off, len)) {
|
|
2238
|
+
return this.refused(`HttpHandler.write: the window ${off} + ${len} is outside its ${body.length} bytes`)
|
|
2239
|
+
}
|
|
2240
|
+
const started: i32 = this.startCore(r, status, names, values, len)
|
|
1456
2241
|
if (started === HANDLER_DROPPED) {
|
|
1457
2242
|
this.release(r)
|
|
1458
2243
|
return HANDLER_DROPPED
|
|
1459
2244
|
}
|
|
1460
|
-
if (
|
|
2245
|
+
if (started !== HANDLER_SENT) {
|
|
2246
|
+
return started
|
|
2247
|
+
}
|
|
2248
|
+
if (sends) {
|
|
1461
2249
|
// No pass between the body and the end: `end` drives the slot once.
|
|
1462
|
-
|
|
2250
|
+
// The head declared `len` and the window is checked, so `put` has no
|
|
2251
|
+
// contract left to refuse; should it refuse one, it abandons `r`.
|
|
2252
|
+
if (this.put(r, body, off, len) === HANDLER_REFUSED) {
|
|
2253
|
+
return HANDLER_REFUSED
|
|
2254
|
+
}
|
|
1463
2255
|
}
|
|
1464
|
-
return this.
|
|
2256
|
+
return this.endCore(r)
|
|
1465
2257
|
}
|
|
1466
2258
|
|
|
1467
2259
|
/**
|
|
@@ -1473,7 +2265,31 @@ export class HttpHandler {
|
|
|
1473
2265
|
* the connection was closed for it
|
|
1474
2266
|
*/
|
|
1475
2267
|
empty(r: HttpRequest, status: i32): i32 {
|
|
1476
|
-
return this.
|
|
2268
|
+
return this.kept(this.emptyCore(r, status))
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
/**
|
|
2272
|
+
* `empty`, with a broken contract answered as an `Err` instead of a panic:
|
|
2273
|
+
* a request not taken or already answered, or a status outside 200 to 599,
|
|
2274
|
+
* leaves `r` as it was.
|
|
2275
|
+
* @param r a request taken and not yet answered; the handler's again after an `Ok`
|
|
2276
|
+
* @param status the response status, from 200 to 599
|
|
2277
|
+
* @returns `Ok` with what `empty` answers, or `Err` saying which contract the call broke
|
|
2278
|
+
*/
|
|
2279
|
+
tryEmpty(r: HttpRequest, status: i32): Result<i32, string> {
|
|
2280
|
+
return this.checked(this.emptyCore(r, status))
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
private emptyCore(r: HttpRequest, status: i32): i32 {
|
|
2284
|
+
return this.sendFieldsCore(
|
|
2285
|
+
r,
|
|
2286
|
+
status,
|
|
2287
|
+
this.noFields,
|
|
2288
|
+
this.noFields,
|
|
2289
|
+
this.scratch,
|
|
2290
|
+
HANDLER_ZERO,
|
|
2291
|
+
HANDLER_ZERO
|
|
2292
|
+
)
|
|
1477
2293
|
}
|
|
1478
2294
|
|
|
1479
2295
|
/**
|
|
@@ -1499,14 +2315,30 @@ export class HttpHandler {
|
|
|
1499
2315
|
* the connection was closed for it
|
|
1500
2316
|
*/
|
|
1501
2317
|
sendJson(r: HttpRequest, status: i32): i32 {
|
|
2318
|
+
return this.kept(this.sendJsonCore(r, status))
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
/**
|
|
2322
|
+
* `sendJson`, with a broken contract answered as an `Err` instead of a
|
|
2323
|
+
* panic: no value begun with `json`, a request not taken or already
|
|
2324
|
+
* answered, or a status outside 200 to 599, leaves `r` as it was.
|
|
2325
|
+
* @param r a request taken and not yet answered; the handler's again after an `Ok`
|
|
2326
|
+
* @param status the response status for a value that fit, from 200 to 599
|
|
2327
|
+
* @returns `Ok` with what `sendJson` answers, or `Err` saying which contract the call broke
|
|
2328
|
+
*/
|
|
2329
|
+
trySendJson(r: HttpRequest, status: i32): Result<i32, string> {
|
|
2330
|
+
return this.checked(this.sendJsonCore(r, status))
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
private sendJsonCore(r: HttpRequest, status: i32): i32 {
|
|
1502
2334
|
if (!this.jsonBegun) {
|
|
1503
|
-
|
|
2335
|
+
return this.refused("HttpHandler.sendJson: no value was begun with json()")
|
|
1504
2336
|
}
|
|
1505
2337
|
this.jsonBegun = false
|
|
1506
2338
|
if (!this.writer.complete()) {
|
|
1507
|
-
return this.
|
|
2339
|
+
return this.problemCore(r, toI32(500), "Internal Server Error", "the response did not fit its buffer")
|
|
1508
2340
|
}
|
|
1509
|
-
return this.
|
|
2341
|
+
return this.sendCore(r, status, this.jsonType, this.scratch, HANDLER_ZERO, this.writer.end)
|
|
1510
2342
|
}
|
|
1511
2343
|
|
|
1512
2344
|
/**
|
|
@@ -1524,8 +2356,26 @@ export class HttpHandler {
|
|
|
1524
2356
|
* the connection was closed for it
|
|
1525
2357
|
*/
|
|
1526
2358
|
problem(r: HttpRequest, status: i32, title: string, detail: string): i32 {
|
|
2359
|
+
return this.kept(this.problemCore(r, status, title, detail))
|
|
2360
|
+
}
|
|
2361
|
+
|
|
2362
|
+
/**
|
|
2363
|
+
* `problem`, with a broken contract answered as an `Err` instead of a
|
|
2364
|
+
* panic: a request not taken or already answered, or a status outside 200
|
|
2365
|
+
* to 599, leaves `r` as it was.
|
|
2366
|
+
* @param r a request taken and not yet answered; the handler's again after an `Ok`
|
|
2367
|
+
* @param status the response status, from 200 to 599, also written into the object
|
|
2368
|
+
* @param title the status's reason phrase, or empty to leave it out
|
|
2369
|
+
* @param detail a sentence for a person about this occurrence, or empty to leave it out
|
|
2370
|
+
* @returns `Ok` with what `problem` answers, or `Err` saying which contract the call broke
|
|
2371
|
+
*/
|
|
2372
|
+
tryProblem(r: HttpRequest, status: i32, title: string, detail: string): Result<i32, string> {
|
|
2373
|
+
return this.checked(this.problemCore(r, status, title, detail))
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
private problemCore(r: HttpRequest, status: i32, title: string, detail: string): i32 {
|
|
1527
2377
|
const length: i32 = this.writeProblem(status, title, detail)
|
|
1528
|
-
return this.
|
|
2378
|
+
return this.sendCore(r, status, this.problemType, this.scratch, HANDLER_ZERO, length)
|
|
1529
2379
|
}
|
|
1530
2380
|
|
|
1531
2381
|
/**
|
|
@@ -1576,10 +2426,33 @@ export class HttpHandler {
|
|
|
1576
2426
|
* the connection was closed for it
|
|
1577
2427
|
*/
|
|
1578
2428
|
refuse(r: HttpRequest): i32 {
|
|
2429
|
+
return this.kept(this.refuseCore(r))
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2432
|
+
/**
|
|
2433
|
+
* `refuse`, with a broken contract answered as an `Err` instead of a
|
|
2434
|
+
* panic: a request not taken or already answered leaves `r` as it was.
|
|
2435
|
+
* @param r a request whose `route` is negative, taken and not yet answered; the handler's again after an `Ok`
|
|
2436
|
+
* @returns `Ok` with what `refuse` answers, or `Err` saying which contract the call broke
|
|
2437
|
+
*/
|
|
2438
|
+
tryRefuse(r: HttpRequest): Result<i32, string> {
|
|
2439
|
+
return this.checked(this.refuseCore(r))
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
private refuseCore(r: HttpRequest): i32 {
|
|
1579
2443
|
if (r.route === ROUTER_METHOD_NOT_ALLOWED) {
|
|
1580
2444
|
const length: i32 = this.writeProblem(toI32(405), "Method Not Allowed", "")
|
|
1581
|
-
|
|
1582
|
-
|
|
2445
|
+
// The `Allow` value goes in the pair the constructor made, one store a
|
|
2446
|
+
// 405; the length test, after `allowFor` runs, proves the index, and
|
|
2447
|
+
// its other branch, never taken, answers the same by making the pair.
|
|
2448
|
+
const allow: u8[] = this.allowFor(r)
|
|
2449
|
+
const values: u8[][] = this.allowValues
|
|
2450
|
+
if (toI32(values.length) >= 2) {
|
|
2451
|
+
values[1] = allow
|
|
2452
|
+
} else {
|
|
2453
|
+
this.allowValues = [this.problemType, allow]
|
|
2454
|
+
}
|
|
2455
|
+
return this.sendFieldsCore(
|
|
1583
2456
|
r,
|
|
1584
2457
|
toI32(405),
|
|
1585
2458
|
this.allowNames,
|
|
@@ -1589,7 +2462,37 @@ export class HttpHandler {
|
|
|
1589
2462
|
length
|
|
1590
2463
|
)
|
|
1591
2464
|
}
|
|
1592
|
-
return this.
|
|
2465
|
+
return this.problemCore(r, toI32(404), "Not Found", "")
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
/** Records why a call broke its contract and answers `HANDLER_REFUSED`. */
|
|
2469
|
+
private refused(why: string): i32 {
|
|
2470
|
+
this.refusal = why
|
|
2471
|
+
return HANDLER_REFUSED
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
/** A core's answer for a public method: the panic it keeps for a broken contract, or the answer. */
|
|
2475
|
+
private kept(code: i32): i32 {
|
|
2476
|
+
if (code === HANDLER_REFUSED) {
|
|
2477
|
+
panic(this.refusal)
|
|
2478
|
+
}
|
|
2479
|
+
return code
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
/** A core's answer for a `try` twin. */
|
|
2483
|
+
private checked(code: i32): Result<i32, string> {
|
|
2484
|
+
if (code === HANDLER_REFUSED) {
|
|
2485
|
+
return Err(this.refusal)
|
|
2486
|
+
}
|
|
2487
|
+
return Ok(code)
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
/** Gives up on `r`'s answer: its connection is closed, since the response cannot be completed, and `r` goes back. */
|
|
2491
|
+
private abandon(r: HttpRequest): void {
|
|
2492
|
+
if (this.live(r)) {
|
|
2493
|
+
this.closeSlot(r.slot)
|
|
2494
|
+
}
|
|
2495
|
+
this.release(r)
|
|
1593
2496
|
}
|
|
1594
2497
|
|
|
1595
2498
|
/**
|
|
@@ -1599,13 +2502,17 @@ export class HttpHandler {
|
|
|
1599
2502
|
*/
|
|
1600
2503
|
private allowFor(r: HttpRequest): u8[] {
|
|
1601
2504
|
const router: Router = this.router
|
|
1602
|
-
const
|
|
2505
|
+
const starts: i32[] = router.methodStart
|
|
2506
|
+
const ends: i32[] = router.methodEnd
|
|
2507
|
+
const methods: i32 = toI32(starts.length) < toI32(ends.length) ? toI32(starts.length) : toI32(ends.length)
|
|
1603
2508
|
let mask: i64 = 0
|
|
1604
|
-
for (let m: i32 = 0; m < methods; m++) {
|
|
2509
|
+
for (let m: i32 = 0; m < methods && m < toI32(starts.length) && m < toI32(ends.length); m++) {
|
|
2510
|
+
const start: i32 = starts[m]
|
|
2511
|
+
const end: i32 = ends[m]
|
|
1605
2512
|
const route: i32 = router.match(
|
|
1606
2513
|
router.methodBytes,
|
|
1607
|
-
|
|
1608
|
-
|
|
2514
|
+
start,
|
|
2515
|
+
end,
|
|
1609
2516
|
r.head,
|
|
1610
2517
|
r.targetStart,
|
|
1611
2518
|
r.targetEnd,
|
|
@@ -1615,18 +2522,20 @@ export class HttpHandler {
|
|
|
1615
2522
|
mask = mask | (toI64(1) << toI64(m))
|
|
1616
2523
|
}
|
|
1617
2524
|
}
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
2525
|
+
const masks: i64[] = this.allowMasks
|
|
2526
|
+
const made: u8[][] = this.allowBytes
|
|
2527
|
+
for (let k: i32 = 0; k < toI32(masks.length) && k < toI32(made.length); k++) {
|
|
2528
|
+
if (masks[k] === mask) {
|
|
2529
|
+
return made[k]
|
|
1621
2530
|
}
|
|
1622
2531
|
}
|
|
1623
2532
|
let get: boolean = false
|
|
1624
2533
|
let head: boolean = false
|
|
1625
2534
|
const value: u8[] = []
|
|
1626
|
-
for (let m: i32 = 0; m < methods; m++) {
|
|
2535
|
+
for (let m: i32 = 0; m < methods && m < toI32(starts.length) && m < toI32(ends.length); m++) {
|
|
1627
2536
|
if ((mask & (toI64(1) << toI64(m))) !== toI64(0)) {
|
|
1628
|
-
const start: i32 =
|
|
1629
|
-
const end: i32 =
|
|
2537
|
+
const start: i32 = starts[m]
|
|
2538
|
+
const end: i32 = ends[m]
|
|
1630
2539
|
get = get || handlerSpanIs(router.methodBytes, start, end, "GET")
|
|
1631
2540
|
head = head || handlerSpanIs(router.methodBytes, start, end, "HEAD")
|
|
1632
2541
|
handlerAllowAppend(value, router.methodBytes, start, end)
|
|
@@ -1655,11 +2564,136 @@ const handlerAllowAppend = (value: u8[], bytes: u8[], start: i32, end: i32): voi
|
|
|
1655
2564
|
}
|
|
1656
2565
|
}
|
|
1657
2566
|
|
|
2567
|
+
/**
|
|
2568
|
+
* The first `n` elements' length as a `u16`, at most 65535, and 0 when `n` is
|
|
2569
|
+
* not positive: an allocation of a `u16` length is not an `array-length`
|
|
2570
|
+
* site, so the helpers below allocate once up to it and push only past it.
|
|
2571
|
+
*/
|
|
2572
|
+
const handlerHead = (n: i32): u16 => {
|
|
2573
|
+
if (n <= 0) {
|
|
2574
|
+
return 0
|
|
2575
|
+
}
|
|
2576
|
+
return toU16(n < 65535 ? n : toI32(65535))
|
|
2577
|
+
}
|
|
2578
|
+
|
|
2579
|
+
/** `n` copies of `fill`, none when `n` is not positive: one allocation up to 65535, pushed past it. */
|
|
2580
|
+
const handlerInts = (n: i32, fill: i32): i32[] => {
|
|
2581
|
+
const out: i32[] = new Array<i32>(handlerHead(n))
|
|
2582
|
+
if (fill !== 0) {
|
|
2583
|
+
out.fill(fill)
|
|
2584
|
+
}
|
|
2585
|
+
for (let k: i32 = 65535; k < n; k++) {
|
|
2586
|
+
out.push(fill)
|
|
2587
|
+
}
|
|
2588
|
+
return out
|
|
2589
|
+
}
|
|
2590
|
+
|
|
2591
|
+
/** `n` flags, each false, made as `handlerInts` makes its numbers. */
|
|
2592
|
+
const handlerFlags = (n: i32): boolean[] => {
|
|
2593
|
+
const out: boolean[] = new Array<boolean>(handlerHead(n))
|
|
2594
|
+
for (let k: i32 = 65535; k < n; k++) {
|
|
2595
|
+
out.push(false)
|
|
2596
|
+
}
|
|
2597
|
+
return out
|
|
2598
|
+
}
|
|
2599
|
+
|
|
2600
|
+
/** `n` zero bytes, made as `handlerInts` makes its numbers. */
|
|
2601
|
+
const handlerBytes = (n: i32): u8[] => {
|
|
2602
|
+
const out: u8[] = new Array<u8>(handlerHead(n))
|
|
2603
|
+
for (let k: i32 = 65535; k < n; k++) {
|
|
2604
|
+
out.push(0)
|
|
2605
|
+
}
|
|
2606
|
+
return out
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2609
|
+
/** The length of `Router.match`'s scratch for routes of up to `maxParams` parameters: two a parameter and two more. */
|
|
2610
|
+
const handlerProbeLength = (maxParams: i32): i32 => toI32(toI64(maxParams) * toI64(2) + toI64(2))
|
|
2611
|
+
|
|
2612
|
+
/**
|
|
2613
|
+
* The nanoseconds from the `monotonicNanos` instant `from` to `to`, or 0 when
|
|
2614
|
+
* `to` is not after it. Worked in `u64`, which has the bits of the signed
|
|
2615
|
+
* difference once `to` is the later.
|
|
2616
|
+
*/
|
|
2617
|
+
const handlerSince = (from: i64, to: i64): i64 => (to > from ? toI64(toU64(to) - toU64(from)) : toI64(0))
|
|
2618
|
+
|
|
2619
|
+
/**
|
|
2620
|
+
* The `monotonicNanos` instant `span` nanoseconds after `now`. Worked in
|
|
2621
|
+
* `u64`, whose wrapping sum has the bits of the signed one: an instant is
|
|
2622
|
+
* centuries short of 2^63 and the span a constant of a second or less.
|
|
2623
|
+
*/
|
|
2624
|
+
const handlerLater = (now: i64, span: i64): i64 => toI64(toU64(now) + toU64(span))
|
|
2625
|
+
|
|
2626
|
+
/** Place `start + k` of a ring of `size` places, in `[0, size)`; -1 for an empty ring or a negative place. */
|
|
2627
|
+
const handlerRing = (start: i32, k: i32, size: i32): i32 => {
|
|
2628
|
+
const n: i64 = toI64(size)
|
|
2629
|
+
if (n < 1 || start < 0 || k < 0) {
|
|
2630
|
+
return -1
|
|
2631
|
+
}
|
|
2632
|
+
// Two non-negative `i32`s sum below 2^32, so the `i64` sum is exact.
|
|
2633
|
+
const at: i64 = toI64(start) + toI64(k)
|
|
2634
|
+
return toI32(at % n)
|
|
2635
|
+
}
|
|
2636
|
+
|
|
2637
|
+
/**
|
|
2638
|
+
* A handler as `new HttpHandler(limits, listener, router)` makes one, with
|
|
2639
|
+
* what that constructor panics on answered as an `Err`: limits below their
|
|
2640
|
+
* least, or a readiness loop `pollCreate` could not make.
|
|
2641
|
+
* @param limits the caps and timeouts, as the constructor reads them
|
|
2642
|
+
* @param listener the listening socket's descriptor, still the program's to close
|
|
2643
|
+
* @param router the routes, which the handler keeps and consults at each request head
|
|
2644
|
+
* @returns `Ok` with the handler, or `Err` saying which limit is below its least or how `pollCreate` failed
|
|
2645
|
+
*/
|
|
2646
|
+
export const openHttpHandler = (
|
|
2647
|
+
limits: HandlerLimits,
|
|
2648
|
+
listener: i32,
|
|
2649
|
+
router: Router
|
|
2650
|
+
): Result<HttpHandler, string> => {
|
|
2651
|
+
const fault: string = handlerLimitsFault(limits)
|
|
2652
|
+
if (fault.length > 0) {
|
|
2653
|
+
return Err(fault)
|
|
2654
|
+
}
|
|
2655
|
+
const loop: i32 = pollCreate()
|
|
2656
|
+
if (loop < 0) {
|
|
2657
|
+
return Err(`HttpHandler: pollCreate failed: ${loop}`)
|
|
2658
|
+
}
|
|
2659
|
+
return Ok(new HttpHandler(limits, listener, router, loop))
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
/** Why `limits` cannot make a handler, or `""` when it can. */
|
|
2663
|
+
const handlerLimitsFault = (limits: HandlerLimits): string => {
|
|
2664
|
+
if (limits.slots < 1 || limits.jsonBytes < 256 || limits.maxEvents < 1 || limits.bodyTimeout < 1) {
|
|
2665
|
+
return `HttpHandler: slots ${limits.slots}, jsonBytes ${limits.jsonBytes}, maxEvents ${limits.maxEvents} and bodyTimeout ${limits.bodyTimeout} need at least 1, 256, 1 and 1`
|
|
2666
|
+
}
|
|
2667
|
+
if (limits.maxDecoded < 0) {
|
|
2668
|
+
return `HttpHandler: maxDecoded ${limits.maxDecoded} is negative`
|
|
2669
|
+
}
|
|
2670
|
+
if (limits.minEvents < 1 || limits.maxWaitMs < 0 || limits.maxWaitMs > HANDLER_MAX_WAIT_MS) {
|
|
2671
|
+
return `HttpHandler: minEvents ${limits.minEvents} and maxWaitMs ${limits.maxWaitMs} need at least 1 and 0 to ${HANDLER_MAX_WAIT_MS}`
|
|
2672
|
+
}
|
|
2673
|
+
return ""
|
|
2674
|
+
}
|
|
2675
|
+
|
|
2676
|
+
/** Whether `buf[off .. off + len)` is not a window of `buf`. */
|
|
2677
|
+
const handlerOutside = (buf: u8[], off: i32, len: i32): boolean =>
|
|
2678
|
+
off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(buf.length)
|
|
2679
|
+
|
|
2680
|
+
/** `a + b`, wrapped to an `i32`: the sum is taken in `i64`, where two `i32`s cannot overflow. */
|
|
2681
|
+
const handlerAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
|
|
2682
|
+
|
|
2683
|
+
/** `a - b`, wrapped to an `i32`, taken in `i64` as `handlerAdd` takes a sum. */
|
|
2684
|
+
const handlerSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
|
|
2685
|
+
|
|
2686
|
+
/** Where slot `part` of field `i` sits in `spans`, four to a field, or -1 when that is past an `i32`. */
|
|
2687
|
+
const handlerSpanSlot = (i: i32, part: i32): i32 => {
|
|
2688
|
+
const at: i64 = toI64(i) * toI64(4) + toI64(part)
|
|
2689
|
+
return at < toI64(0) || at > toI64(2147483647) ? -1 : toI32(at)
|
|
2690
|
+
}
|
|
2691
|
+
|
|
1658
2692
|
/** A buffer size of at least `need`: `have` doubled until it is, starting from 64. */
|
|
1659
2693
|
const handlerGrown = (have: i32, need: i32): i32 => {
|
|
1660
2694
|
let size: i32 = have < 64 ? 64 : have
|
|
1661
2695
|
while (size < need && size < 1073741824) {
|
|
1662
|
-
size = size
|
|
2696
|
+
size = handlerAdd(size, size)
|
|
1663
2697
|
}
|
|
1664
2698
|
return size < need ? need : size
|
|
1665
2699
|
}
|
|
@@ -1667,31 +2701,38 @@ const handlerGrown = (have: i32, need: i32): i32 => {
|
|
|
1667
2701
|
/** Whether `bytes[start .. end)` is exactly `text`. */
|
|
1668
2702
|
const handlerSpanIs = (bytes: u8[], start: i32, end: i32, text: string): boolean => {
|
|
1669
2703
|
const n: i32 = toI32(text.length)
|
|
1670
|
-
if (
|
|
2704
|
+
if (start < 0 || end < start || end > toI32(bytes.length) || handlerSub(end, start) !== n) {
|
|
1671
2705
|
return false
|
|
1672
2706
|
}
|
|
2707
|
+
let at: i32 = start
|
|
1673
2708
|
for (let k: i32 = 0; k < n; k++) {
|
|
1674
|
-
if (toI32(bytes
|
|
2709
|
+
if (at < 0 || at >= toI32(bytes.length) || toI32(bytes[at]) !== toI32(text.charCodeAt(k))) {
|
|
1675
2710
|
return false
|
|
1676
2711
|
}
|
|
2712
|
+
at = handlerAdd(at, 1)
|
|
1677
2713
|
}
|
|
1678
2714
|
return true
|
|
1679
2715
|
}
|
|
1680
2716
|
|
|
1681
2717
|
/** `bytes[start .. end)` as a decimal number that is not negative and fits an `i32`, or -1. */
|
|
1682
2718
|
const handlerDecimal = (bytes: u8[], start: i32, end: i32): i32 => {
|
|
1683
|
-
if (start < 0 || end <= start || end
|
|
2719
|
+
if (start < 0 || end <= start || end > toI32(bytes.length) || handlerSub(end, start) > 10) {
|
|
1684
2720
|
return -1
|
|
1685
2721
|
}
|
|
1686
|
-
let
|
|
2722
|
+
let value: i32 = 0
|
|
1687
2723
|
for (let k: i32 = start; k < end && k < toI32(bytes.length); k++) {
|
|
1688
|
-
const
|
|
1689
|
-
|
|
2724
|
+
const c: i32 = toI32(bytes[k])
|
|
2725
|
+
// A value past 999999999 has ten digits already, and an eleventh is past an `i32`.
|
|
2726
|
+
if (c < 48 || c > 57 || value < 0 || value > 999999999) {
|
|
1690
2727
|
return -1
|
|
1691
2728
|
}
|
|
1692
|
-
|
|
2729
|
+
const next: i64 = toI64(value) * toI64(10) + toI64(c - 48)
|
|
2730
|
+
if (next > toI64(2147483647)) {
|
|
2731
|
+
return -1
|
|
2732
|
+
}
|
|
2733
|
+
value = toI32(next)
|
|
1693
2734
|
}
|
|
1694
|
-
return
|
|
2735
|
+
return value
|
|
1695
2736
|
}
|
|
1696
2737
|
|
|
1697
2738
|
/**
|
|
@@ -1699,6 +2740,11 @@ const handlerDecimal = (bytes: u8[], start: i32, end: i32): i32 => {
|
|
|
1699
2740
|
* many bytes it took, packed as `byte | taken << 8`; -1 for a malformed escape.
|
|
1700
2741
|
*/
|
|
1701
2742
|
const handlerFormByte = (bytes: u8[], at: i32, end: i32): i32 => {
|
|
2743
|
+
// Its callers step `at` inside a span of `bytes`; the literal bound keeps
|
|
2744
|
+
// `at + 2` from overflowing, far past any head.
|
|
2745
|
+
if (at < 0 || at >= end || at >= toI32(bytes.length) || at > 1073741823) {
|
|
2746
|
+
return -1
|
|
2747
|
+
}
|
|
1702
2748
|
const c: i32 = toI32(bytes[at])
|
|
1703
2749
|
if (c === 43) {
|
|
1704
2750
|
return 32 | HANDLER_TOOK_ONE
|
|
@@ -1706,12 +2752,15 @@ const handlerFormByte = (bytes: u8[], at: i32, end: i32): i32 => {
|
|
|
1706
2752
|
if (c !== 37) {
|
|
1707
2753
|
return c | HANDLER_TOOK_ONE
|
|
1708
2754
|
}
|
|
1709
|
-
if (at + 2 >= end) {
|
|
2755
|
+
if (at + 2 >= end || at + 2 >= toI32(bytes.length)) {
|
|
1710
2756
|
return -1
|
|
1711
2757
|
}
|
|
1712
|
-
|
|
1713
|
-
const
|
|
1714
|
-
|
|
2758
|
+
// Both bytes read before either call, which the prover takes to move lengths.
|
|
2759
|
+
const high: i32 = toI32(bytes[at + 1])
|
|
2760
|
+
const low: i32 = toI32(bytes[at + 2])
|
|
2761
|
+
const hi: i32 = http1HexValue(high)
|
|
2762
|
+
const lo: i32 = http1HexValue(low)
|
|
2763
|
+
if (hi < 0 || lo < 0 || hi > 15 || lo > 15) {
|
|
1715
2764
|
return -1
|
|
1716
2765
|
}
|
|
1717
2766
|
return (hi * 16 + lo) | HANDLER_TOOK_THREE
|
|
@@ -1722,27 +2771,34 @@ const handlerFormIs = (bytes: u8[], start: i32, end: i32, name: string): boolean
|
|
|
1722
2771
|
const n: i32 = toI32(name.length)
|
|
1723
2772
|
let at: i32 = start
|
|
1724
2773
|
let k: i32 = 0
|
|
1725
|
-
|
|
2774
|
+
// A head is capped far below the literal bound, which keeps `at + 3` in an `i32`.
|
|
2775
|
+
while (at < end && at >= 0 && at < 1073741824) {
|
|
1726
2776
|
const packed: i32 = handlerFormByte(bytes, at, end)
|
|
1727
2777
|
if (packed < 0 || k >= n || (packed & 255) !== toI32(name.charCodeAt(k))) {
|
|
1728
2778
|
return false
|
|
1729
2779
|
}
|
|
1730
|
-
|
|
2780
|
+
// One byte taken, or three for an escape; `handlerFormByte` kept `at` far
|
|
2781
|
+
// below the top of an `i32`.
|
|
2782
|
+
if ((packed & HANDLER_TOOK_THREE) === HANDLER_TOOK_THREE) {
|
|
2783
|
+
at += 3
|
|
2784
|
+
} else {
|
|
2785
|
+
at += 1
|
|
2786
|
+
}
|
|
1731
2787
|
k += 1
|
|
1732
2788
|
}
|
|
1733
2789
|
return k === n
|
|
1734
2790
|
}
|
|
1735
2791
|
|
|
1736
2792
|
/**
|
|
1737
|
-
* `bytes[start ..
|
|
2793
|
+
* `bytes[start .. start + len)` form-decoded into `out` from 0: its length,
|
|
1738
2794
|
* `ROUTER_BAD_ESCAPE`, or `ROUTER_NO_ROOM`. `%00` decodes like any byte, as a
|
|
1739
2795
|
* route parameter's does.
|
|
1740
2796
|
*/
|
|
1741
|
-
const handlerFormDecode = (bytes: u8[], start: i32,
|
|
2797
|
+
const handlerFormDecode = (bytes: u8[], start: i32, len: i32, out: u8[]): i32 => {
|
|
1742
2798
|
const length: i32 = percentDecode(
|
|
1743
2799
|
bytes,
|
|
1744
2800
|
start,
|
|
1745
|
-
|
|
2801
|
+
len,
|
|
1746
2802
|
out,
|
|
1747
2803
|
HANDLER_ZERO,
|
|
1748
2804
|
URL_DECODE_FORM | URL_DECODE_ALLOW_NUL
|
|
@@ -1752,3 +2808,40 @@ const handlerFormDecode = (bytes: u8[], start: i32, end: i32, out: u8[]): i32 =>
|
|
|
1752
2808
|
}
|
|
1753
2809
|
return length < 0 ? ROUTER_BAD_ESCAPE : length
|
|
1754
2810
|
}
|
|
2811
|
+
|
|
2812
|
+
/**
|
|
2813
|
+
* `given` followed by `added[0 .. count)`, in the array `byLength` keeps for
|
|
2814
|
+
* that length: made the first time an answer has that many fields, and
|
|
2815
|
+
* written over by each answer after.
|
|
2816
|
+
*/
|
|
2817
|
+
const handlerWithAdded = (byLength: u8[][][], given: u8[][], added: u8[][], count: i32): u8[][] => {
|
|
2818
|
+
const have: i32 = toI32(given.length)
|
|
2819
|
+
const length: i32 = handlerAdd(have, count)
|
|
2820
|
+
if (length < 0 || added.length === 0) {
|
|
2821
|
+
return given
|
|
2822
|
+
}
|
|
2823
|
+
const first: u8[] = added.length > 0 ? added[0] : []
|
|
2824
|
+
while (toI32(byLength.length) <= length) {
|
|
2825
|
+
const size: i32 = toI32(byLength.length)
|
|
2826
|
+
const made: u8[][] = []
|
|
2827
|
+
for (let k: i32 = 0; k < size; k++) {
|
|
2828
|
+
made.push(first)
|
|
2829
|
+
}
|
|
2830
|
+
byLength.push(made)
|
|
2831
|
+
}
|
|
2832
|
+
if (length >= toI32(byLength.length)) {
|
|
2833
|
+
return given
|
|
2834
|
+
}
|
|
2835
|
+
const out: u8[][] = byLength[length]
|
|
2836
|
+
for (let k: i32 = 0; k < toI32(given.length) && k < toI32(out.length); k++) {
|
|
2837
|
+
out[k] = given[k]
|
|
2838
|
+
}
|
|
2839
|
+
let at: i32 = have
|
|
2840
|
+
for (let k: i32 = 0; k < count && k < toI32(added.length); k++) {
|
|
2841
|
+
if (at >= 0 && at < toI32(out.length)) {
|
|
2842
|
+
out[at] = added[k]
|
|
2843
|
+
}
|
|
2844
|
+
at = handlerAdd(at, 1)
|
|
2845
|
+
}
|
|
2846
|
+
return out
|
|
2847
|
+
}
|