@amritk/nish-x86_64-linux 0.42.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 +1 -1
- package/runtime/core.d.ts +16 -0
- package/runtime/core.mjs +5 -0
- package/runtime/nish.d.ts +120 -19
- package/runtime/nish.mjs +3 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +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/handler.ts +1 -1
- package/std/net/http-server.ts +2 -3
- package/std/net/http1-server.ts +281 -51
- package/std/net/http3-server.ts +26 -15
- package/std/net/http3.ts +2 -2
- 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/quic.ts
CHANGED
|
@@ -1712,6 +1712,17 @@ export class QuicConnection {
|
|
|
1712
1712
|
datagramEcn: i32 = 0
|
|
1713
1713
|
/** Whether the carrier sends with the ECN bits of `datagramEcn`; the carrier sets it, and `reset` keeps it. */
|
|
1714
1714
|
ecnMarking: boolean = false
|
|
1715
|
+
/**
|
|
1716
|
+
* The ECN bits the next datagram handed to `receive`, `receiveWindow` or
|
|
1717
|
+
* `receiveWindowFrom` arrived with (the IP header's two low traffic-class
|
|
1718
|
+
* bits: 0 Not-ECT, 1 ECT(1), 2 ECT(0), 3 CE), which the carrier sets before
|
|
1719
|
+
* the call and keeps for every coalesced packet of that datagram. Each
|
|
1720
|
+
* packet that opens and is not a duplicate adds them to its packet number
|
|
1721
|
+
* space's counts (RFC 9000 §13.4.1), which the space's ACKs then carry as
|
|
1722
|
+
* an ACK_ECN. A carrier that cannot read the bits leaves it 0, and its
|
|
1723
|
+
* ACKs stay plain. `reset` clears it.
|
|
1724
|
+
*/
|
|
1725
|
+
arrivalEcn: i32 = 0
|
|
1715
1726
|
/** Whether `error` is an application code (CONNECTION_CLOSE 0x1d) rather than a transport error. */
|
|
1716
1727
|
errorIsApplication: boolean = false
|
|
1717
1728
|
/** Whether the one CONNECTION_CLOSE this side owes has gone out. */
|
|
@@ -1953,6 +1964,7 @@ export class QuicConnection {
|
|
|
1953
1964
|
this.sealedUnderKey = 0
|
|
1954
1965
|
this.authFailures = 0
|
|
1955
1966
|
this.datagramEcn = 0
|
|
1967
|
+
this.arrivalEcn = 0
|
|
1956
1968
|
this.nextSkip = -1
|
|
1957
1969
|
quicConnForgetSkips(this.skipped)
|
|
1958
1970
|
this.skipsDrawn = 0
|
|
@@ -2549,7 +2561,10 @@ export class QuicConnection {
|
|
|
2549
2561
|
if (eliciting && type === QUIC_PACKET_HANDSHAKE) {
|
|
2550
2562
|
this.resendLevel = TLS_LEVEL_HANDSHAKE
|
|
2551
2563
|
}
|
|
2552
|
-
|
|
2564
|
+
// §13.4.1: a packet processed, not a duplicate, counts its datagram's ECN bits in its space.
|
|
2565
|
+
if (space.received.record(pn, eliciting)) {
|
|
2566
|
+
space.received.countEcn(this.arrivalEcn)
|
|
2567
|
+
}
|
|
2553
2568
|
// §10.1: a packet received and processed restarts the idle timer.
|
|
2554
2569
|
this.idleSince = this.now
|
|
2555
2570
|
this.elicitingSent = false
|
package/std/net/serve.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* const HOME: i32 = 0 // module constants, in the order the routes are added
|
|
10
10
|
* const USER: i32 = 1
|
|
11
11
|
*
|
|
12
|
-
* async
|
|
12
|
+
* const handle = async (req: Request): Promise<Response> => {
|
|
13
13
|
* switch (req.route) {
|
|
14
14
|
* case HOME: return new Response(200).text("hello\n")
|
|
15
15
|
* case USER: {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* }
|
|
21
21
|
* }
|
|
22
22
|
*
|
|
23
|
-
* export async
|
|
23
|
+
* export const main = async (): Promise<i32> => {
|
|
24
24
|
* const router = new Router(new RouterLimits())
|
|
25
25
|
* router.add("GET", "/")
|
|
26
26
|
* router.add("GET", "/users/:id")
|
|
@@ -818,7 +818,7 @@ const serveGrown = (have: i32, need: i32): i32 => {
|
|
|
818
818
|
|
|
819
819
|
/** One slot's exchange: what it needs after its task's copy, and its record until then. */
|
|
820
820
|
class ServeExchange {
|
|
821
|
-
/** The slot's connection: the carrier
|
|
821
|
+
/** The slot's connection as of its last drive: the carrier lends one to a busy slot, so `driveOn` sets it again. */
|
|
822
822
|
conn: Http1Connection
|
|
823
823
|
/** The record, from the head's parse to the task's copy; `ServeConns.none` otherwise. */
|
|
824
824
|
rec: HttpRequest
|
|
@@ -1052,7 +1052,6 @@ class ServeConns {
|
|
|
1052
1052
|
this.open[slot] = true
|
|
1053
1053
|
const e: ServeExchange = this.ex[slot]
|
|
1054
1054
|
e.state = SERVE_IDLE
|
|
1055
|
-
e.conn = this.server.connection(slot)
|
|
1056
1055
|
pollAdd(this.loop, this.server.fd(slot), H1_WANT_READ, slot)
|
|
1057
1056
|
}
|
|
1058
1057
|
slot = this.server.accept()
|
|
@@ -1083,7 +1082,10 @@ class ServeConns {
|
|
|
1083
1082
|
|
|
1084
1083
|
/** `drive` for a caller that holds slot `s`'s exchange `e`. */
|
|
1085
1084
|
driveOn(s: i32, e: ServeExchange): void {
|
|
1086
|
-
|
|
1085
|
+
// A slot's connection is lent to it while it is busy and given back when
|
|
1086
|
+
// it rests, so the exchange takes the one it holds now.
|
|
1087
|
+
const conn: Http1Connection = this.server.connection(s)
|
|
1088
|
+
e.conn = conn
|
|
1087
1089
|
let event: i32 = this.server.next(s)
|
|
1088
1090
|
while (event !== H1_NEED_MORE && event !== H1_ERROR) {
|
|
1089
1091
|
if (event === H1_REQUEST) {
|
|
@@ -1169,9 +1171,9 @@ class ServeConns {
|
|
|
1169
1171
|
* A server sized for many connections pays for the ones it has had.
|
|
1170
1172
|
*/
|
|
1171
1173
|
growSlots(slot: i32): void {
|
|
1172
|
-
// An exchange's connection is set
|
|
1173
|
-
// names the
|
|
1174
|
-
const conn: Http1Connection = this.server.connection(
|
|
1174
|
+
// An exchange's connection is set each time its slot is driven; until
|
|
1175
|
+
// then it names the pool's first, which this does not lend the slot.
|
|
1176
|
+
const conn: Http1Connection = this.server.connection(toI32(-1))
|
|
1175
1177
|
let n: i32 = toI32(this.ex.length) * 2
|
|
1176
1178
|
if (n < 64) {
|
|
1177
1179
|
n = 64
|
package/std/net/websocket.ts
CHANGED
|
@@ -9,7 +9,11 @@
|
|
|
9
9
|
* writes the 101 that accepts it, with `Sec-WebSocket-Accept` from
|
|
10
10
|
* `websocketAcceptKey` (§4.2.2): the base64 of the SHA-1 of the key and a
|
|
11
11
|
* fixed GUID. `websocketOriginAllowed(parser, allowed)` is the opt-in check
|
|
12
|
-
* of `Origin` a server makes first, against a page on another site.
|
|
12
|
+
* of `Origin` a server makes first, against a page on another site. A server
|
|
13
|
+
* that accepts handshake after handshake keeps a `WsAcceptKey` and writes the
|
|
14
|
+
* 101 into its output with `websocketRequestKeyField` and
|
|
15
|
+
* `websocketWriteUpgradeResponse`, allocating nothing per handshake, as
|
|
16
|
+
* `Http1Connection.acceptWebSocket` does. What `HTTP1_UPGRADE`'s `upgradeBytes()` answers is the first
|
|
13
17
|
* input for the decoder.
|
|
14
18
|
*
|
|
15
19
|
* **The client's handshake** (§4.1). `websocketClientKey(nonce)` spells 16
|
|
@@ -70,14 +74,14 @@
|
|
|
70
74
|
*
|
|
71
75
|
* Written from RFC 6455, not ported from another implementation.
|
|
72
76
|
*/
|
|
73
|
-
import { base64StdPadEncode,
|
|
74
|
-
import {
|
|
77
|
+
import { base64StdPadDecodedLength, base64StdPadEncode, base64StdPadEncodeInto } from "nish/crypto/base64url"
|
|
78
|
+
import { SHA1_SIZE, Sha1 } from "nish/crypto/sha1"
|
|
75
79
|
import {
|
|
76
|
-
|
|
80
|
+
HTTP1_NO_ROOM,
|
|
81
|
+
HTTP1_REFUSED,
|
|
77
82
|
Http1Parser,
|
|
78
83
|
http1IsOws,
|
|
79
84
|
http1IsToken,
|
|
80
|
-
http1ResponseHead,
|
|
81
85
|
http1SameWord,
|
|
82
86
|
http1Text,
|
|
83
87
|
} from "nish/net/http1-codec"
|
|
@@ -864,23 +868,79 @@ const websocketBytesOf = (text: string): u8[] => {
|
|
|
864
868
|
return out
|
|
865
869
|
}
|
|
866
870
|
|
|
871
|
+
/** The length of a valid `Sec-WebSocket-Key`: 16 bytes in padded base64. */
|
|
872
|
+
/** Zero as an `i32`, for offsets under `--number-mode f64`. */
|
|
873
|
+
const WS_ZERO: i32 = 0
|
|
874
|
+
|
|
875
|
+
const WS_KEY_LENGTH: i32 = 24
|
|
876
|
+
/** The length of `Sec-WebSocket-Accept`: 20 bytes of SHA-1 in padded base64. */
|
|
877
|
+
const WS_ACCEPT_LENGTH: i32 = 28
|
|
878
|
+
|
|
867
879
|
/**
|
|
868
|
-
* `text`
|
|
869
|
-
*
|
|
880
|
+
* Whether `text[start .. end)` is a valid `Sec-WebSocket-Key` (§4.2.1 item 5):
|
|
881
|
+
* 24 characters of padded standard base64 that decode to 16 bytes in their
|
|
882
|
+
* one canonical spelling, read where they lie and allocating nothing.
|
|
870
883
|
*/
|
|
871
|
-
const
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
884
|
+
const websocketKeySpanIsValid = (text: u8[], start: i32, end: i32): boolean =>
|
|
885
|
+
start >= 0 &&
|
|
886
|
+
end <= toI32(text.length) &&
|
|
887
|
+
end - start === WS_KEY_LENGTH &&
|
|
888
|
+
base64StdPadDecodedLength(text, start, WS_KEY_LENGTH) === 16
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* The accept key of one handshake after another, in storage the caller keeps:
|
|
892
|
+
* a server builds one per connection and computes every handshake's
|
|
893
|
+
* `Sec-WebSocket-Accept` into it, allocating nothing after it is built (the
|
|
894
|
+
* fix for H1-3 in `docs/security/http1-codec.md`).
|
|
895
|
+
*
|
|
896
|
+
* const accept = new WsAcceptKey();
|
|
897
|
+
* if (accept.compute(p.head, start, end)) {
|
|
898
|
+
* // accept.value[0 .. 28) is the field's value
|
|
899
|
+
* }
|
|
900
|
+
*/
|
|
901
|
+
export class WsAcceptKey {
|
|
902
|
+
/** The hasher, its state kept from one handshake to the next. */
|
|
903
|
+
hasher: Sha1
|
|
904
|
+
/** The key followed by `WS_GUID`: what is hashed (§4.2.2 item 5.4). */
|
|
905
|
+
input: u8[]
|
|
906
|
+
/** The SHA-1 of `input`. */
|
|
907
|
+
digest: u8[]
|
|
908
|
+
/** The accept value's 28 characters, one a byte, once `compute` answers true. */
|
|
909
|
+
value: u8[]
|
|
910
|
+
|
|
911
|
+
/** Storage for one accept value, its hasher and buffers allocated. */
|
|
912
|
+
constructor() {
|
|
913
|
+
this.hasher = new Sha1()
|
|
914
|
+
const guid: i32 = toI32(WS_GUID.length)
|
|
915
|
+
this.input = new Array<u8>(WS_KEY_LENGTH + guid)
|
|
916
|
+
for (let k: i32 = 0; k < guid; k += 1) {
|
|
917
|
+
this.input[WS_KEY_LENGTH + k] = toU8(WS_GUID.charCodeAt(k))
|
|
880
918
|
}
|
|
881
|
-
|
|
919
|
+
this.digest = new Array<u8>(SHA1_SIZE)
|
|
920
|
+
this.value = new Array<u8>(WS_ACCEPT_LENGTH)
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* Computes into `value` the accept value of the key in `text[start .. end)`
|
|
925
|
+
* (§4.2.2 item 5.4), when that is a valid key.
|
|
926
|
+
*
|
|
927
|
+
* @param text The buffer holding the client's key, one character a byte, such as an `Http1Parser`'s `head`.
|
|
928
|
+
* @param start Where in `text` the key starts.
|
|
929
|
+
* @param end Where in `text` it ends.
|
|
930
|
+
* @returns True with `value` written, or false, `value` unchanged, when the key is not valid.
|
|
931
|
+
*/
|
|
932
|
+
compute(text: u8[], start: i32, end: i32): boolean {
|
|
933
|
+
if (!websocketKeySpanIsValid(text, start, end)) {
|
|
934
|
+
return false
|
|
935
|
+
}
|
|
936
|
+
const input: u8[] = this.input
|
|
937
|
+
for (let k: i32 = 0; k < WS_KEY_LENGTH && k < toI32(input.length); k += 1) {
|
|
938
|
+
input[k] = text[start + k]
|
|
939
|
+
}
|
|
940
|
+
this.hasher.digestInto(input, WS_ZERO, toI32(input.length), this.digest, WS_ZERO)
|
|
941
|
+
base64StdPadEncodeInto(this.digest, WS_ZERO, SHA1_SIZE, this.value, WS_ZERO)
|
|
942
|
+
return true
|
|
882
943
|
}
|
|
883
|
-
return parts.join("")
|
|
884
944
|
}
|
|
885
945
|
|
|
886
946
|
/**
|
|
@@ -890,8 +950,14 @@ const websocketSwapBytes = (text: string, from1: i32, to1: i32, from2: i32, to2:
|
|
|
890
950
|
* @param key The key as the client sent it; it is not checked here, so check it with `websocketKeyIsValid` first.
|
|
891
951
|
* @returns The 28-character accept value.
|
|
892
952
|
*/
|
|
893
|
-
export const websocketAcceptKey = (key: string): string =>
|
|
894
|
-
|
|
953
|
+
export const websocketAcceptKey = (key: string): string => {
|
|
954
|
+
const input: u8[] = websocketBytesOf(`${key}${WS_GUID}`)
|
|
955
|
+
const digest: u8[] = new Array<u8>(SHA1_SIZE)
|
|
956
|
+
new Sha1().digestInto(input, WS_ZERO, toI32(input.length), digest, WS_ZERO)
|
|
957
|
+
const text: u8[] = new Array<u8>(WS_ACCEPT_LENGTH)
|
|
958
|
+
base64StdPadEncodeInto(digest, WS_ZERO, SHA1_SIZE, text, WS_ZERO)
|
|
959
|
+
return http1Text(text, 0, WS_ACCEPT_LENGTH, false)
|
|
960
|
+
}
|
|
895
961
|
|
|
896
962
|
/**
|
|
897
963
|
* Whether `key` is a valid `Sec-WebSocket-Key` (§4.2.1 item 5): padded
|
|
@@ -902,65 +968,138 @@ export const websocketAcceptKey = (key: string): string =>
|
|
|
902
968
|
* @returns True for a 24-character key ending in `==` whose other characters decode to 16 bytes.
|
|
903
969
|
*/
|
|
904
970
|
export const websocketKeyIsValid = (key: string): boolean => {
|
|
905
|
-
if (toI32(key.length) !==
|
|
971
|
+
if (toI32(key.length) !== WS_KEY_LENGTH) {
|
|
906
972
|
return false
|
|
907
973
|
}
|
|
908
|
-
const
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
974
|
+
const bytes: u8[] = websocketBytesOf(key)
|
|
975
|
+
return websocketKeySpanIsValid(bytes, 0, WS_KEY_LENGTH)
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Which field of the head `p` has just read holds the client's key, when that
|
|
980
|
+
* head is a valid opening handshake (§4.2.1): a GET in HTTP/1.1, asking to
|
|
981
|
+
* upgrade to `websocket` (alone, in any case), at version 13, with a valid
|
|
982
|
+
* first `Sec-WebSocket-Key`. Otherwise -1. Nothing is allocated: the key is
|
|
983
|
+
* `p.head[p.spans[4 * i + 2] .. p.spans[4 * i + 3])`, for `WsAcceptKey`.
|
|
984
|
+
*
|
|
985
|
+
* @param p The server's parser, at the `HTTP1_HEAD` of the request.
|
|
986
|
+
* @returns The key's field index, or -1 when the request is not a valid handshake.
|
|
987
|
+
*/
|
|
988
|
+
export const websocketRequestKeyField = (p: Http1Parser): i32 => {
|
|
989
|
+
if (!p.methodIs("GET") || p.minor !== 1 || !p.upgradeIs("websocket")) {
|
|
990
|
+
return -1
|
|
913
991
|
}
|
|
914
|
-
|
|
915
|
-
|
|
992
|
+
if (!p.headerIs("sec-websocket-version", "13")) {
|
|
993
|
+
return -1
|
|
994
|
+
}
|
|
995
|
+
const i: i32 = p.headerIndex("sec-websocket-key")
|
|
996
|
+
const spans: i32[] = p.spans
|
|
997
|
+
if (i < 0 || 4 * i + 3 >= toI32(spans.length)) {
|
|
998
|
+
return -1
|
|
999
|
+
}
|
|
1000
|
+
if (!websocketKeySpanIsValid(p.head, spans[4 * i + 2], spans[4 * i + 3])) {
|
|
1001
|
+
return -1
|
|
1002
|
+
}
|
|
1003
|
+
return i
|
|
916
1004
|
}
|
|
917
1005
|
|
|
918
1006
|
/**
|
|
919
1007
|
* The client's key when the head `p` has just read is a valid opening
|
|
920
|
-
* handshake (§4.2.1)
|
|
921
|
-
*
|
|
922
|
-
*
|
|
923
|
-
* version is wrong, §4.4).
|
|
1008
|
+
* handshake (§4.2.1), as `websocketRequestKeyField` judges it. Otherwise
|
|
1009
|
+
* `null`, and the server answers 400 (or 426 with `Sec-WebSocket-Version: 13`
|
|
1010
|
+
* when only the version is wrong, §4.4).
|
|
924
1011
|
*
|
|
925
1012
|
* @param p The server's parser, at the `HTTP1_HEAD` of the request.
|
|
926
1013
|
* @returns The `Sec-WebSocket-Key` to pass to `websocketUpgradeResponse`, or null when the request is not a valid handshake.
|
|
927
1014
|
*/
|
|
928
1015
|
export const websocketRequestKey = (p: Http1Parser): string | null => {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
if (!p.headerIs("sec-websocket-version", "13")) {
|
|
1016
|
+
const i: i32 = websocketRequestKeyField(p)
|
|
1017
|
+
const spans: i32[] = p.spans
|
|
1018
|
+
if (i < 0 || 4 * i + 3 >= toI32(spans.length)) {
|
|
933
1019
|
return null
|
|
934
1020
|
}
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
1021
|
+
return http1Text(p.head, spans[4 * i + 2], spans[4 * i + 3], false)
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/** Writes `text` into `out` from `at`; the caller has checked it fits. */
|
|
1025
|
+
const websocketPutText = (out: u8[], at: i32, text: string): i32 => {
|
|
1026
|
+
const n: i32 = toI32(text.length)
|
|
1027
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
1028
|
+
out[at + k] = toU8(text.charCodeAt(k))
|
|
938
1029
|
}
|
|
939
|
-
return
|
|
1030
|
+
return at + n
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/** The 101's status line and its first two fields, which never change. */
|
|
1034
|
+
const WS_SWITCHING: string =
|
|
1035
|
+
"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Writes the server's 101 response to an opening handshake (§4.2.2) into
|
|
1039
|
+
* `out` from `at`, allocating nothing: `Upgrade`, `Connection`,
|
|
1040
|
+
* `Sec-WebSocket-Accept` with the 28 bytes `accept` holds, and
|
|
1041
|
+
* `Sec-WebSocket-Protocol` when `protocol` is not empty. The bytes are those
|
|
1042
|
+
* of `websocketUpgradeResponse`.
|
|
1043
|
+
*
|
|
1044
|
+
* @param out The buffer the head is written into.
|
|
1045
|
+
* @param at Where in `out` it starts.
|
|
1046
|
+
* @param accept The accept value, its 28 characters one a byte: a `WsAcceptKey`'s `value`.
|
|
1047
|
+
* @param protocol The subprotocol the server chose from `websocketOfferedProtocols`, or "" for none.
|
|
1048
|
+
* @returns The index just past the head, `HTTP1_REFUSED` when `protocol` is not a token or `accept` is not 28 bytes, or `HTTP1_NO_ROOM` when the head does not fit, `out` unchanged either way.
|
|
1049
|
+
*/
|
|
1050
|
+
export const websocketWriteUpgradeResponse = (out: u8[], at: i32, accept: u8[], protocol: string): i32 => {
|
|
1051
|
+
const p: i32 = toI32(protocol.length)
|
|
1052
|
+
if (toI32(accept.length) !== WS_ACCEPT_LENGTH || (p > 0 && !http1IsToken(protocol)) || p > 65536) {
|
|
1053
|
+
return HTTP1_REFUSED
|
|
1054
|
+
}
|
|
1055
|
+
const fixed: i32 = toI32(WS_SWITCHING.length)
|
|
1056
|
+
const named: i32 = p > 0 ? 26 + p : 0
|
|
1057
|
+
const length: i32 = fixed + WS_ACCEPT_LENGTH + 2 + named + 2
|
|
1058
|
+
if (at < 0 || at > toI32(out.length) - length) {
|
|
1059
|
+
return HTTP1_NO_ROOM
|
|
1060
|
+
}
|
|
1061
|
+
let j: i32 = websocketPutText(out, at, WS_SWITCHING)
|
|
1062
|
+
for (let k: i32 = 0; k < toI32(accept.length); k += 1) {
|
|
1063
|
+
out[j + k] = accept[k]
|
|
1064
|
+
}
|
|
1065
|
+
j = websocketPutText(out, j + WS_ACCEPT_LENGTH, "\r\n")
|
|
1066
|
+
if (p > 0) {
|
|
1067
|
+
j = websocketPutText(out, j, "Sec-WebSocket-Protocol: ")
|
|
1068
|
+
j = websocketPutText(out, j, protocol)
|
|
1069
|
+
j = websocketPutText(out, j, "\r\n")
|
|
1070
|
+
}
|
|
1071
|
+
return websocketPutText(out, j, "\r\n")
|
|
940
1072
|
}
|
|
941
1073
|
|
|
942
1074
|
/**
|
|
943
1075
|
* The server's 101 response to an opening handshake (§4.2.2): `Upgrade`,
|
|
944
1076
|
* `Connection`, `Sec-WebSocket-Accept`, and `Sec-WebSocket-Protocol` when
|
|
945
1077
|
* `protocol` is not empty (the caller picks it from the client's list).
|
|
946
|
-
* Answers `null` for an invalid key or a protocol that is not a token.
|
|
1078
|
+
* Answers `null` for an invalid key or a protocol that is not a token. A
|
|
1079
|
+
* server answering handshake after handshake writes the same bytes with
|
|
1080
|
+
* `WsAcceptKey` and `websocketWriteUpgradeResponse` instead, allocating
|
|
1081
|
+
* nothing.
|
|
947
1082
|
*
|
|
948
1083
|
* @param key The client's `Sec-WebSocket-Key`, as `websocketRequestKey` gave it.
|
|
949
1084
|
* @param protocol The subprotocol the server chose from `websocketOfferedProtocols`, or "" for none.
|
|
950
1085
|
* @returns The whole response head, ending in the empty line, or null when refused.
|
|
951
1086
|
*/
|
|
952
1087
|
export const websocketUpgradeResponse = (key: string, protocol: string): u8[] | null => {
|
|
953
|
-
|
|
1088
|
+
const bytes: u8[] = websocketBytesOf(key)
|
|
1089
|
+
const accept = new WsAcceptKey()
|
|
1090
|
+
if (!accept.compute(bytes, WS_ZERO, toI32(bytes.length))) {
|
|
954
1091
|
return null
|
|
955
1092
|
}
|
|
956
|
-
const
|
|
957
|
-
const
|
|
958
|
-
if (
|
|
959
|
-
|
|
960
|
-
|
|
1093
|
+
const out: u8[] = new Array<u8>(toI32(WS_SWITCHING.length) + WS_ACCEPT_LENGTH + 30 + toI32(protocol.length))
|
|
1094
|
+
const end: i32 = websocketWriteUpgradeResponse(out, WS_ZERO, accept.value, protocol)
|
|
1095
|
+
if (end < 0) {
|
|
1096
|
+
return null
|
|
1097
|
+
}
|
|
1098
|
+
const head: u8[] = new Array<u8>(end)
|
|
1099
|
+
for (let k: i32 = 0; k < toI32(head.length) && k < toI32(out.length); k++) {
|
|
1100
|
+
head[k] = out[k]
|
|
961
1101
|
}
|
|
962
|
-
|
|
963
|
-
return http1ResponseHead(switching, "Switching Protocols", names, values, HTTP1_NO_BODY)
|
|
1102
|
+
return head
|
|
964
1103
|
}
|
|
965
1104
|
|
|
966
1105
|
/**
|