2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-05-01 11:40:00 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file camelcase
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
2023-02-12 15:51:07 -05:00
|
|
|
const internals = globalThis.__bootstrap.internals;
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const { BadResourcePrototype, InterruptedPrototype, ops } = core;
|
2023-06-06 05:01:28 -04:00
|
|
|
const { op_http_write } = Deno.core.ensureFastOps();
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { InnerBody } from "ext:deno_fetch/22_body.js";
|
|
|
|
import { Event, setEventTargetData } from "ext:deno_web/02_event.js";
|
|
|
|
import { BlobPrototype } from "ext:deno_web/09_file.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
fromInnerResponse,
|
|
|
|
newInnerResponse,
|
|
|
|
ResponsePrototype,
|
|
|
|
toInnerResponse,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_fetch/23_response.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
fromInnerRequest,
|
|
|
|
newInnerRequest,
|
2023-04-02 17:27:12 -04:00
|
|
|
toInnerRequest,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_fetch/23_request.js";
|
2023-03-15 18:37:06 -04:00
|
|
|
import { AbortController } from "ext:deno_web/03_abort_signal.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
_eventLoop,
|
|
|
|
_idleTimeoutDuration,
|
|
|
|
_idleTimeoutTimeout,
|
|
|
|
_protocol,
|
|
|
|
_readyState,
|
|
|
|
_rid,
|
2023-04-06 09:14:31 -04:00
|
|
|
_role,
|
2023-02-07 14:22:46 -05:00
|
|
|
_server,
|
|
|
|
_serverHandleIdleTimeout,
|
2023-04-06 09:14:31 -04:00
|
|
|
SERVER,
|
2023-02-07 14:22:46 -05:00
|
|
|
WebSocket,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_websocket/01_websocket.js";
|
2023-04-22 13:48:21 -04:00
|
|
|
import { TcpConn, UnixConn } from "ext:deno_net/01_net.js";
|
|
|
|
import { TlsConn } from "ext:deno_net/02_tls.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
Deferred,
|
|
|
|
getReadableStreamResourceBacking,
|
|
|
|
readableStreamClose,
|
|
|
|
readableStreamForRid,
|
|
|
|
ReadableStreamPrototype,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/06_streams.js";
|
2023-04-22 13:48:21 -04:00
|
|
|
import { serve } from "ext:deno_http/00_serve.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayPrototypeIncludes,
|
2023-02-12 15:51:07 -05:00
|
|
|
ArrayPrototypeMap,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush,
|
|
|
|
Error,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeSet,
|
2023-02-07 14:22:46 -05:00
|
|
|
SafeSetIterator,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeDelete,
|
2023-02-12 15:51:07 -05:00
|
|
|
StringPrototypeCharCodeAt,
|
2023-02-07 14:22:46 -05:00
|
|
|
StringPrototypeIncludes,
|
|
|
|
StringPrototypeSplit,
|
2023-05-01 09:30:02 -04:00
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
StringPrototypeToUpperCase,
|
2023-02-07 14:22:46 -05:00
|
|
|
Symbol,
|
|
|
|
SymbolAsyncIterator,
|
|
|
|
TypeError,
|
|
|
|
Uint8Array,
|
|
|
|
Uint8ArrayPrototype,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const connErrorSymbol = Symbol("connError");
|
|
|
|
const _deferred = Symbol("upgradeHttpDeferred");
|
|
|
|
|
|
|
|
class HttpConn {
|
|
|
|
#rid = 0;
|
|
|
|
#closed = false;
|
|
|
|
#remoteAddr;
|
|
|
|
#localAddr;
|
|
|
|
|
|
|
|
// This set holds resource ids of resources
|
|
|
|
// that were created during lifecycle of this request.
|
|
|
|
// When the connection is closed these resources should be closed
|
|
|
|
// as well.
|
2023-04-14 16:23:28 -04:00
|
|
|
managedResources = new SafeSet();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
constructor(rid, remoteAddr, localAddr) {
|
|
|
|
this.#rid = rid;
|
|
|
|
this.#remoteAddr = remoteAddr;
|
|
|
|
this.#localAddr = localAddr;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {number} */
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
2021-10-04 21:50:40 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {Promise<RequestEvent | null>} */
|
|
|
|
async nextRequest() {
|
|
|
|
let nextRequest;
|
|
|
|
try {
|
|
|
|
nextRequest = await core.opAsync("op_http_accept", this.#rid);
|
|
|
|
} catch (error) {
|
|
|
|
this.close();
|
|
|
|
// A connection error seen here would cause disrupted responses to throw
|
|
|
|
// a generic `BadResource` error. Instead store this error and replace
|
|
|
|
// those with it.
|
|
|
|
this[connErrorSymbol] = error;
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error) ||
|
|
|
|
StringPrototypeIncludes(error.message, "connection closed")
|
|
|
|
) {
|
2021-10-04 21:50:40 -04:00
|
|
|
return null;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
if (nextRequest == null) {
|
|
|
|
// Work-around for servers (deno_std/http in particular) that call
|
|
|
|
// `nextRequest()` before upgrading a previous request which has a
|
|
|
|
// `connection: upgrade` header.
|
|
|
|
await null;
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
return null;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: streamRid, 1: method, 2: url } = nextRequest;
|
|
|
|
SetPrototypeAdd(this.managedResources, streamRid);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @type {ReadableStream<Uint8Array> | undefined} */
|
|
|
|
let body = null;
|
|
|
|
// There might be a body, but we don't expose it for GET/HEAD requests.
|
|
|
|
// It will be closed automatically once the request has been handled and
|
|
|
|
// the response has been sent.
|
|
|
|
if (method !== "GET" && method !== "HEAD") {
|
|
|
|
body = readableStreamForRid(streamRid, false);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const innerRequest = newInnerRequest(
|
2023-04-04 06:37:56 -04:00
|
|
|
method,
|
2023-02-07 14:22:46 -05:00
|
|
|
url,
|
|
|
|
() => ops.op_http_headers(streamRid),
|
|
|
|
body !== null ? new InnerBody(body) : null,
|
|
|
|
false,
|
|
|
|
);
|
2023-04-02 17:27:12 -04:00
|
|
|
innerRequest[streamRid] = streamRid;
|
2023-03-15 18:37:06 -04:00
|
|
|
const abortController = new AbortController();
|
2023-02-07 14:22:46 -05:00
|
|
|
const request = fromInnerRequest(
|
|
|
|
innerRequest,
|
2023-03-15 18:37:06 -04:00
|
|
|
abortController.signal,
|
2023-02-07 14:22:46 -05:00
|
|
|
"immutable",
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
const respondWith = createRespondWith(
|
|
|
|
this,
|
|
|
|
streamRid,
|
|
|
|
request,
|
|
|
|
this.#remoteAddr,
|
|
|
|
this.#localAddr,
|
2023-03-15 18:37:06 -04:00
|
|
|
abortController,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
return { request, respondWith };
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {void} */
|
|
|
|
close() {
|
|
|
|
if (!this.#closed) {
|
|
|
|
this.#closed = true;
|
|
|
|
core.close(this.#rid);
|
|
|
|
for (const rid of new SafeSetIterator(this.managedResources)) {
|
|
|
|
SetPrototypeDelete(this.managedResources, rid);
|
|
|
|
core.close(rid);
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolAsyncIterator]() {
|
|
|
|
// deno-lint-ignore no-this-alias
|
|
|
|
const httpConn = this;
|
|
|
|
return {
|
|
|
|
async next() {
|
|
|
|
const reqEvt = await httpConn.nextRequest();
|
|
|
|
// Change with caution, current form avoids a v8 deopt
|
|
|
|
return { value: reqEvt ?? undefined, done: reqEvt === null };
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRespondWith(
|
|
|
|
httpConn,
|
|
|
|
streamRid,
|
|
|
|
request,
|
|
|
|
remoteAddr,
|
|
|
|
localAddr,
|
2023-03-15 18:37:06 -04:00
|
|
|
abortController,
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
return async function respondWith(resp) {
|
|
|
|
try {
|
|
|
|
resp = await resp;
|
|
|
|
if (!(ObjectPrototypeIsPrototypeOf(ResponsePrototype, resp))) {
|
|
|
|
throw new TypeError(
|
|
|
|
"First argument to respondWith must be a Response or a promise resolving to a Response.",
|
|
|
|
);
|
|
|
|
}
|
2021-10-04 21:50:40 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const innerResp = toInnerResponse(resp);
|
2021-10-04 21:50:40 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// If response body length is known, it will be sent synchronously in a
|
|
|
|
// single op, in other case a "response body" resource will be created and
|
|
|
|
// we'll be streaming it.
|
|
|
|
/** @type {ReadableStream<Uint8Array> | Uint8Array | null} */
|
|
|
|
let respBody = null;
|
|
|
|
if (innerResp.body !== null) {
|
|
|
|
if (innerResp.body.unusable()) {
|
|
|
|
throw new TypeError("Body is unusable.");
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
innerResp.body.streamOrStatic,
|
|
|
|
)
|
|
|
|
) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
innerResp.body.length === null ||
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf(
|
2023-02-07 14:22:46 -05:00
|
|
|
BlobPrototype,
|
|
|
|
innerResp.body.source,
|
2022-02-01 12:06:11 -05:00
|
|
|
)
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
respBody = innerResp.body.stream;
|
|
|
|
} else {
|
|
|
|
const reader = innerResp.body.stream.getReader();
|
|
|
|
const r1 = await reader.read();
|
|
|
|
if (r1.done) {
|
|
|
|
respBody = new Uint8Array(0);
|
2021-05-20 21:11:53 -04:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
respBody = r1.value;
|
|
|
|
const r2 = await reader.read();
|
|
|
|
if (!r2.done) throw new TypeError("Unreachable");
|
2021-05-20 21:11:53 -04:00
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
innerResp.body.streamOrStatic.consumed = true;
|
|
|
|
respBody = innerResp.body.streamOrStatic.body;
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
respBody = new Uint8Array(0);
|
|
|
|
}
|
|
|
|
const isStreamingResponseBody = !(
|
|
|
|
typeof respBody === "string" ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, respBody)
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
await core.opAsync(
|
|
|
|
"op_http_write_headers",
|
|
|
|
streamRid,
|
|
|
|
innerResp.status ?? 200,
|
|
|
|
innerResp.headerList,
|
|
|
|
isStreamingResponseBody ? null : respBody,
|
2021-11-12 14:21:58 -05:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (error) {
|
|
|
|
const connError = httpConn[connErrorSymbol];
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) &&
|
|
|
|
connError != null
|
|
|
|
) {
|
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
respBody !== null &&
|
|
|
|
ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, respBody)
|
|
|
|
) {
|
|
|
|
await respBody.cancel(error);
|
2021-11-09 06:10:21 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw error;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (isStreamingResponseBody) {
|
|
|
|
let success = false;
|
|
|
|
if (
|
|
|
|
respBody === null ||
|
|
|
|
!ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, respBody)
|
|
|
|
) {
|
|
|
|
throw new TypeError("Unreachable");
|
|
|
|
}
|
|
|
|
const resourceBacking = getReadableStreamResourceBacking(respBody);
|
|
|
|
let reader;
|
|
|
|
if (resourceBacking) {
|
|
|
|
if (respBody.locked) {
|
|
|
|
throw new TypeError("ReadableStream is locked.");
|
|
|
|
}
|
2023-06-26 09:10:27 -04:00
|
|
|
reader = respBody.getReader(); // Acquire JS lock.
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
await core.opAsync(
|
|
|
|
"op_http_write_resource",
|
|
|
|
streamRid,
|
|
|
|
resourceBacking.rid,
|
|
|
|
);
|
|
|
|
if (resourceBacking.autoClose) core.tryClose(resourceBacking.rid);
|
|
|
|
readableStreamClose(respBody); // Release JS lock.
|
|
|
|
success = true;
|
|
|
|
} catch (error) {
|
|
|
|
const connError = httpConn[connErrorSymbol];
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) &&
|
|
|
|
connError != null
|
|
|
|
) {
|
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
|
|
|
await reader.cancel(error);
|
|
|
|
throw error;
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
reader = respBody.getReader();
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
if (!ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, value)) {
|
|
|
|
await reader.cancel(new TypeError("Value not a Uint8Array"));
|
|
|
|
break;
|
2021-04-23 06:34:04 -04:00
|
|
|
}
|
|
|
|
try {
|
2023-05-01 11:40:00 -04:00
|
|
|
await op_http_write(streamRid, value);
|
2021-04-23 06:34:04 -04:00
|
|
|
} catch (error) {
|
2022-04-20 20:22:55 -04:00
|
|
|
const connError = httpConn[connErrorSymbol];
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) &&
|
|
|
|
connError != null
|
|
|
|
) {
|
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
2021-04-23 06:34:04 -04:00
|
|
|
await reader.cancel(error);
|
|
|
|
throw error;
|
|
|
|
}
|
2022-04-20 20:22:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
success = true;
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (success) {
|
|
|
|
try {
|
|
|
|
await core.opAsync("op_http_shutdown", streamRid);
|
|
|
|
} catch (error) {
|
|
|
|
await reader.cancel(error);
|
|
|
|
throw error;
|
2022-03-16 09:54:18 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 09:54:18 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const deferred = request[_deferred];
|
|
|
|
if (deferred) {
|
|
|
|
const res = await core.opAsync("op_http_upgrade", streamRid);
|
|
|
|
let conn;
|
|
|
|
if (res.connType === "tcp") {
|
|
|
|
conn = new TcpConn(res.connRid, remoteAddr, localAddr);
|
|
|
|
} else if (res.connType === "tls") {
|
|
|
|
conn = new TlsConn(res.connRid, remoteAddr, localAddr);
|
|
|
|
} else if (res.connType === "unix") {
|
|
|
|
conn = new UnixConn(res.connRid, remoteAddr, localAddr);
|
|
|
|
} else {
|
|
|
|
throw new Error("unreachable");
|
2022-03-16 09:54:18 -04:00
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
deferred.resolve([conn, res.readBuf]);
|
|
|
|
}
|
|
|
|
const ws = resp[_ws];
|
|
|
|
if (ws) {
|
|
|
|
const wsRid = await core.opAsync(
|
|
|
|
"op_http_upgrade_websocket",
|
|
|
|
streamRid,
|
|
|
|
);
|
|
|
|
ws[_rid] = wsRid;
|
|
|
|
ws[_protocol] = resp.headers.get("sec-websocket-protocol");
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
httpConn.close();
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ws[_readyState] = WebSocket.OPEN;
|
2023-04-06 09:14:31 -04:00
|
|
|
ws[_role] = SERVER;
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new Event("open");
|
|
|
|
ws.dispatchEvent(event);
|
|
|
|
|
|
|
|
ws[_eventLoop]();
|
|
|
|
if (ws[_idleTimeoutDuration]) {
|
|
|
|
ws.addEventListener(
|
|
|
|
"close",
|
|
|
|
() => clearTimeout(ws[_idleTimeoutTimeout]),
|
|
|
|
);
|
2021-07-08 07:33:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ws[_serverHandleIdleTimeout]();
|
2021-07-08 07:33:01 -04:00
|
|
|
}
|
2023-03-15 18:37:06 -04:00
|
|
|
} catch (error) {
|
|
|
|
abortController.abort(error);
|
|
|
|
throw error;
|
2023-02-07 14:22:46 -05:00
|
|
|
} finally {
|
|
|
|
if (SetPrototypeDelete(httpConn.managedResources, streamRid)) {
|
|
|
|
core.close(streamRid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const _ws = Symbol("[[associated_ws]]");
|
2023-02-12 15:51:07 -05:00
|
|
|
const websocketCvf = buildCaseInsensitiveCommaValueFinder("websocket");
|
|
|
|
const upgradeCvf = buildCaseInsensitiveCommaValueFinder("upgrade");
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
function upgradeWebSocket(request, options = {}) {
|
2023-04-22 13:48:21 -04:00
|
|
|
const inner = toInnerRequest(request);
|
2023-02-07 14:22:46 -05:00
|
|
|
const upgrade = request.headers.get("upgrade");
|
|
|
|
const upgradeHasWebSocketOption = upgrade !== null &&
|
2023-02-12 15:51:07 -05:00
|
|
|
websocketCvf(upgrade);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!upgradeHasWebSocketOption) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Invalid Header: 'upgrade' header must contain 'websocket'",
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const connection = request.headers.get("connection");
|
|
|
|
const connectionHasUpgradeOption = connection !== null &&
|
2023-02-12 15:51:07 -05:00
|
|
|
upgradeCvf(connection);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!connectionHasUpgradeOption) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Invalid Header: 'connection' header must contain 'Upgrade'",
|
|
|
|
);
|
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const websocketKey = request.headers.get("sec-websocket-key");
|
|
|
|
if (websocketKey === null) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Invalid Header: 'sec-websocket-key' header must be set",
|
|
|
|
);
|
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const accept = ops.op_http_websocket_accept_header(websocketKey);
|
|
|
|
|
|
|
|
const r = newInnerResponse(101);
|
|
|
|
r.headerList = [
|
|
|
|
["upgrade", "websocket"],
|
|
|
|
["connection", "Upgrade"],
|
|
|
|
["sec-websocket-accept", accept],
|
|
|
|
];
|
|
|
|
|
|
|
|
const protocolsStr = request.headers.get("sec-websocket-protocol") || "";
|
|
|
|
const protocols = StringPrototypeSplit(protocolsStr, ", ");
|
|
|
|
if (protocols && options.protocol) {
|
|
|
|
if (ArrayPrototypeIncludes(protocols, options.protocol)) {
|
|
|
|
ArrayPrototypePush(r.headerList, [
|
|
|
|
"sec-websocket-protocol",
|
|
|
|
options.protocol,
|
|
|
|
]);
|
|
|
|
} else {
|
2021-07-08 07:33:01 -04:00
|
|
|
throw new TypeError(
|
2023-02-07 14:22:46 -05:00
|
|
|
`Protocol '${options.protocol}' not in the request's protocol list (non negotiable)`,
|
2021-07-08 07:33:01 -04:00
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const socket = webidl.createBranded(WebSocket);
|
|
|
|
setEventTargetData(socket);
|
|
|
|
socket[_server] = true;
|
|
|
|
socket[_idleTimeoutDuration] = options.idleTimeout ?? 120;
|
|
|
|
socket[_idleTimeoutTimeout] = null;
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-04-22 13:48:21 -04:00
|
|
|
if (inner._wantsUpgrade) {
|
|
|
|
return inner._wantsUpgrade("upgradeWebSocket", r, socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = fromInnerResponse(r, "immutable");
|
|
|
|
|
|
|
|
response[_ws] = socket;
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return { response, socket };
|
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function upgradeHttp(req) {
|
2023-04-22 13:48:21 -04:00
|
|
|
const inner = toInnerRequest(req);
|
|
|
|
if (inner._wantsUpgrade) {
|
|
|
|
return inner._wantsUpgrade("upgradeHttp", arguments);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
req[_deferred] = new Deferred();
|
|
|
|
return req[_deferred].promise;
|
|
|
|
}
|
2022-08-18 08:05:02 -04:00
|
|
|
|
2023-02-12 15:51:07 -05:00
|
|
|
const spaceCharCode = StringPrototypeCharCodeAt(" ", 0);
|
|
|
|
const tabCharCode = StringPrototypeCharCodeAt("\t", 0);
|
|
|
|
const commaCharCode = StringPrototypeCharCodeAt(",", 0);
|
|
|
|
|
|
|
|
/** Builds a case function that can be used to find a case insensitive
|
|
|
|
* value in some text that's separated by commas.
|
|
|
|
*
|
|
|
|
* This is done because it doesn't require any allocations.
|
|
|
|
* @param checkText {string} - The text to find. (ex. "websocket")
|
|
|
|
*/
|
|
|
|
function buildCaseInsensitiveCommaValueFinder(checkText) {
|
|
|
|
const charCodes = ArrayPrototypeMap(
|
|
|
|
StringPrototypeSplit(
|
|
|
|
StringPrototypeToLowerCase(checkText),
|
|
|
|
"",
|
|
|
|
),
|
2023-05-01 09:30:02 -04:00
|
|
|
(c) => [
|
|
|
|
StringPrototypeCharCodeAt(c, 0),
|
|
|
|
StringPrototypeCharCodeAt(StringPrototypeToUpperCase(c), 0),
|
|
|
|
],
|
2023-02-12 15:51:07 -05:00
|
|
|
);
|
|
|
|
/** @type {number} */
|
|
|
|
let i;
|
|
|
|
/** @type {number} */
|
|
|
|
let char;
|
|
|
|
|
2023-05-01 09:30:02 -04:00
|
|
|
/** @param {string} value */
|
2023-02-12 15:51:07 -05:00
|
|
|
return function (value) {
|
|
|
|
for (i = 0; i < value.length; i++) {
|
2023-05-01 09:30:02 -04:00
|
|
|
char = StringPrototypeCharCodeAt(value, i);
|
2023-02-12 15:51:07 -05:00
|
|
|
skipWhitespace(value);
|
|
|
|
|
|
|
|
if (hasWord(value)) {
|
|
|
|
skipWhitespace(value);
|
|
|
|
if (i === value.length || char === commaCharCode) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skipUntilComma(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @param value {string} */
|
|
|
|
function hasWord(value) {
|
2023-03-13 06:24:31 -04:00
|
|
|
for (let j = 0; j < charCodes.length; ++j) {
|
|
|
|
const { 0: cLower, 1: cUpper } = charCodes[j];
|
2023-02-12 15:51:07 -05:00
|
|
|
if (cLower === char || cUpper === char) {
|
|
|
|
char = StringPrototypeCharCodeAt(value, ++i);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param value {string} */
|
|
|
|
function skipWhitespace(value) {
|
|
|
|
while (char === spaceCharCode || char === tabCharCode) {
|
|
|
|
char = StringPrototypeCharCodeAt(value, ++i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param value {string} */
|
|
|
|
function skipUntilComma(value) {
|
|
|
|
while (char !== commaCharCode && i < value.length) {
|
|
|
|
char = StringPrototypeCharCodeAt(value, ++i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expose this function for unit tests
|
|
|
|
internals.buildCaseInsensitiveCommaValueFinder =
|
|
|
|
buildCaseInsensitiveCommaValueFinder;
|
|
|
|
|
2023-04-26 18:58:18 -04:00
|
|
|
export { _ws, HttpConn, serve, upgradeHttp, upgradeWebSocket };
|