Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Merge the round 1 review fixes of #6854 (yamux, session, HTTP) into the WebSocket branch

Bugfixer 20694196c308a
diff --cc src/lib/e2e/http.ts
index d66fc32004,51945d6ce4..8ba9fc38ff
--- a/src/lib/e2e/http.ts
+++ b/src/lib/e2e/http.ts
@@@ -30,11 -30,12 +30,13 @@@ export interface StreamRequest
signal?: AbortSignal;
}
-/** The request head, refused before anything is sent when a part could split or reframe the request. */
-export function requestHead(method: string, path: string, headers: Headers, length: number | null): string {
+/** The request line, Host and the caller's headers, refused before anything is sent when a part could split or reframe
+ * the request. The caller adds its own framing and connection lines and the blank line. */
+export function headStart(method: string, path: string, headers: Headers): string {
if (!TOKEN.test(method)) throw new TypeError(`e2e http: bad method ${JSON.stringify(method)}`);
- if (!path.startsWith("/") || [...path].some((char) => char <= " " || char === "\x7f")) throw new TypeError("e2e http: path must be origin-form with no spaces or controls");
+ if (!path.startsWith("/") || [...path].some((char) => char <= " " || char === "\x7f" || char === "#")) {
+ throw new TypeError("e2e http: path must be origin-form with no spaces, controls or fragment");
+ }
let head = `${method} ${path} HTTP/1.1\r\nHost: companion\r\n`;
// Headers already refuses names that are not tokens and values with CR, LF or NUL.
for (const [name, value] of headers) {
@@@ -125,21 -113,25 +127,25 @@@ export interface Head
headers: Headers;
}
+ /** One `name: value` line of a head or the trailers. */
+ function appendField(headers: Headers, line: string): void {
+ const colon = line.indexOf(":");
+ const name = line.slice(0, colon);
+ if (colon < 1 || !TOKEN.test(name)) throw new HttpError("bad header line");
+ try {
+ headers.append(name, line.slice(colon + 1).trim());
+ } catch {
+ throw new HttpError(`bad value for ${name}`);
+ }
+ }
+
-function parseHead(bytes: Uint8Array): Head {
+export function parseHead(bytes: Uint8Array): Head {
const [statusLine, ...lines] = byteString(bytes).split("\r\n");
- const status = /^HTTP\/1\.[01] ([1-5]\d\d)(?: ([^\r\n]*))?$/.exec(statusLine);
+ // The reason phrase as Response takes it: tab, space, visible and obs-text bytes.
+ const status = /^HTTP\/1\.[01] ([1-5]\d\d)(?: ([\t\x20-\x7e\x80-\xff]*))?$/.exec(statusLine);
if (!status) throw new HttpError("bad status line");
const headers = new Headers();
- for (const line of lines) {
- const colon = line.indexOf(":");
- const name = line.slice(0, colon);
- if (colon < 1 || !TOKEN.test(name)) throw new HttpError("bad header line");
- try {
- headers.append(name, line.slice(colon + 1).trim());
- } catch {
- throw new HttpError(`bad value for ${name}`);
- }
- }
+ for (const line of lines) appendField(headers, line);
return { status: Number(status[1]), statusText: status[2] ?? "", headers };
}