@amritk/nish-x86_64-linux 0.49.1 → 0.51.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 +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +237 -160
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/net/http-server.ts
CHANGED
|
@@ -72,15 +72,19 @@ import {
|
|
|
72
72
|
H1_AGAIN,
|
|
73
73
|
H1_ALPN,
|
|
74
74
|
H1_BODY,
|
|
75
|
+
H1_CLOSED,
|
|
75
76
|
H1_DONE,
|
|
76
77
|
H1_END,
|
|
77
78
|
H1_ERROR,
|
|
79
|
+
H1_INVALID,
|
|
78
80
|
H1_NEED_MORE,
|
|
79
81
|
H1_POOL_FULL,
|
|
80
82
|
H1_REQUEST,
|
|
81
83
|
H1_WANT_READ,
|
|
82
84
|
H1_WANT_WRITE,
|
|
83
85
|
H1_WRITE,
|
|
86
|
+
H1_WS_CLOSE,
|
|
87
|
+
H1_WS_MESSAGE,
|
|
84
88
|
Http1Config,
|
|
85
89
|
Http1Connection,
|
|
86
90
|
Http1Server,
|
|
@@ -119,7 +123,7 @@ import {
|
|
|
119
123
|
H3_REQUEST_CANCELLED,
|
|
120
124
|
H3_REQUEST_REJECTED,
|
|
121
125
|
} from "nish/net/http3-frame"
|
|
122
|
-
import { H3_SERVER_RECEIVE_BURST, Http3Server } from "nish/net/http3-server"
|
|
126
|
+
import { H3_SERVER_RECEIVE_BURST, Http3Server, openHttp3Server } from "nish/net/http3-server"
|
|
123
127
|
import { QUIC_CONN_CID_LENGTH, QuicServerConfig } from "nish/net/quic"
|
|
124
128
|
import { QUIC_MAX_CID_LENGTH } from "nish/net/quic-packet"
|
|
125
129
|
import {
|
|
@@ -147,7 +151,8 @@ const HTTP_SERVER_TOO_LARGE: i32 = 413
|
|
|
147
151
|
const HTTP_SERVER_UNAVAILABLE: i32 = 503
|
|
148
152
|
|
|
149
153
|
// A request's life: free, reading its body, whole but parked behind a full output, queued for `take`,
|
|
150
|
-
// held by the program, streaming its response, and flushing the response's last bytes
|
|
154
|
+
// held by the program, streaming its response, and flushing the response's last bytes; or, once
|
|
155
|
+
// `acceptWebSocket` took its upgrade, a WebSocket the program holds until `closeWebSocket`.
|
|
151
156
|
const HTTP_SERVER_FREE: i32 = 0
|
|
152
157
|
const HTTP_SERVER_READING: i32 = 1
|
|
153
158
|
const HTTP_SERVER_PARKED: i32 = 2
|
|
@@ -155,6 +160,9 @@ const HTTP_SERVER_QUEUED: i32 = 3
|
|
|
155
160
|
const HTTP_SERVER_HELD: i32 = 4
|
|
156
161
|
const HTTP_SERVER_STREAMING: i32 = 5
|
|
157
162
|
const HTTP_SERVER_FLUSHING: i32 = 6
|
|
163
|
+
const HTTP_SERVER_SOCKET: i32 = 7
|
|
164
|
+
/** The close code a WebSocket gets when its connection ends with no close frame: 1006, which `nish/net/websocket` reserves as report-only. */
|
|
165
|
+
const HTTP_SERVER_WS_ABNORMAL: i32 = 1006
|
|
158
166
|
/** `checkParam`'s `end` for a parameter's start offset (an i32 under `--number-mode f64`). */
|
|
159
167
|
const HTTP_SERVER_PARAM_START: i32 = 0
|
|
160
168
|
/** `checkParam`'s `end` for a parameter's end offset. */
|
|
@@ -411,6 +419,46 @@ export class HttpServerRequest {
|
|
|
411
419
|
* @internal Set by `HttpServer.before` and cleared when a request is read into it, in this module.
|
|
412
420
|
*/
|
|
413
421
|
stepped: boolean = false
|
|
422
|
+
/**
|
|
423
|
+
* Whether `acceptWebSocket` took this request's upgrade: it is a WebSocket
|
|
424
|
+
* now, the program's until `closeWebSocket`, and comes out of `take` again
|
|
425
|
+
* each time a message, a close or room to send arrives.
|
|
426
|
+
*/
|
|
427
|
+
socket: boolean = false
|
|
428
|
+
/**
|
|
429
|
+
* Whether a WebSocket message is held for the program: `messageData` from
|
|
430
|
+
* `messageStart`, `messageLength` bytes, valid until `received`, or until it
|
|
431
|
+
* comes out `closed`, which clears this. No further frame is read till then.
|
|
432
|
+
*/
|
|
433
|
+
message: boolean = false
|
|
434
|
+
/** The held message's opcode: 1 (`WS_OP_TEXT`) for text, which the decoder has checked is UTF-8, or 2 (`WS_OP_BINARY`). */
|
|
435
|
+
opcode: i32 = 0
|
|
436
|
+
/**
|
|
437
|
+
* The bytes of the held message, the connection's own decoder buffer, not a copy:
|
|
438
|
+
* `messageData[messageStart .. messageStart + messageLength)`.
|
|
439
|
+
*/
|
|
440
|
+
messageData: u8[]
|
|
441
|
+
/** Where the held message starts in `messageData`. */
|
|
442
|
+
messageStart: i32 = 0
|
|
443
|
+
/** How many bytes the held message has. */
|
|
444
|
+
messageLength: i32 = 0
|
|
445
|
+
/**
|
|
446
|
+
* Whether the connection has room again after `sendFrame` answered
|
|
447
|
+
* `H1_AGAIN`; cleared by the next `sendFrame`.
|
|
448
|
+
*/
|
|
449
|
+
writable: boolean = false
|
|
450
|
+
/**
|
|
451
|
+
* Whether the WebSocket is over: the peer sent a close, broke the
|
|
452
|
+
* protocol, or the connection ended. `closeCode` says why; the program
|
|
453
|
+
* hands the request back with `closeWebSocket`.
|
|
454
|
+
*/
|
|
455
|
+
closed: boolean = false
|
|
456
|
+
/**
|
|
457
|
+
* Why the WebSocket closed: the peer's close code (1005 when its close
|
|
458
|
+
* carried none), the code a protocol error was answered with (1002, 1007,
|
|
459
|
+
* 1009), or 1006 when the connection ended with no close.
|
|
460
|
+
*/
|
|
461
|
+
closeCode: i32 = 0
|
|
414
462
|
|
|
415
463
|
/**
|
|
416
464
|
* An empty request for the server's pool; its buffers are made on its first use.
|
|
@@ -427,6 +475,7 @@ export class HttpServerRequest {
|
|
|
427
475
|
this.trailerValues = []
|
|
428
476
|
this.addedNames = []
|
|
429
477
|
this.addedValues = []
|
|
478
|
+
this.messageData = []
|
|
430
479
|
}
|
|
431
480
|
|
|
432
481
|
/**
|
|
@@ -749,14 +798,28 @@ const httpServerCell = (row: i32, width: i32, col: i32, length: i32): i32 => {
|
|
|
749
798
|
}
|
|
750
799
|
|
|
751
800
|
/**
|
|
752
|
-
* The `monotonicNanos` instant `ns` nanoseconds after `at
|
|
753
|
-
*
|
|
754
|
-
* short of 2^63 and every delay a constant or a checked limit
|
|
801
|
+
* The `monotonicNanos` instant `ns` nanoseconds after `at`, or the last `i64`
|
|
802
|
+
* when the sum would pass it, which no clock reading does: an instant is
|
|
803
|
+
* centuries short of 2^63 and every delay a constant or a checked limit.
|
|
804
|
+
* Both are at least 0, so they are summed in `u64`, where two values below
|
|
805
|
+
* 2^63 cannot wrap, and the sum is turned back only once it is known to fit.
|
|
806
|
+
* A negative `at` or `ns`, which no caller passes, answers `at`.
|
|
755
807
|
*/
|
|
756
|
-
const httpServerAfter = (at: i64, ns: i64): i64 =>
|
|
808
|
+
const httpServerAfter = (at: i64, ns: i64): i64 => {
|
|
809
|
+
if (at < 0 || ns < 0) {
|
|
810
|
+
return at
|
|
811
|
+
}
|
|
812
|
+
const sum: u64 = toU64(at) + toU64(ns)
|
|
813
|
+
const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
|
|
814
|
+
return sum > top ? toI64(top) : toI64(sum)
|
|
815
|
+
}
|
|
757
816
|
|
|
758
|
-
/**
|
|
759
|
-
|
|
817
|
+
/**
|
|
818
|
+
* `until - now` in nanoseconds, or 0 once `until` is not after `now`. An
|
|
819
|
+
* instant is never negative, so the difference of the later and a `now` of
|
|
820
|
+
* at least 0 fits an `i64`; a negative `now` answers 0 too.
|
|
821
|
+
*/
|
|
822
|
+
const httpServerLeft = (until: i64, now: i64): i64 => (until > now && now >= 0 ? until - now : toI64(0))
|
|
760
823
|
|
|
761
824
|
/**
|
|
762
825
|
* Whether more than `ns` nanoseconds lie between the `monotonicNanos`
|
|
@@ -1467,8 +1530,9 @@ export class HttpServer {
|
|
|
1467
1530
|
* field on every HTTP/1.1 and HTTP/2 response. Each connection holds at most
|
|
1468
1531
|
* `quic.maxStreamsBidi` requests and a quarter of `limits.requests`; a
|
|
1469
1532
|
* stream past that is rejected with H3_REQUEST_REJECTED. Panics on a second
|
|
1470
|
-
* HTTP/3 listener, a `maxFieldSectionSize` past `limits.maxHead`,
|
|
1471
|
-
* `webtransportSessions` above 0
|
|
1533
|
+
* HTTP/3 listener, a `maxFieldSectionSize` past `limits.maxHead`,
|
|
1534
|
+
* `webtransportSessions` above 0, or a `slots` or timeout `openHttp3Server`
|
|
1535
|
+
* refuses (slots 1 to 65536, timeouts at least 1). Serve it with `waitSigned`.
|
|
1472
1536
|
* @param fd the UDP socket's descriptor, still the program's to close
|
|
1473
1537
|
* @param quic the QUIC and TLS settings
|
|
1474
1538
|
* @param config the HTTP/3 caps
|
|
@@ -1490,12 +1554,16 @@ export class HttpServer {
|
|
|
1490
1554
|
if (slots < 1) {
|
|
1491
1555
|
panic(`HttpServer.listenHttp3: slots ${slots} is below 1`)
|
|
1492
1556
|
}
|
|
1557
|
+
const opened = openHttp3Server(quic, config, fd, slots, entropy)
|
|
1558
|
+
if (!opened.ok) {
|
|
1559
|
+
panic(`HttpServer.listenHttp3: ${opened.error}`)
|
|
1560
|
+
}
|
|
1493
1561
|
let per: i64 = toI64(toI32(this.requests.length) / 4)
|
|
1494
1562
|
if (quic.maxStreamsBidi < per) {
|
|
1495
1563
|
per = quic.maxStreamsBidi
|
|
1496
1564
|
}
|
|
1497
1565
|
this.perConnection3 = per < toI64(1) ? 1 : toI32(per)
|
|
1498
|
-
this.h3 =
|
|
1566
|
+
this.h3 = opened.value
|
|
1499
1567
|
this.h3Fd = fd
|
|
1500
1568
|
this.h3Gen = httpServerZeros(toI64(slots), toI32(0))
|
|
1501
1569
|
this.h3Count = httpServerZeros(toI64(slots), toI32(0))
|
|
@@ -1568,7 +1636,7 @@ export class HttpServer {
|
|
|
1568
1636
|
* QUIC handshake with `key`, closes what is past its timeouts, and answers
|
|
1569
1637
|
* how many whole requests are waiting for `take`.
|
|
1570
1638
|
* @param timeoutMs the longest to block, in milliseconds, or -1 to block until something is ready
|
|
1571
|
-
* @param key the certificate's P-256
|
|
1639
|
+
* @param key the certificate's private key, P-256 or RSA as `signatureScheme` says; borrowed for the call, still the caller's to wipe
|
|
1572
1640
|
* @returns how many whole requests are queued for `take` after this turn
|
|
1573
1641
|
*/
|
|
1574
1642
|
waitSigned(timeoutMs: i32, key: Secret<u8[]>): i32 {
|
|
@@ -1923,8 +1991,19 @@ export class HttpServer {
|
|
|
1923
1991
|
// The peer's end or a failure: HTTP/1.1 reads it again and answers it as it does today.
|
|
1924
1992
|
return this.h1Decided(s, h1)
|
|
1925
1993
|
}
|
|
1926
|
-
// `netRead` answers at most the bytes it was asked for, so
|
|
1927
|
-
|
|
1994
|
+
// `netRead` answers at most the `room` bytes it was asked for, so `gap`
|
|
1995
|
+
// is never negative and `end` is `got + n`, at most the head's 24. A
|
|
1996
|
+
// larger answer would end the head at its last byte.
|
|
1997
|
+
const size: i32 = toI32(head.length)
|
|
1998
|
+
const room: i32 = size - got
|
|
1999
|
+
let end: i32 = size
|
|
2000
|
+
if (room >= 0) {
|
|
2001
|
+
const gap: i32 = room - n
|
|
2002
|
+
if (gap >= 0) {
|
|
2003
|
+
end = size - gap
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
|
|
1928
2007
|
for (let k: i32 = got; k < end && k < toI32(head.length) && k < toI32(want.length); k++) {
|
|
1929
2008
|
if (toI32(head[k]) !== toI32(want.charCodeAt(k))) {
|
|
1930
2009
|
s.got = end
|
|
@@ -2113,7 +2192,7 @@ export class HttpServer {
|
|
|
2113
2192
|
return
|
|
2114
2193
|
}
|
|
2115
2194
|
let event: i32 = H1_NEED_MORE
|
|
2116
|
-
if (!this.starve(s) && !this.unended(s)) {
|
|
2195
|
+
if (!this.starve(s) && !this.unended(s) && !this.holding(s, conn)) {
|
|
2117
2196
|
event = this.h1Next(s)
|
|
2118
2197
|
while (event !== H1_NEED_MORE && event !== H1_ERROR) {
|
|
2119
2198
|
// A TLS slot's connection is built by the read that brought its first plaintext.
|
|
@@ -2121,7 +2200,10 @@ export class HttpServer {
|
|
|
2121
2200
|
if (live === null) {
|
|
2122
2201
|
break
|
|
2123
2202
|
}
|
|
2124
|
-
|
|
2203
|
+
const socket: HttpServerRequest | null = this.socketOf(s)
|
|
2204
|
+
if (socket !== null) {
|
|
2205
|
+
this.frame(socket, live, event)
|
|
2206
|
+
} else if (event === H1_REQUEST) {
|
|
2125
2207
|
this.begin(s, live)
|
|
2126
2208
|
} else if (event === H1_BODY) {
|
|
2127
2209
|
this.gather(s, live)
|
|
@@ -2130,7 +2212,7 @@ export class HttpServer {
|
|
|
2130
2212
|
} else if (event === H1_WRITE) {
|
|
2131
2213
|
this.resume(s, live)
|
|
2132
2214
|
}
|
|
2133
|
-
if (this.starve(s) || this.unended(s)) {
|
|
2215
|
+
if (this.starve(s) || this.unended(s) || this.holding(s, live)) {
|
|
2134
2216
|
break
|
|
2135
2217
|
}
|
|
2136
2218
|
event = this.h1Next(s)
|
|
@@ -2138,13 +2220,71 @@ export class HttpServer {
|
|
|
2138
2220
|
}
|
|
2139
2221
|
if (event === H1_ERROR) {
|
|
2140
2222
|
// The carrier has written its refusal and ends the connection; a
|
|
2141
|
-
// request it was reading is nobody's.
|
|
2223
|
+
// request it was reading is nobody's, and a WebSocket learns why.
|
|
2142
2224
|
this.releaseReading(s)
|
|
2225
|
+
const socket: HttpServerRequest | null = this.socketOf(s)
|
|
2226
|
+
const live: Http1Connection | null = this.h1Conn(s)
|
|
2227
|
+
if (socket !== null && live !== null) {
|
|
2228
|
+
this.shut(socket, live.status)
|
|
2229
|
+
}
|
|
2143
2230
|
}
|
|
2144
2231
|
const after: Http1Connection | null = this.h1Conn(s)
|
|
2145
2232
|
this.settle(s, after !== null ? after : conn)
|
|
2146
2233
|
}
|
|
2147
2234
|
|
|
2235
|
+
/** The WebSocket slot `s` carries, the program's or closing, or null when it carries HTTP. */
|
|
2236
|
+
private socketOf(s: HttpServerH1Slot): HttpServerRequest | null {
|
|
2237
|
+
const r: HttpServerRequest | null = s.current
|
|
2238
|
+
return r !== null && r.socket ? r : null
|
|
2239
|
+
}
|
|
2240
|
+
|
|
2241
|
+
/**
|
|
2242
|
+
* Whether slot `s` must not be asked for its next event: its WebSocket
|
|
2243
|
+
* holds a message, whose bytes are the decoder's buffer that the next
|
|
2244
|
+
* frame would overwrite. A full output still lets `H1_WRITE` through,
|
|
2245
|
+
* which the carrier gives without decoding.
|
|
2246
|
+
*/
|
|
2247
|
+
private holding(s: HttpServerH1Slot, conn: Http1Connection): boolean {
|
|
2248
|
+
const r: HttpServerRequest | null = this.socketOf(s)
|
|
2249
|
+
return r !== null && r.message && !conn.writeHeld
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
/**
|
|
2253
|
+
* One carrier event of WebSocket `r`: a message is held for the program, a
|
|
2254
|
+
* close is marked, room to send is said, and the program is told by `r`
|
|
2255
|
+
* coming out of `take` again.
|
|
2256
|
+
*/
|
|
2257
|
+
private frame(r: HttpServerRequest, conn: Http1Connection, event: i32): void {
|
|
2258
|
+
if (r.state !== HTTP_SERVER_SOCKET) {
|
|
2259
|
+
// Closing: the carrier only sends what is left, and the slot closes once it is.
|
|
2260
|
+
return
|
|
2261
|
+
}
|
|
2262
|
+
if (event === H1_WS_MESSAGE) {
|
|
2263
|
+
r.message = true
|
|
2264
|
+
r.opcode = conn.opcode
|
|
2265
|
+
r.messageData = conn.data
|
|
2266
|
+
r.messageStart = conn.dataStart
|
|
2267
|
+
r.messageLength = conn.dataLength
|
|
2268
|
+
this.requeue(r)
|
|
2269
|
+
} else if (event === H1_WS_CLOSE) {
|
|
2270
|
+
this.shut(r, conn.status)
|
|
2271
|
+
} else if (event === H1_WRITE) {
|
|
2272
|
+
r.writable = true
|
|
2273
|
+
this.requeue(r)
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
/** Marks WebSocket `r` closed with `code`, once, and queues it for the program. */
|
|
2278
|
+
private shut(r: HttpServerRequest, code: i32): void {
|
|
2279
|
+
if (r.closed) {
|
|
2280
|
+
return
|
|
2281
|
+
}
|
|
2282
|
+
r.closed = true
|
|
2283
|
+
r.closeCode = code
|
|
2284
|
+
r.message = false
|
|
2285
|
+
this.requeue(r)
|
|
2286
|
+
}
|
|
2287
|
+
|
|
2148
2288
|
/**
|
|
2149
2289
|
* Whether `slot`'s response is under way with every byte handed over but
|
|
2150
2290
|
* not yet ended. The carrier finishes a `Content-Length` response once its
|
|
@@ -2214,7 +2354,9 @@ export class HttpServer {
|
|
|
2214
2354
|
this.h1Close(s)
|
|
2215
2355
|
return
|
|
2216
2356
|
}
|
|
2217
|
-
|
|
2357
|
+
// A slot that cannot read yet (no free request, or a WebSocket message
|
|
2358
|
+
// the program holds) is not woken by input it would leave unread.
|
|
2359
|
+
if (s.starved || this.holding(s, conn)) {
|
|
2218
2360
|
wants = wants & ~H1_WANT_READ
|
|
2219
2361
|
}
|
|
2220
2362
|
this.h1Arm(s, wants)
|
|
@@ -2419,6 +2561,9 @@ export class HttpServer {
|
|
|
2419
2561
|
const r: HttpServerRequest | null = s.current
|
|
2420
2562
|
s.current = null
|
|
2421
2563
|
if (r !== null) {
|
|
2564
|
+
if (r.state === HTTP_SERVER_SOCKET) {
|
|
2565
|
+
this.shut(r, HTTP_SERVER_WS_ABNORMAL)
|
|
2566
|
+
}
|
|
2422
2567
|
this.drop(r)
|
|
2423
2568
|
}
|
|
2424
2569
|
}
|
|
@@ -2487,6 +2632,14 @@ export class HttpServer {
|
|
|
2487
2632
|
r.bodyEnded = true
|
|
2488
2633
|
r.stepped = false
|
|
2489
2634
|
r.added = 0
|
|
2635
|
+
r.socket = false
|
|
2636
|
+
r.message = false
|
|
2637
|
+
r.opcode = 0
|
|
2638
|
+
r.messageStart = 0
|
|
2639
|
+
r.messageLength = 0
|
|
2640
|
+
r.writable = false
|
|
2641
|
+
r.closed = false
|
|
2642
|
+
r.closeCode = 0
|
|
2490
2643
|
this.route(r)
|
|
2491
2644
|
const declared: i32 = this.declared(r)
|
|
2492
2645
|
if (declared > toI32(r.body.length) && !(mayStream && this.streamsBody(r.route))) {
|
|
@@ -2676,13 +2829,18 @@ export class HttpServer {
|
|
|
2676
2829
|
* flushing is past caring.
|
|
2677
2830
|
*/
|
|
2678
2831
|
private requeue(r: HttpServerRequest): void {
|
|
2679
|
-
if (r.again || (r
|
|
2832
|
+
if (r.again || !this.programs(r)) {
|
|
2680
2833
|
return
|
|
2681
2834
|
}
|
|
2682
2835
|
r.again = true
|
|
2683
2836
|
this.queuePush(r.index)
|
|
2684
2837
|
}
|
|
2685
2838
|
|
|
2839
|
+
/** Whether `r` is the program's while queued again: held, streaming or an open WebSocket. */
|
|
2840
|
+
private programs(r: HttpServerRequest): boolean {
|
|
2841
|
+
return r.state === HTTP_SERVER_HELD || r.state === HTTP_SERVER_STREAMING || r.state === HTTP_SERVER_SOCKET
|
|
2842
|
+
}
|
|
2843
|
+
|
|
2686
2844
|
/** Takes `r`'s second place out of the queue, closing the gap so the ring still holds each request at most once. */
|
|
2687
2845
|
private unqueue(r: HttpServerRequest): void {
|
|
2688
2846
|
r.again = false
|
|
@@ -2784,7 +2942,7 @@ export class HttpServer {
|
|
|
2784
2942
|
}
|
|
2785
2943
|
// A streaming request queued again is the program's, live or not: it learns of a reset by taking it.
|
|
2786
2944
|
// One whose answer ended since (`end`, `trailers`) is flushing on its own, and stays out of `take`.
|
|
2787
|
-
const held: boolean =
|
|
2945
|
+
const held: boolean = this.programs(r)
|
|
2788
2946
|
if ((r.again && held) || (!r.again && this.live(r))) {
|
|
2789
2947
|
return r
|
|
2790
2948
|
}
|
|
@@ -2801,7 +2959,9 @@ export class HttpServer {
|
|
|
2801
2959
|
/**
|
|
2802
2960
|
* The next request, of any version, which the program now owes an answer;
|
|
2803
2961
|
* a `streaming` request it already holds comes out again each time more
|
|
2804
|
-
* of its body, its end or a reset arrives, at most once per wait
|
|
2962
|
+
* of its body, its end or a reset arrives, at most once per wait, and an
|
|
2963
|
+
* upgraded request (`acceptWebSocket`) comes out again for each message,
|
|
2964
|
+
* for room after `H1_AGAIN` (`writable`) and once it is `closed`. Panics
|
|
2805
2965
|
* when none is `ready`.
|
|
2806
2966
|
* @returns the oldest queued request, the program's until it answers it
|
|
2807
2967
|
*/
|
|
@@ -3602,11 +3762,13 @@ export class HttpServer {
|
|
|
3602
3762
|
* `carry` cannot say this, since the frame is taken as 0 bytes either way.
|
|
3603
3763
|
*/
|
|
3604
3764
|
private carryEnd(r: HttpServerRequest): i32 {
|
|
3765
|
+
// A local zero: the window prover reads locals, not module constants.
|
|
3766
|
+
const zero: i32 = 0
|
|
3605
3767
|
let done: i32 = -1
|
|
3606
3768
|
if (r.kind === HTTP_SERVER_H2) {
|
|
3607
3769
|
const conn: Http2Connection | null = this.h2Conn(r.slot)
|
|
3608
3770
|
if (conn !== null) {
|
|
3609
|
-
done = conn.writeData(toI32(r.stream), r.pending,
|
|
3771
|
+
done = conn.writeData(toI32(r.stream), r.pending, zero, zero, true)
|
|
3610
3772
|
}
|
|
3611
3773
|
if (done === H2_AGAIN) {
|
|
3612
3774
|
return 0
|
|
@@ -3614,7 +3776,7 @@ export class HttpServer {
|
|
|
3614
3776
|
} else {
|
|
3615
3777
|
const h3: Http3Server | null = this.h3
|
|
3616
3778
|
if (h3 !== null) {
|
|
3617
|
-
done = h3.connection(r.slot).writeData(r.stream, r.pending,
|
|
3779
|
+
done = h3.connection(r.slot).writeData(r.stream, r.pending, zero, zero, true)
|
|
3618
3780
|
}
|
|
3619
3781
|
if (done === H3_AGAIN) {
|
|
3620
3782
|
return 0
|
|
@@ -3789,6 +3951,146 @@ export class HttpServer {
|
|
|
3789
3951
|
return w.complete() ? w.end : -1
|
|
3790
3952
|
}
|
|
3791
3953
|
|
|
3954
|
+
// ---- WebSockets ------------------------------------------------------------------
|
|
3955
|
+
|
|
3956
|
+
/**
|
|
3957
|
+
* Accepts `r`'s WebSocket opening handshake, as `Http1Connection` checks it, with
|
|
3958
|
+
* `protocol`, empty for none: the 101 is queued and `r` becomes a WebSocket,
|
|
3959
|
+
* `socket` set, the program's until `closeWebSocket`. It then comes out of
|
|
3960
|
+
* `take` again whenever a message is held (`message`), the connection has
|
|
3961
|
+
* room after `sendFrame` answered `H1_AGAIN` (`writable`), or the WebSocket
|
|
3962
|
+
* is over (`closed`). Only an HTTP/1.1 request upgrades: one on HTTP/2 or
|
|
3963
|
+
* HTTP/3 answers `H1_INVALID` (HTTP/2's extended CONNECT is not served).
|
|
3964
|
+
* On anything but 0, `r` is as it was, the program's to answer as any other
|
|
3965
|
+
* request: 400, or 426 for a wrong `Sec-WebSocket-Version`. The slot's
|
|
3966
|
+
* `idleTimeout` (`Http1Config`) closes a WebSocket nothing moves on, and not
|
|
3967
|
+
* `bodyTimeout`. A draining server upgrades no request, and leaves an open
|
|
3968
|
+
* WebSocket open until the program closes it. Panics on another server's
|
|
3969
|
+
* request; answers `H1_INVALID` for one that is not taken and unanswered.
|
|
3970
|
+
* @param r a request `take` handed over and the program has not answered
|
|
3971
|
+
* @param protocol the subprotocol to name in `Sec-WebSocket-Protocol`, one the client offered, or "" for none
|
|
3972
|
+
* @returns 0 once the 101 is queued; `H1_INVALID` for a request that is not
|
|
3973
|
+
* held, not a valid HTTP/1.1 opening handshake, a `protocol` not a token, or on a draining server;
|
|
3974
|
+
* `H1_AGAIN` when the output has no room yet; `H1_CLOSED` when the connection closed
|
|
3975
|
+
*/
|
|
3976
|
+
acceptWebSocket(r: HttpServerRequest, protocol: string): i32 {
|
|
3977
|
+
this.own(r, "acceptWebSocket")
|
|
3978
|
+
if (r.state !== HTTP_SERVER_HELD || r.kind !== HTTP_SERVER_H1 || this.draining) {
|
|
3979
|
+
return H1_INVALID
|
|
3980
|
+
}
|
|
3981
|
+
const conn: Http1Connection | null = this.h1ConnOf(r)
|
|
3982
|
+
if (!this.live(r) || conn === null) {
|
|
3983
|
+
return H1_CLOSED
|
|
3984
|
+
}
|
|
3985
|
+
const done: i32 = conn.acceptWebSocket(protocol)
|
|
3986
|
+
if (done !== 0) {
|
|
3987
|
+
return done
|
|
3988
|
+
}
|
|
3989
|
+
r.state = HTTP_SERVER_SOCKET
|
|
3990
|
+
r.socket = true
|
|
3991
|
+
this.driveOf(r)
|
|
3992
|
+
return 0
|
|
3993
|
+
}
|
|
3994
|
+
|
|
3995
|
+
/** Whether `r` is an open WebSocket the program holds; panics, naming `who`, on another server's request. */
|
|
3996
|
+
private ownSocket(r: HttpServerRequest, who: string): boolean {
|
|
3997
|
+
this.own(r, who)
|
|
3998
|
+
return r.state === HTTP_SERVER_SOCKET
|
|
3999
|
+
}
|
|
4000
|
+
|
|
4001
|
+
/**
|
|
4002
|
+
* Sends one frame on WebSocket `r`, unmasked as a server's are: a whole
|
|
4003
|
+
* text or binary message, a fragment of one (`fin` false, then
|
|
4004
|
+
* continuations), a ping or a pong. The payload is copied once, into the
|
|
4005
|
+
* connection's output. On `H1_AGAIN`, `r` comes out of `take` again with
|
|
4006
|
+
* `writable` set once there is room. Panics on another server's request.
|
|
4007
|
+
* @param r a request `acceptWebSocket` upgraded and `closeWebSocket` has not handed back
|
|
4008
|
+
* @param fin true when this frame ends its message; false for a fragment that continuations follow
|
|
4009
|
+
* @param opcode the frame's `WS_OP_` opcode: text, binary, continuation, ping or pong, never close
|
|
4010
|
+
* @param buf the payload bytes
|
|
4011
|
+
* @param off where in `buf` the window starts
|
|
4012
|
+
* @param len how many bytes the window holds
|
|
4013
|
+
* @returns 0 once the frame is queued; `H1_AGAIN` when it does not fit yet;
|
|
4014
|
+
* `H1_INVALID` for a close, a frame the encoder refuses, one larger
|
|
4015
|
+
* than the output, a window outside `buf`, or an `r` that is not a WebSocket the program holds; `H1_CLOSED` when the WebSocket is closing or its connection closed
|
|
4016
|
+
*/
|
|
4017
|
+
sendFrame(r: HttpServerRequest, fin: boolean, opcode: i32, buf: u8[], off: i32, len: i32): i32 {
|
|
4018
|
+
if (!this.ownSocket(r, "sendFrame")) {
|
|
4019
|
+
return H1_INVALID
|
|
4020
|
+
}
|
|
4021
|
+
// Summed in i64, where two i32s cannot overflow; a window outside `buf` leaves `end` at -1. The guard
|
|
4022
|
+
// around the call restates it so that `src/bounds.ts` proves the window there.
|
|
4023
|
+
const sum: i64 = toI64(off) + toI64(len)
|
|
4024
|
+
let end: i32 = -1
|
|
4025
|
+
if (off >= 0 && len >= 0 && sum <= toI64(buf.length)) {
|
|
4026
|
+
end = toI32(sum)
|
|
4027
|
+
}
|
|
4028
|
+
if (end < 0) {
|
|
4029
|
+
return H1_INVALID
|
|
4030
|
+
}
|
|
4031
|
+
r.writable = false
|
|
4032
|
+
const conn: Http1Connection | null = this.h1ConnOf(r)
|
|
4033
|
+
if (!this.live(r) || conn === null) {
|
|
4034
|
+
return H1_CLOSED
|
|
4035
|
+
}
|
|
4036
|
+
let sent: i32 = H1_INVALID
|
|
4037
|
+
if (off >= 0 && off <= end && end <= toI32(buf.length)) {
|
|
4038
|
+
sent = conn.sendFrame(fin, opcode, buf, off, end - off)
|
|
4039
|
+
}
|
|
4040
|
+
if (sent === 0) {
|
|
4041
|
+
this.driveOf(r)
|
|
4042
|
+
}
|
|
4043
|
+
return sent
|
|
4044
|
+
}
|
|
4045
|
+
|
|
4046
|
+
/**
|
|
4047
|
+
* Hands WebSocket `r`'s held message back: `message` is cleared and
|
|
4048
|
+
* `messageData` may be overwritten, as the connection reads its next
|
|
4049
|
+
* frame. Nothing is read past a held message, so a program that keeps one
|
|
4050
|
+
* stops its peer. Panics on another server's request; does nothing when
|
|
4051
|
+
* `r` is not a WebSocket the program holds.
|
|
4052
|
+
* @param r a request `acceptWebSocket` upgraded, with or without a message held
|
|
4053
|
+
*/
|
|
4054
|
+
received(r: HttpServerRequest): void {
|
|
4055
|
+
if (!this.ownSocket(r, "received")) {
|
|
4056
|
+
return
|
|
4057
|
+
}
|
|
4058
|
+
r.message = false
|
|
4059
|
+
this.driveOf(r)
|
|
4060
|
+
}
|
|
4061
|
+
|
|
4062
|
+
/**
|
|
4063
|
+
* Ends WebSocket `r` and hands it back to the server: a close frame with
|
|
4064
|
+
* `code` when the WebSocket is still open (a peer's close is answered by
|
|
4065
|
+
* the connection already), then the connection closes once that is sent.
|
|
4066
|
+
* Panics on another server's request.
|
|
4067
|
+
* @param r a request `acceptWebSocket` upgraded; the server's again unless this answers `H1_INVALID`
|
|
4068
|
+
* @param code a close status code `nish/net/websocket` may send, such as 1000 for a normal close
|
|
4069
|
+
* @returns 0 once `r` is handed back; `H1_INVALID`, with `r` unchanged, for
|
|
4070
|
+
* an `r` that is not a WebSocket the program holds, or a code that may
|
|
4071
|
+
* not be sent while it is open
|
|
4072
|
+
*/
|
|
4073
|
+
closeWebSocket(r: HttpServerRequest, code: i32): i32 {
|
|
4074
|
+
if (!this.ownSocket(r, "closeWebSocket")) {
|
|
4075
|
+
return H1_INVALID
|
|
4076
|
+
}
|
|
4077
|
+
const conn: Http1Connection | null = this.h1ConnOf(r)
|
|
4078
|
+
if (!this.live(r) || conn === null) {
|
|
4079
|
+
this.release(r)
|
|
4080
|
+
return 0
|
|
4081
|
+
}
|
|
4082
|
+
if (conn.closeWebSocket(code) === H1_INVALID) {
|
|
4083
|
+
return H1_INVALID
|
|
4084
|
+
}
|
|
4085
|
+
r.message = false
|
|
4086
|
+
r.state = HTTP_SERVER_FLUSHING
|
|
4087
|
+
if (r.again) {
|
|
4088
|
+
this.unqueue(r)
|
|
4089
|
+
}
|
|
4090
|
+
this.driveOf(r)
|
|
4091
|
+
return 0
|
|
4092
|
+
}
|
|
4093
|
+
|
|
3792
4094
|
/**
|
|
3793
4095
|
* Answers a request the router refused, by its `route`: 405 with an `Allow`
|
|
3794
4096
|
* field naming the methods the path has, and 404 for anything else.
|