@amritk/nish-x86_64-linux 0.41.0 → 0.44.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/bin/nish +0 -0
- package/docs/AI.md +282 -126
- package/package.json +2 -2
- package/runtime/core.d.ts +16 -0
- package/runtime/core.mjs +5 -0
- package/runtime/nish.d.ts +204 -33
- package/runtime/nish.h +19 -3
- package/runtime/nish.mjs +5 -1
- package/runtime/runtime-cross.c +15 -0
- package/runtime/runtime-net.c +876 -10
- package/runtime/runtime-region.c +21 -0
- package/runtime/runtime-sandbox.c +112 -15
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +533 -77
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +474 -5
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +125 -29
- package/std/README.md +3 -3
- package/std/crypto/base64url.ts +102 -1
- package/std/crypto/scram.ts +142 -0
- package/std/crypto/sha1.ts +99 -41
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/net/fetch.ts +9 -1
- package/std/net/handler.ts +79 -15
- package/std/net/http-server.ts +2 -3
- package/std/net/http1-server.ts +294 -51
- package/std/net/http3-server.ts +26 -15
- package/std/net/http3.ts +2 -2
- package/std/net/oidc.ts +1107 -0
- package/std/net/postgres-async.ts +64 -33
- package/std/net/postgres.ts +65 -76
- package/std/net/quic-conn-ack.ts +57 -7
- package/std/net/quic-frame.ts +104 -2
- package/std/net/quic-listener.ts +9 -2
- package/std/net/quic.ts +16 -1
- package/std/net/serve.ts +10 -8
- package/std/net/websocket.ts +188 -49
- package/std/sqlite.ts +325 -0
- package/std/text.ts +80 -11
package/std/net/http1-server.ts
CHANGED
|
@@ -104,14 +104,16 @@
|
|
|
104
104
|
* a whole request head (`headTimeout`, against slowloris); the slots are the
|
|
105
105
|
* pool's size.
|
|
106
106
|
*
|
|
107
|
-
* `Http1Server`
|
|
108
|
-
*
|
|
109
|
-
* depend on the size, and holds
|
|
110
|
-
*
|
|
111
|
-
*
|
|
107
|
+
* `Http1Server` lends a connection to a slot only while it is busy and builds
|
|
108
|
+
* one only when none is spare, so a pool sized for its peak costs at start-up
|
|
109
|
+
* only what does not depend on the size, and holds as many connections as were
|
|
110
|
+
* busy at once; `Http1TlsServer` builds a slot's connection on its first
|
|
111
|
+
* accept and keeps it, with its TLS state through
|
|
112
|
+
* `TlsTcpServer`. Every buffer is allocated with its slot or
|
|
112
113
|
* grown to its cap on first use and kept, so a warmed slot serves request after
|
|
113
|
-
* request with `Arena.used()` flat. Accepting a WebSocket
|
|
114
|
-
* key
|
|
114
|
+
* request with `Arena.used()` flat. Accepting a WebSocket computes the accept
|
|
115
|
+
* key and writes the 101 into storage the slot keeps, built on its first
|
|
116
|
+
* accept, so later handshakes leave the arena flat too.
|
|
115
117
|
*
|
|
116
118
|
* **Output is borrowed.** The pools' slots share an `Http1OutputPool`: a
|
|
117
119
|
* connection takes an `outputSize` buffer when it first needs room to write
|
|
@@ -168,9 +170,10 @@ import {
|
|
|
168
170
|
WsDecoder,
|
|
169
171
|
websocketClosePayload,
|
|
170
172
|
websocketFrameSize,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
WsAcceptKey,
|
|
174
|
+
websocketRequestKeyField,
|
|
173
175
|
websocketWriteFrame,
|
|
176
|
+
websocketWriteUpgradeResponse,
|
|
174
177
|
} from "nish/net/websocket"
|
|
175
178
|
import { TlsServerConfig } from "nish/net/tls"
|
|
176
179
|
import { HTTP_DATE_LAST_SECOND, HTTP_DATE_SIZE, httpDateWrite } from "nish/net/http-fields"
|
|
@@ -865,6 +868,11 @@ export class Http1Connection {
|
|
|
865
868
|
* the slot's later peers.
|
|
866
869
|
*/
|
|
867
870
|
ws: WsDecoder | null
|
|
871
|
+
/**
|
|
872
|
+
* The accept key's storage: made on the slot's first WebSocket accept, like
|
|
873
|
+
* `ws`, then kept, so a later handshake allocates nothing.
|
|
874
|
+
*/
|
|
875
|
+
private acceptKey: WsAcceptKey | null
|
|
868
876
|
/** `H1_BODY`'s and `H1_WS_MESSAGE`'s bytes. */
|
|
869
877
|
data: u8[]
|
|
870
878
|
/** A ping's payload, kept until its pong fits: empty until the first ping. */
|
|
@@ -912,6 +920,11 @@ export class Http1Connection {
|
|
|
912
920
|
* still has that count, `respond` neither compares nor keeps. -1 while on.
|
|
913
921
|
*/
|
|
914
922
|
private headOff: i64 = -1
|
|
923
|
+
/**
|
|
924
|
+
* The accept serial of the peer whose responses the kept head was kept
|
|
925
|
+
* for, -1 for none: `lendTo` drops a kept head lent to another peer.
|
|
926
|
+
*/
|
|
927
|
+
private keptFor: i64 = -1
|
|
915
928
|
/**
|
|
916
929
|
* How many heads `respond` has copied from the kept one rather than written.
|
|
917
930
|
*
|
|
@@ -960,6 +973,7 @@ export class Http1Connection {
|
|
|
960
973
|
this.parser.keepText = false
|
|
961
974
|
this.parser.maxChunk = config.chunkSize
|
|
962
975
|
this.ws = null
|
|
976
|
+
this.acceptKey = null
|
|
963
977
|
this.noOutput = []
|
|
964
978
|
this.output = this.noOutput
|
|
965
979
|
this.pool = null
|
|
@@ -1019,6 +1033,7 @@ export class Http1Connection {
|
|
|
1019
1033
|
this.headStatus = 0
|
|
1020
1034
|
this.headMisses = 0
|
|
1021
1035
|
this.headOff = -1
|
|
1036
|
+
this.keptFor = -1
|
|
1022
1037
|
this.lingerSince = 0
|
|
1023
1038
|
this.lingered = -1
|
|
1024
1039
|
this.mss = 0
|
|
@@ -1230,6 +1245,77 @@ export class Http1Connection {
|
|
|
1230
1245
|
)
|
|
1231
1246
|
}
|
|
1232
1247
|
|
|
1248
|
+
/**
|
|
1249
|
+
* Whether the connection is between requests, so that a server draining
|
|
1250
|
+
* gracefully may close it without cutting a request short: it speaks HTTP,
|
|
1251
|
+
* not upgraded or closing, no request is open, no next head has begun to
|
|
1252
|
+
* arrive (its `headTimeout` clock has not started), and every byte of the
|
|
1253
|
+
* last response is sent.
|
|
1254
|
+
*
|
|
1255
|
+
* @returns true while closing the connection loses nothing its client sent or is owed
|
|
1256
|
+
*/
|
|
1257
|
+
betweenRequests(): boolean {
|
|
1258
|
+
return this.state === H1_S_HTTP && !this.requestOpen && !this.headTimed && !this.wantsWrite()
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
/**
|
|
1262
|
+
* Whether the connection holds nothing its peer needs kept between
|
|
1263
|
+
* requests: `betweenRequests`, with no output borrowed, no write held back
|
|
1264
|
+
* or pong owed, the peer's stream not ended, no close asked for, no
|
|
1265
|
+
* lingering close, no program-set `mss`, and no byte fed and unread. Such a
|
|
1266
|
+
* connection is the same for any peer, so `Http1Server` lends it to the
|
|
1267
|
+
* next slot that reads and keeps only the slot's `lastActive` and
|
|
1268
|
+
* `socketMss` while it rests.
|
|
1269
|
+
*
|
|
1270
|
+
* @returns true when the connection may serve another peer's next request as it is
|
|
1271
|
+
* @internal Called by `Http1Server.flush` in this module to free a resting slot's connection.
|
|
1272
|
+
*/
|
|
1273
|
+
/**
|
|
1274
|
+
* Takes the connection for the peer accepted as `owner`, dropping its kept
|
|
1275
|
+
* head unless that peer's own responses wrote it, so that how long
|
|
1276
|
+
* `respond` takes tells one peer nothing of another's heads (H1-7).
|
|
1277
|
+
*
|
|
1278
|
+
* @param owner the accept serial of the peer the connection serves next
|
|
1279
|
+
* @internal Called by `Http1Server` in this module as it lends a connection to a slot.
|
|
1280
|
+
*/
|
|
1281
|
+
lendTo(owner: i64): void {
|
|
1282
|
+
if (this.keptFor !== owner) {
|
|
1283
|
+
this.headLength = 0
|
|
1284
|
+
this.headStatus = 0
|
|
1285
|
+
this.headMisses = 0
|
|
1286
|
+
this.headOff = -1
|
|
1287
|
+
this.keptFor = owner
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
/**
|
|
1292
|
+
* Whether the connection holds nothing a resting keep-alive slot must keep:
|
|
1293
|
+
* speaking HTTP with no request open, no head clock started, no output
|
|
1294
|
+
* borrowed or unsent, no write held or pong owed, the peer's stream not
|
|
1295
|
+
* ended, no close asked for or lingering, no program-set `mss`, and its
|
|
1296
|
+
* parser at rest. `Http1Server` then gives the connection back for another
|
|
1297
|
+
* busy slot.
|
|
1298
|
+
*
|
|
1299
|
+
* @returns true when the connection may be lent to another slot.
|
|
1300
|
+
* @internal Called by `Http1Server` in this module at `flush`.
|
|
1301
|
+
*/
|
|
1302
|
+
rests(): boolean {
|
|
1303
|
+
return (
|
|
1304
|
+
this.state === H1_S_HTTP &&
|
|
1305
|
+
!this.requestOpen &&
|
|
1306
|
+
!this.headTimed &&
|
|
1307
|
+
this.outputCap === 0 &&
|
|
1308
|
+
this.outputEnd === this.outputStart &&
|
|
1309
|
+
!this.writeHeld &&
|
|
1310
|
+
!this.pongPending &&
|
|
1311
|
+
!this.inputEnded &&
|
|
1312
|
+
!this.closeAfter &&
|
|
1313
|
+
this.lingered < 0 &&
|
|
1314
|
+
this.mss === 0 &&
|
|
1315
|
+
this.parser.resting()
|
|
1316
|
+
)
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1233
1319
|
/** The room at the end of the output, once what was sent is moved out of the way. */
|
|
1234
1320
|
private room(): i32 {
|
|
1235
1321
|
// Tested here, so that the usual case, an output already held, is no call.
|
|
@@ -2088,26 +2174,45 @@ export class Http1Connection {
|
|
|
2088
2174
|
if (!this.answerable()) {
|
|
2089
2175
|
return H1_CLOSED
|
|
2090
2176
|
}
|
|
2091
|
-
const
|
|
2092
|
-
|
|
2177
|
+
const p: Http1Parser = this.parser
|
|
2178
|
+
const field: i32 = websocketRequestKeyField(p)
|
|
2179
|
+
const spans: i32[] = p.spans
|
|
2180
|
+
if (field < 0 || 4 * field + 3 >= toI32(spans.length)) {
|
|
2181
|
+
return H1_INVALID
|
|
2182
|
+
}
|
|
2183
|
+
const accept: WsAcceptKey = this.acceptStorage()
|
|
2184
|
+
if (!accept.compute(p.head, spans[4 * field + 2], spans[4 * field + 3])) {
|
|
2093
2185
|
return H1_INVALID
|
|
2094
2186
|
}
|
|
2095
|
-
|
|
2096
|
-
|
|
2187
|
+
// Written past `outputEnd`, where nothing live is, and kept only when it
|
|
2188
|
+
// fits beside the control reserve.
|
|
2189
|
+
const room: i32 = this.bodyRoom()
|
|
2190
|
+
const end: i32 = websocketWriteUpgradeResponse(this.output, this.outputEnd, accept.value, protocol)
|
|
2191
|
+
if (end === HTTP1_REFUSED) {
|
|
2097
2192
|
return H1_INVALID
|
|
2098
2193
|
}
|
|
2099
|
-
|
|
2100
|
-
if (n > this.bodyRoom()) {
|
|
2194
|
+
if (end < 0 || end - this.outputEnd > room) {
|
|
2101
2195
|
this.writeHeld = true
|
|
2102
2196
|
return H1_AGAIN
|
|
2103
2197
|
}
|
|
2104
|
-
this.outputEnd =
|
|
2198
|
+
this.outputEnd = end
|
|
2105
2199
|
this.response = H1_R_DONE
|
|
2106
2200
|
this.upgraded = true
|
|
2107
2201
|
this.closeAfter = false
|
|
2108
2202
|
return 0
|
|
2109
2203
|
}
|
|
2110
2204
|
|
|
2205
|
+
/** The slot's `WsAcceptKey`, made on its first WebSocket accept and kept. */
|
|
2206
|
+
private acceptStorage(): WsAcceptKey {
|
|
2207
|
+
const made: WsAcceptKey | null = this.acceptKey
|
|
2208
|
+
if (made !== null) {
|
|
2209
|
+
return made
|
|
2210
|
+
}
|
|
2211
|
+
const fresh: WsAcceptKey = new WsAcceptKey()
|
|
2212
|
+
this.acceptKey = fresh
|
|
2213
|
+
return fresh
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2111
2216
|
/**
|
|
2112
2217
|
* Sends one WebSocket frame, unmasked as a server's are (§5.1): a whole
|
|
2113
2218
|
* text or binary message, a fragment of one (`fin` false, then
|
|
@@ -2324,14 +2429,36 @@ const http1ExpireFloor = (config: Http1Config): i64 => {
|
|
|
2324
2429
|
* A pool of HTTP/1.1 connections over plain TCP on one listening socket.
|
|
2325
2430
|
* `slot`s are the indices `accept` answers; a slot is the program's from
|
|
2326
2431
|
* `accept` until it calls `close`, and its descriptor is `fd(slot)`.
|
|
2432
|
+
*
|
|
2433
|
+
* A slot holds an `Http1Connection` only while it has something to keep: a
|
|
2434
|
+
* request read or answered, bytes fed and unread, output to send, or a
|
|
2435
|
+
* WebSocket. One that rests between requests (`Http1Connection.rests`) gives
|
|
2436
|
+
* its connection back at `flush` and keeps only its descriptor, its
|
|
2437
|
+
* interest, its `lastActive` and its socket's MSS; the next slot that reads
|
|
2438
|
+
* takes a resting connection, keeping its response head only if it was kept
|
|
2439
|
+
* for that slot's own peer (H1-7). So the pool builds
|
|
2440
|
+
* as many connections as are busy at once, not one per open slot, and an
|
|
2441
|
+
* idle keep-alive costs its slot a few words.
|
|
2327
2442
|
*/
|
|
2328
2443
|
export class Http1Server {
|
|
2329
2444
|
private config: Http1Config
|
|
2330
2445
|
/**
|
|
2331
|
-
* The connections built so far,
|
|
2332
|
-
*
|
|
2446
|
+
* The connections built so far, as many as were ever busy at once: each is
|
|
2447
|
+
* a slot's (`at`) or spare.
|
|
2333
2448
|
*/
|
|
2334
2449
|
connections: Http1Connection[]
|
|
2450
|
+
/** Each used slot's connection, an index into `connections`, or -1 while the slot rests. */
|
|
2451
|
+
private at: i32[]
|
|
2452
|
+
/** The indices in `connections` of the connections no slot holds, a stack. */
|
|
2453
|
+
private spare: i32[]
|
|
2454
|
+
/** Each resting slot's `lastActive` (`monotonicNanos`), which its connection carried. */
|
|
2455
|
+
private rested: i64[]
|
|
2456
|
+
/** Each resting slot's `socketMss`, which its connection carried. */
|
|
2457
|
+
private restedMss: i32[]
|
|
2458
|
+
/** Each used slot's accept serial, which keys the kept head of a connection lent to it (H1-7). */
|
|
2459
|
+
private owner: i64[]
|
|
2460
|
+
/** How many connections `accept` has taken: the next one's serial. */
|
|
2461
|
+
private accepts: i64 = 0
|
|
2335
2462
|
/**
|
|
2336
2463
|
* The `Date` every slot's responses carry, formatted once a second for all
|
|
2337
2464
|
* of them and read from the clock once per serve call: `readable` and
|
|
@@ -2378,10 +2505,10 @@ export class Http1Server {
|
|
|
2378
2505
|
|
|
2379
2506
|
/**
|
|
2380
2507
|
* A pool of `size` slots, at least one, serving `config` on `listener`.
|
|
2381
|
-
* No slot is built here: a
|
|
2382
|
-
*
|
|
2383
|
-
*
|
|
2384
|
-
*
|
|
2508
|
+
* No slot is built here: a connection is built the first time a slot
|
|
2509
|
+
* reads and none rests spare, and kept, so a pool sized for a peak costs
|
|
2510
|
+
* at start-up only the read scratch, the shared `Date` and the output
|
|
2511
|
+
* pool, and at most as many connections as were ever busy at once.
|
|
2385
2512
|
*
|
|
2386
2513
|
* @param config the caps every slot's connection is held to
|
|
2387
2514
|
* @param listener the listening socket from `tcpListen`, which stays the program's to close
|
|
@@ -2391,6 +2518,11 @@ export class Http1Server {
|
|
|
2391
2518
|
this.config = config
|
|
2392
2519
|
this.listener = listener
|
|
2393
2520
|
this.connections = []
|
|
2521
|
+
this.at = []
|
|
2522
|
+
this.spare = []
|
|
2523
|
+
this.rested = []
|
|
2524
|
+
this.restedMss = []
|
|
2525
|
+
this.owner = []
|
|
2394
2526
|
this.fds = []
|
|
2395
2527
|
this.armed = []
|
|
2396
2528
|
this.free = []
|
|
@@ -2403,7 +2535,7 @@ export class Http1Server {
|
|
|
2403
2535
|
this.peer = new Array<u8>(H1_ADDRESS_SIZE)
|
|
2404
2536
|
}
|
|
2405
2537
|
|
|
2406
|
-
/** A new connection on this pool's outputs and `Date`,
|
|
2538
|
+
/** A new connection on this pool's outputs and `Date`, appended to `connections`. */
|
|
2407
2539
|
private build(): Http1Connection {
|
|
2408
2540
|
const conn: Http1Connection = new Http1Connection(this.config, this.date)
|
|
2409
2541
|
conn.pool = this.outputs
|
|
@@ -2460,20 +2592,82 @@ export class Http1Server {
|
|
|
2460
2592
|
}
|
|
2461
2593
|
|
|
2462
2594
|
/**
|
|
2463
|
-
* The connection of `slot`. A slot
|
|
2464
|
-
*
|
|
2465
|
-
*
|
|
2595
|
+
* The connection of `slot`. A resting slot is given one here, a spare or
|
|
2596
|
+
* a new one, which it keeps until it rests again at a `flush`: so a
|
|
2597
|
+
* program holds the answer only within a serve call, and asks again in
|
|
2598
|
+
* the next. A free slot, or one out of range, names the first connection
|
|
2599
|
+
* built, which this builds when none has been; it is another slot's or
|
|
2600
|
+
* spare, so read nothing from it.
|
|
2466
2601
|
*
|
|
2467
2602
|
* @param slot the slot `accept` answered
|
|
2468
2603
|
* @returns the slot's connection, which the pool keeps; read its events and write its response through it
|
|
2469
2604
|
*/
|
|
2470
2605
|
connection(slot: i32): Http1Connection {
|
|
2471
|
-
if (slot
|
|
2472
|
-
return this.
|
|
2606
|
+
if (this.holds(slot) && slot < toI32(this.at.length)) {
|
|
2607
|
+
return this.attach(slot)
|
|
2473
2608
|
}
|
|
2474
2609
|
return this.connections.length > 0 ? this.connections[0] : this.build()
|
|
2475
2610
|
}
|
|
2476
2611
|
|
|
2612
|
+
/**
|
|
2613
|
+
* Whether `slot` holds a connection that rests between requests and has
|
|
2614
|
+
* given its `Http1Connection` back: nothing is open, unread or unsent on
|
|
2615
|
+
* it, so closing it loses nothing. It does not give the slot a connection,
|
|
2616
|
+
* as `connection` does.
|
|
2617
|
+
*
|
|
2618
|
+
* @param slot the slot index to ask about, any value
|
|
2619
|
+
* @returns true when the slot is accepted into and resting; false when it is free, out of range or busy
|
|
2620
|
+
*/
|
|
2621
|
+
resting(slot: i32): boolean {
|
|
2622
|
+
return this.holds(slot) && slot < toI32(this.at.length) && this.at[slot] < 0
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
/**
|
|
2626
|
+
* How many connections no slot holds now, of the `connections` built.
|
|
2627
|
+
*
|
|
2628
|
+
* @returns the count of spare connections, which a resting slot that reads takes before one is built
|
|
2629
|
+
*/
|
|
2630
|
+
spares(): i32 {
|
|
2631
|
+
return toI32(this.spare.length)
|
|
2632
|
+
}
|
|
2633
|
+
|
|
2634
|
+
/** `slot`'s connection, which it holds, given one when it rests: carrying its descriptor, `lastActive` and MSS. */
|
|
2635
|
+
private attach(slot: i32): Http1Connection {
|
|
2636
|
+
const k: i32 = this.at[slot]
|
|
2637
|
+
if (k >= 0) {
|
|
2638
|
+
return this.connections[k]
|
|
2639
|
+
}
|
|
2640
|
+
const j: i32 = this.spare.length > 0 ? this.spare[this.spare.length - 1] : -1
|
|
2641
|
+
if (j >= 0) {
|
|
2642
|
+
this.spare.pop()
|
|
2643
|
+
}
|
|
2644
|
+
const conn: Http1Connection = j >= 0 ? this.connections[j] : this.build()
|
|
2645
|
+
this.at[slot] = j >= 0 ? j : toI32(this.connections.length) - 1
|
|
2646
|
+
conn.fd = this.fds[slot]
|
|
2647
|
+
conn.lastActive = this.rested[slot]
|
|
2648
|
+
conn.socketMss = this.restedMss[slot]
|
|
2649
|
+
conn.lendTo(this.owner[slot])
|
|
2650
|
+
return conn
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
/** Gives back `slot`'s connection when it rests, keeping what the slot needs of it. */
|
|
2654
|
+
private rest(slot: i32): void {
|
|
2655
|
+
const k: i32 = this.at[slot]
|
|
2656
|
+
if (k < 0) {
|
|
2657
|
+
return
|
|
2658
|
+
}
|
|
2659
|
+
const conn: Http1Connection = this.connections[k]
|
|
2660
|
+
if (!conn.rests()) {
|
|
2661
|
+
return
|
|
2662
|
+
}
|
|
2663
|
+
this.rested[slot] = conn.lastActive
|
|
2664
|
+
this.restedMss[slot] = conn.socketMss
|
|
2665
|
+
conn.fd = -1
|
|
2666
|
+
conn.socketMss = 0
|
|
2667
|
+
this.at[slot] = -1
|
|
2668
|
+
this.spare.push(k)
|
|
2669
|
+
}
|
|
2670
|
+
|
|
2477
2671
|
/**
|
|
2478
2672
|
* Accepts the next waiting connection into a free slot and answers the
|
|
2479
2673
|
* slot, which the program adds to its loop with `H1_WANT_READ`; -11 when
|
|
@@ -2498,23 +2692,34 @@ export class Http1Server {
|
|
|
2498
2692
|
k = toI32(this.fds.length)
|
|
2499
2693
|
this.fds.push(H1_FREE)
|
|
2500
2694
|
this.armed.push(H1_WANT_READ)
|
|
2695
|
+
this.at.push(toI32(-1))
|
|
2696
|
+
this.rested.push(toI64(0))
|
|
2697
|
+
this.restedMss.push(toI32(0))
|
|
2698
|
+
this.owner.push(toI64(0))
|
|
2501
2699
|
}
|
|
2502
|
-
if (
|
|
2700
|
+
if (
|
|
2701
|
+
k < 0 ||
|
|
2702
|
+
k >= toI32(this.fds.length) ||
|
|
2703
|
+
k >= toI32(this.armed.length) ||
|
|
2704
|
+
k >= toI32(this.at.length) ||
|
|
2705
|
+
k >= toI32(this.rested.length) ||
|
|
2706
|
+
k >= toI32(this.restedMss.length) ||
|
|
2707
|
+
k >= toI32(this.owner.length)
|
|
2708
|
+
) {
|
|
2503
2709
|
netClose(fd)
|
|
2504
2710
|
return H1_POOL_FULL
|
|
2505
2711
|
}
|
|
2506
|
-
const conn: Http1Connection = k < toI32(this.connections.length) ? this.connections[k] : this.build()
|
|
2507
2712
|
this.fds[k] = fd
|
|
2508
2713
|
this.armed[k] = H1_WANT_READ
|
|
2509
|
-
|
|
2510
|
-
|
|
2714
|
+
// The slot starts resting: it is given a connection when it first reads.
|
|
2715
|
+
this.at[k] = -1
|
|
2716
|
+
this.rested[k] = monotonicNanos()
|
|
2717
|
+
this.owner[k] = this.accepts
|
|
2718
|
+
this.accepts += 1
|
|
2511
2719
|
// Once a connection, not a request, so a keep-alive request costs no
|
|
2512
2720
|
// syscall: a path whose MSS moves after the handshake is not followed.
|
|
2513
2721
|
const mss: i32 = netMss(fd)
|
|
2514
|
-
|
|
2515
|
-
conn.socketMss = mss
|
|
2516
|
-
}
|
|
2517
|
-
conn.lastActive = monotonicNanos()
|
|
2722
|
+
this.restedMss[k] = mss > 0 ? mss : 0
|
|
2518
2723
|
return k
|
|
2519
2724
|
}
|
|
2520
2725
|
|
|
@@ -2532,7 +2737,7 @@ export class Http1Server {
|
|
|
2532
2737
|
return H1_DONE
|
|
2533
2738
|
}
|
|
2534
2739
|
this.date.hold()
|
|
2535
|
-
const conn: Http1Connection = this.
|
|
2740
|
+
const conn: Http1Connection = this.attach(slot)
|
|
2536
2741
|
const fd: i32 = this.fds[slot]
|
|
2537
2742
|
let moved: boolean = false
|
|
2538
2743
|
while (true) {
|
|
@@ -2596,7 +2801,11 @@ export class Http1Server {
|
|
|
2596
2801
|
if (!this.holds(slot)) {
|
|
2597
2802
|
return H1_ERROR
|
|
2598
2803
|
}
|
|
2599
|
-
|
|
2804
|
+
if (this.at[slot] < 0) {
|
|
2805
|
+
// A resting slot has read nothing since it was answered.
|
|
2806
|
+
return H1_NEED_MORE
|
|
2807
|
+
}
|
|
2808
|
+
const conn: Http1Connection = this.connections[this.at[slot]]
|
|
2600
2809
|
if (conn.lingering()) {
|
|
2601
2810
|
// Its `H1_ERROR` was answered before the lingering close began.
|
|
2602
2811
|
return H1_NEED_MORE
|
|
@@ -2618,6 +2827,7 @@ export class Http1Server {
|
|
|
2618
2827
|
* shut and what the client still sends is drained, answering
|
|
2619
2828
|
* `H1_WANT_READ` until the client's end of stream, `lingerBytes`, or
|
|
2620
2829
|
* `expire` past `lingerTimeout`; `next` answers `H1_NEED_MORE` meanwhile.
|
|
2830
|
+
* A slot whose connection then rests gives it back (see the class comment).
|
|
2621
2831
|
*
|
|
2622
2832
|
* @param slot the slot `accept` answered
|
|
2623
2833
|
* @returns the `H1_WANT_` bits to arm the slot with, or `H1_DONE` when the program should `close` it
|
|
@@ -2625,7 +2835,11 @@ export class Http1Server {
|
|
|
2625
2835
|
flush(slot: i32): i32 {
|
|
2626
2836
|
this.date.unhold()
|
|
2627
2837
|
const wants: i32 = this.settle(slot)
|
|
2628
|
-
if (
|
|
2838
|
+
if (!this.holds(slot)) {
|
|
2839
|
+
return wants
|
|
2840
|
+
}
|
|
2841
|
+
if (wants !== H1_DONE) {
|
|
2842
|
+
this.rest(slot)
|
|
2629
2843
|
return wants
|
|
2630
2844
|
}
|
|
2631
2845
|
// A refusal is sent: shut the write side and drain what the client still
|
|
@@ -2633,7 +2847,7 @@ export class Http1Server {
|
|
|
2633
2847
|
// Closed with those bytes unread, the socket would answer them with a
|
|
2634
2848
|
// reset that can destroy the response before the client reads it (H1-2).
|
|
2635
2849
|
const now: i64 = monotonicNanos()
|
|
2636
|
-
const conn: Http1Connection = this.
|
|
2850
|
+
const conn: Http1Connection = this.attach(slot)
|
|
2637
2851
|
if (!conn.lingerStart(now) || netShutdown(this.fds[slot], H1_SHUT_WRITE) < 0) {
|
|
2638
2852
|
return H1_DONE
|
|
2639
2853
|
}
|
|
@@ -2651,7 +2865,7 @@ export class Http1Server {
|
|
|
2651
2865
|
* `lastActive` stays, and `expire` closes the slot past `lingerTimeout`.
|
|
2652
2866
|
*/
|
|
2653
2867
|
private drain(slot: i32): i32 {
|
|
2654
|
-
const conn: Http1Connection = this.
|
|
2868
|
+
const conn: Http1Connection = this.attach(slot)
|
|
2655
2869
|
const fd: i32 = this.fds[slot]
|
|
2656
2870
|
while (true) {
|
|
2657
2871
|
const n: i32 = netRead(fd, this.scratch, H1_ZERO, toI32(this.scratch.length))
|
|
@@ -2669,7 +2883,11 @@ export class Http1Server {
|
|
|
2669
2883
|
if (!this.holds(slot)) {
|
|
2670
2884
|
return H1_DONE
|
|
2671
2885
|
}
|
|
2672
|
-
|
|
2886
|
+
if (this.at[slot] < 0) {
|
|
2887
|
+
// A resting slot holds nothing to send and has room to read.
|
|
2888
|
+
return H1_WANT_READ
|
|
2889
|
+
}
|
|
2890
|
+
const conn: Http1Connection = this.connections[this.at[slot]]
|
|
2673
2891
|
if (conn.lingering()) {
|
|
2674
2892
|
return this.drain(slot)
|
|
2675
2893
|
}
|
|
@@ -2695,7 +2913,7 @@ export class Http1Server {
|
|
|
2695
2913
|
|
|
2696
2914
|
/** Writes what `slot`'s connection holds, which it holds, until the socket would block. */
|
|
2697
2915
|
private send(slot: i32): void {
|
|
2698
|
-
const conn: Http1Connection = this.
|
|
2916
|
+
const conn: Http1Connection = this.attach(slot)
|
|
2699
2917
|
const fd: i32 = this.fds[slot]
|
|
2700
2918
|
const pending: i32 = conn.outputEnd - conn.outputStart
|
|
2701
2919
|
while (conn.wantsWrite()) {
|
|
@@ -2737,7 +2955,8 @@ export class Http1Server {
|
|
|
2737
2955
|
}
|
|
2738
2956
|
|
|
2739
2957
|
/**
|
|
2740
|
-
* Closes `slot`'s socket, whatever is left unsent, and frees the slot.
|
|
2958
|
+
* Closes `slot`'s socket, whatever is left unsent, and frees the slot. Its
|
|
2959
|
+
* connection, when it holds one, is aborted and restarted, and is spare.
|
|
2741
2960
|
*
|
|
2742
2961
|
* @param slot the slot to close; a free or unknown one is ignored
|
|
2743
2962
|
*/
|
|
@@ -2748,8 +2967,13 @@ export class Http1Server {
|
|
|
2748
2967
|
netClose(this.fds[slot])
|
|
2749
2968
|
this.fds[slot] = H1_FREE
|
|
2750
2969
|
this.free.push(slot)
|
|
2751
|
-
if (slot < toI32(this.
|
|
2752
|
-
this.
|
|
2970
|
+
if (slot < toI32(this.at.length) && this.at[slot] >= 0) {
|
|
2971
|
+
const k: i32 = this.at[slot]
|
|
2972
|
+
const conn: Http1Connection = this.connections[k]
|
|
2973
|
+
conn.abort()
|
|
2974
|
+
conn.restart()
|
|
2975
|
+
this.at[slot] = -1
|
|
2976
|
+
this.spare.push(k)
|
|
2753
2977
|
}
|
|
2754
2978
|
}
|
|
2755
2979
|
|
|
@@ -2767,14 +2991,33 @@ export class Http1Server {
|
|
|
2767
2991
|
this.expireScans = this.expireScans + 1
|
|
2768
2992
|
let soonest: i64 = http1ExpireFloor(this.config)
|
|
2769
2993
|
let n: i32 = 0
|
|
2770
|
-
|
|
2994
|
+
const idle: i64 = toI64(this.config.idleTimeout) * toI64(1000000)
|
|
2995
|
+
const head: i64 = now + toI64(this.config.headTimeout) * toI64(1000000)
|
|
2996
|
+
for (
|
|
2997
|
+
let k: i32 = 0;
|
|
2998
|
+
k < toI32(this.fds.length) && k < toI32(this.at.length) && k < toI32(this.rested.length);
|
|
2999
|
+
k++
|
|
3000
|
+
) {
|
|
2771
3001
|
if (this.fds[k] >= 0) {
|
|
2772
|
-
const
|
|
2773
|
-
if (
|
|
3002
|
+
const j: i32 = this.at[k]
|
|
3003
|
+
if (j < 0) {
|
|
3004
|
+
// A resting slot is idle past `idleTimeout`, and a head that
|
|
3005
|
+
// starts arriving no sooner than now has `headTimeout` from now.
|
|
3006
|
+
const since: i64 = this.rested[k]
|
|
3007
|
+
if (now - since > idle) {
|
|
3008
|
+
this.close(k)
|
|
3009
|
+
n = n + 1
|
|
3010
|
+
} else {
|
|
3011
|
+
const quiet: i64 = since + idle < head ? since + idle : head
|
|
3012
|
+
if (quiet < soonest) {
|
|
3013
|
+
soonest = quiet
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
} else if (this.connections[j].expired(now)) {
|
|
2774
3017
|
this.close(k)
|
|
2775
3018
|
n = n + 1
|
|
2776
|
-
} else if (
|
|
2777
|
-
soonest =
|
|
3019
|
+
} else if (this.connections[j].expiresNoSooner(now) < soonest) {
|
|
3020
|
+
soonest = this.connections[j].expiresNoSooner(now)
|
|
2778
3021
|
}
|
|
2779
3022
|
}
|
|
2780
3023
|
}
|
package/std/net/http3-server.ts
CHANGED
|
@@ -574,6 +574,19 @@ export class Http3Server {
|
|
|
574
574
|
private free: i32[]
|
|
575
575
|
/** Per slot: 1 while on the ready ring, 2 while on the dirty stack. */
|
|
576
576
|
private queued: i32[]
|
|
577
|
+
/**
|
|
578
|
+
* The stream buffers every slot's connection borrows from, made by the
|
|
579
|
+
* constructor for the stream table `quicConfig` gives every slot: `null`
|
|
580
|
+
* when its streams have none. Each pool makes a buffer only when a stream
|
|
581
|
+
* first needs one, so an idle server holds none (QUIC-3). It is made even
|
|
582
|
+
* for a configuration `quicConnConfigFits` refuses; a slot's connection
|
|
583
|
+
* shares it only when its stream buffers are the pools' size.
|
|
584
|
+
*/
|
|
585
|
+
streamPools: QuicStreamPools | null = null
|
|
586
|
+
/** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
|
|
587
|
+
dataPool: BufferPool
|
|
588
|
+
/** The `maxFieldSectionSize`-byte HEADERS buffers every slot's `Http3Connection` borrows around a decode. */
|
|
589
|
+
sectionPool: BufferPool
|
|
577
590
|
/** When the answer budget was last topped up, -1 before the first answer. */
|
|
578
591
|
private answerRefilled: i64 = -1
|
|
579
592
|
/** The caller's clock in microseconds, from `clock`, or -1: what each connection's pacer counts. */
|
|
@@ -588,8 +601,6 @@ export class Http3Server {
|
|
|
588
601
|
private slots: i32 = 1
|
|
589
602
|
/** The slots ever accepted into, 0 up: one past it has never held a connection, so is free without being on `free`. */
|
|
590
603
|
private used: i32 = 0
|
|
591
|
-
/** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
|
|
592
|
-
private stopped: boolean = false
|
|
593
604
|
/** What is left of the answer budget (see `answerRefilled`). */
|
|
594
605
|
private answerBudget: i32 = 0
|
|
595
606
|
/** Connections accepted into a slot, for a log or a test; saturating at 2^31 - 1. */
|
|
@@ -599,21 +610,18 @@ export class Http3Server {
|
|
|
599
610
|
/** Datagrams no slot owns dropped past the answer budget before the listener read them, saturating. */
|
|
600
611
|
limited: i32 = 0
|
|
601
612
|
private sendErrors: i32 = 0
|
|
602
|
-
/** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
|
|
603
|
-
ecn: boolean = true
|
|
604
613
|
/**
|
|
605
|
-
* The
|
|
606
|
-
*
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
* shares it only when its stream buffers are the pools' size.
|
|
614
|
+
* The ECN bits of the datagram being handed on (`udpRecvFrom`'s second
|
|
615
|
+
* metadata word, 0 where the platform does not report them), which `serve`
|
|
616
|
+
* gives the slot's connection as its `arrivalEcn`, so that the server's
|
|
617
|
+
* ACKs echo the counts (RFC 9000 §13.4.1). `receive` sets it for every
|
|
618
|
+
* datagram it reads; a caller of `datagram` sets it before the call.
|
|
611
619
|
*/
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
|
|
615
|
-
/**
|
|
616
|
-
|
|
620
|
+
arrivalEcn: i32 = 0
|
|
621
|
+
/** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
|
|
622
|
+
ecn: boolean = true
|
|
623
|
+
/** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
|
|
624
|
+
private stopped: boolean = false
|
|
617
625
|
|
|
618
626
|
/**
|
|
619
627
|
* A pool of `size` slots serving HTTP/3 under `config` over QUIC under
|
|
@@ -859,6 +867,8 @@ export class Http3Server {
|
|
|
859
867
|
return count
|
|
860
868
|
}
|
|
861
869
|
const segment: i32 = this.meta[0] > 0 ? this.meta[0] : n
|
|
870
|
+
// One receive is one ECN codepoint for every GRO segment of it.
|
|
871
|
+
this.arrivalEcn = this.meta[1] & 3
|
|
862
872
|
let at: i32 = 0
|
|
863
873
|
while (at < n && segment > 0) {
|
|
864
874
|
const length: i32 = n - at < segment ? n - at : segment
|
|
@@ -1045,6 +1055,7 @@ export class Http3Server {
|
|
|
1045
1055
|
serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
|
|
1046
1056
|
const quic: QuicConnection = this.quics[slot]
|
|
1047
1057
|
quic.recovery.micros = this.micros
|
|
1058
|
+
quic.arrivalEcn = this.arrivalEcn
|
|
1048
1059
|
quic.receiveWindowFrom(buf, at, len, address, now)
|
|
1049
1060
|
const input: u8[] | null = quic.signatureInput()
|
|
1050
1061
|
if (input !== null) {
|
package/std/net/http3.ts
CHANGED
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
* import { QuicConnection } from "nish/net/quic";
|
|
18
18
|
*
|
|
19
19
|
* // Once per slot, beside a server QuicConnection; restart() per connection.
|
|
20
|
-
*
|
|
20
|
+
* const open = (quic: QuicConnection): Http3Connection => {
|
|
21
21
|
* return new Http3Connection(new Http3Config(), quic);
|
|
22
22
|
* }
|
|
23
23
|
*
|
|
24
24
|
* // After each quic.receive(...):
|
|
25
|
-
*
|
|
25
|
+
* const serve = (h3: Http3Connection): void => {
|
|
26
26
|
* const names: u8[][] = [httpFieldBytes("content-type")];
|
|
27
27
|
* const values: u8[][] = [httpFieldBytes("text/plain")];
|
|
28
28
|
* const body: u8[] = httpFieldBytes("hello\n");
|