2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-04-08 18:34:15 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
2021-07-08 07:33:01 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2021-04-20 08:47:22 -04:00
|
|
|
const { InnerBody } = window.__bootstrap.fetchBody;
|
2022-10-24 10:14:17 -04:00
|
|
|
const { Event } = window.__bootstrap.event;
|
2021-07-08 07:33:01 -04:00
|
|
|
const { setEventTargetData } = window.__bootstrap.eventTarget;
|
2022-02-01 12:06:11 -05:00
|
|
|
const { BlobPrototype } = window.__bootstrap.file;
|
2021-07-08 07:33:01 -04:00
|
|
|
const {
|
2022-02-01 12:06:11 -05:00
|
|
|
ResponsePrototype,
|
2021-07-08 07:33:01 -04:00
|
|
|
fromInnerRequest,
|
|
|
|
toInnerResponse,
|
|
|
|
newInnerRequest,
|
|
|
|
newInnerResponse,
|
|
|
|
fromInnerResponse,
|
2022-08-18 08:05:02 -04:00
|
|
|
_flash,
|
2021-07-08 07:33:01 -04:00
|
|
|
} = window.__bootstrap.fetch;
|
2021-04-08 18:34:15 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const { BadResourcePrototype, InterruptedPrototype, ops } = core;
|
2022-09-30 01:54:12 -04:00
|
|
|
const { ReadableStreamPrototype } = window.__bootstrap.streams;
|
2021-06-25 18:01:25 -04:00
|
|
|
const abortSignal = window.__bootstrap.abortSignal;
|
2022-01-06 11:41:16 -05:00
|
|
|
const {
|
|
|
|
WebSocket,
|
|
|
|
_rid,
|
|
|
|
_readyState,
|
|
|
|
_eventLoop,
|
|
|
|
_protocol,
|
|
|
|
_server,
|
|
|
|
_idleTimeoutDuration,
|
|
|
|
_idleTimeoutTimeout,
|
|
|
|
_serverHandleIdleTimeout,
|
|
|
|
} = window.__bootstrap.webSocket;
|
2022-03-19 09:21:49 -04:00
|
|
|
const { TcpConn, UnixConn } = window.__bootstrap.net;
|
2022-03-16 09:54:18 -04:00
|
|
|
const { TlsConn } = window.__bootstrap.tls;
|
2022-09-30 01:54:12 -04:00
|
|
|
const {
|
|
|
|
Deferred,
|
|
|
|
getReadableStreamResourceBacking,
|
|
|
|
readableStreamForRid,
|
|
|
|
readableStreamClose,
|
|
|
|
} = window.__bootstrap.streams;
|
2021-07-03 11:02:14 -04:00
|
|
|
const {
|
2021-07-08 07:33:01 -04:00
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypePush,
|
2021-07-23 22:14:03 -04:00
|
|
|
ArrayPrototypeSome,
|
2022-03-16 09:54:18 -04:00
|
|
|
Error,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2022-12-19 21:37:50 -05:00
|
|
|
SafeSetIterator,
|
2021-08-23 10:15:59 -04:00
|
|
|
Set,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeDelete,
|
2021-07-03 11:02:14 -04:00
|
|
|
StringPrototypeIncludes,
|
2021-07-23 22:14:03 -04:00
|
|
|
StringPrototypeToLowerCase,
|
2021-07-08 07:33:01 -04:00
|
|
|
StringPrototypeSplit,
|
|
|
|
Symbol,
|
2021-07-03 11:02:14 -04:00
|
|
|
SymbolAsyncIterator,
|
2021-07-08 07:33:01 -04:00
|
|
|
TypeError,
|
|
|
|
Uint8Array,
|
2022-02-01 12:06:11 -05:00
|
|
|
Uint8ArrayPrototype,
|
2021-07-03 11:02:14 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-05-19 08:39:52 -04:00
|
|
|
const connErrorSymbol = Symbol("connError");
|
2022-03-16 09:54:18 -04:00
|
|
|
const _deferred = Symbol("upgradeHttpDeferred");
|
2021-05-19 08:39:52 -04:00
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
class HttpConn {
|
|
|
|
#rid = 0;
|
2021-10-04 21:50:40 -04:00
|
|
|
#closed = false;
|
2022-03-16 09:54:18 -04:00
|
|
|
#remoteAddr;
|
|
|
|
#localAddr;
|
2021-10-04 21:50:40 -04:00
|
|
|
|
2021-08-23 10:15:59 -04:00
|
|
|
// 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.
|
|
|
|
managedResources = new Set();
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
constructor(rid, remoteAddr, localAddr) {
|
2021-04-08 18:34:15 -04:00
|
|
|
this.#rid = rid;
|
2022-03-16 09:54:18 -04:00
|
|
|
this.#remoteAddr = remoteAddr;
|
|
|
|
this.#localAddr = localAddr;
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {number} */
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
2022-07-06 20:40:27 -04:00
|
|
|
/** @returns {Promise<RequestEvent | null>} */
|
2021-04-08 18:34:15 -04:00
|
|
|
async nextRequest() {
|
|
|
|
let nextRequest;
|
|
|
|
try {
|
2021-11-12 14:21:58 -05:00
|
|
|
nextRequest = await core.opAsync("op_http_accept", this.#rid);
|
2021-04-08 18:34:15 -04:00
|
|
|
} catch (error) {
|
2021-10-04 21:50:40 -04:00
|
|
|
this.close();
|
2021-05-19 08:39:52 -04:00
|
|
|
// 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;
|
2021-09-29 14:55:10 -04:00
|
|
|
if (
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error) ||
|
2021-07-03 11:02:14 -04:00
|
|
|
StringPrototypeIncludes(error.message, "connection closed")
|
|
|
|
) {
|
2021-05-19 08:39:52 -04:00
|
|
|
return null;
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-10-04 21:50:40 -04:00
|
|
|
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
|
|
|
|
2022-07-03 22:11:52 -04:00
|
|
|
const [streamRid, method, url] = nextRequest;
|
2021-10-04 21:50:40 -04:00
|
|
|
SetPrototypeAdd(this.managedResources, streamRid);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
/** @type {ReadableStream<Uint8Array> | undefined} */
|
2021-04-20 08:47:22 -04:00
|
|
|
let body = null;
|
2021-10-04 21:50:40 -04:00
|
|
|
// 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") {
|
2022-09-30 01:54:12 -04:00
|
|
|
body = readableStreamForRid(streamRid, false);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
const innerRequest = newInnerRequest(
|
2022-08-17 10:29:26 -04:00
|
|
|
() => method,
|
2021-04-20 08:47:22 -04:00
|
|
|
url,
|
2022-08-11 09:56:56 -04:00
|
|
|
() => ops.op_http_headers(streamRid),
|
2021-04-20 08:47:22 -04:00
|
|
|
body !== null ? new InnerBody(body) : null,
|
2021-09-27 07:19:24 -04:00
|
|
|
false,
|
2021-04-20 08:47:22 -04:00
|
|
|
);
|
2021-06-25 18:01:25 -04:00
|
|
|
const signal = abortSignal.newSignal();
|
2023-01-14 23:08:34 -05:00
|
|
|
const request = fromInnerRequest(
|
|
|
|
innerRequest,
|
|
|
|
signal,
|
|
|
|
"immutable",
|
|
|
|
false,
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
const respondWith = createRespondWith(
|
|
|
|
this,
|
|
|
|
streamRid,
|
|
|
|
request,
|
|
|
|
this.#remoteAddr,
|
|
|
|
this.#localAddr,
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
return { request, respondWith };
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {void} */
|
|
|
|
close() {
|
2021-10-04 21:50:40 -04:00
|
|
|
if (!this.#closed) {
|
|
|
|
this.#closed = true;
|
|
|
|
core.close(this.#rid);
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const rid of new SafeSetIterator(this.managedResources)) {
|
2021-10-04 21:50:40 -04:00
|
|
|
SetPrototypeDelete(this.managedResources, rid);
|
|
|
|
core.close(rid);
|
|
|
|
}
|
2021-08-23 10:15:59 -04:00
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2021-07-03 11:02:14 -04:00
|
|
|
[SymbolAsyncIterator]() {
|
2021-05-18 11:24:01 -04:00
|
|
|
// deno-lint-ignore no-this-alias
|
2021-04-08 18:34:15 -04:00
|
|
|
const httpConn = this;
|
|
|
|
return {
|
|
|
|
async next() {
|
|
|
|
const reqEvt = await httpConn.nextRequest();
|
2021-04-14 07:56:14 -04:00
|
|
|
// Change with caution, current form avoids a v8 deopt
|
2021-10-04 21:50:40 -04:00
|
|
|
return { value: reqEvt ?? undefined, done: reqEvt === null };
|
2021-04-08 18:34:15 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
function createRespondWith(
|
|
|
|
httpConn,
|
|
|
|
streamRid,
|
|
|
|
request,
|
|
|
|
remoteAddr,
|
|
|
|
localAddr,
|
|
|
|
) {
|
2021-04-13 07:41:47 -04:00
|
|
|
return async function respondWith(resp) {
|
2021-10-04 21:50:40 -04:00
|
|
|
try {
|
2022-03-22 21:53:59 -04:00
|
|
|
resp = await resp;
|
2022-02-01 12:06:11 -05:00
|
|
|
if (!(ObjectPrototypeIsPrototypeOf(ResponsePrototype, resp))) {
|
2021-10-04 21:50:40 -04:00
|
|
|
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
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
const innerResp = toInnerResponse(resp);
|
|
|
|
|
|
|
|
// 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.");
|
|
|
|
}
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
innerResp.body.streamOrStatic,
|
|
|
|
)
|
|
|
|
) {
|
2021-10-04 21:50:40 -04:00
|
|
|
if (
|
|
|
|
innerResp.body.length === null ||
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
BlobPrototype,
|
|
|
|
innerResp.body.source,
|
|
|
|
)
|
2021-10-04 21:50:40 -04:00
|
|
|
) {
|
|
|
|
respBody = innerResp.body.stream;
|
2021-05-20 21:11:53 -04:00
|
|
|
} else {
|
2021-10-04 21:50:40 -04:00
|
|
|
const reader = innerResp.body.stream.getReader();
|
|
|
|
const r1 = await reader.read();
|
|
|
|
if (r1.done) {
|
|
|
|
respBody = new Uint8Array(0);
|
|
|
|
} else {
|
|
|
|
respBody = r1.value;
|
|
|
|
const r2 = await reader.read();
|
|
|
|
if (!r2.done) throw new TypeError("Unreachable");
|
|
|
|
}
|
2021-05-20 21:11:53 -04:00
|
|
|
}
|
2021-10-04 21:50:40 -04:00
|
|
|
} else {
|
|
|
|
innerResp.body.streamOrStatic.consumed = true;
|
|
|
|
respBody = innerResp.body.streamOrStatic.body;
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-04 21:50:40 -04:00
|
|
|
respBody = new Uint8Array(0);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2021-11-12 14:21:58 -05:00
|
|
|
const isStreamingResponseBody = !(
|
2022-02-01 12:06:11 -05:00
|
|
|
typeof respBody === "string" ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, respBody)
|
2021-11-12 14:21:58 -05:00
|
|
|
);
|
2021-10-04 21:50:40 -04:00
|
|
|
try {
|
2021-11-12 14:21:58 -05:00
|
|
|
await core.opAsync(
|
|
|
|
"op_http_write_headers",
|
2022-04-04 05:49:35 -04:00
|
|
|
streamRid,
|
|
|
|
innerResp.status ?? 200,
|
|
|
|
innerResp.headerList,
|
2021-11-12 14:21:58 -05:00
|
|
|
isStreamingResponseBody ? null : respBody,
|
|
|
|
);
|
2021-10-04 21:50:40 -04:00
|
|
|
} catch (error) {
|
|
|
|
const connError = httpConn[connErrorSymbol];
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) &&
|
|
|
|
connError != null
|
|
|
|
) {
|
2021-10-04 21:50:40 -04:00
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
respBody !== null &&
|
|
|
|
ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, respBody)
|
|
|
|
) {
|
2021-10-04 21:50:40 -04:00
|
|
|
await respBody.cancel(error);
|
|
|
|
}
|
|
|
|
throw error;
|
2021-11-09 06:10:21 -05:00
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
if (isStreamingResponseBody) {
|
2022-12-20 03:46:45 -05:00
|
|
|
let success = false;
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
respBody === null ||
|
|
|
|
!ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, respBody)
|
|
|
|
) {
|
2021-04-23 06:34:04 -04:00
|
|
|
throw new TypeError("Unreachable");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2022-09-30 01:54:12 -04:00
|
|
|
const resourceBacking = getReadableStreamResourceBacking(respBody);
|
2022-05-18 06:43:22 -04:00
|
|
|
let reader;
|
2022-09-30 01:54:12 -04:00
|
|
|
if (resourceBacking) {
|
2022-04-22 06:49:08 -04:00
|
|
|
if (respBody.locked) {
|
|
|
|
throw new TypeError("ReadableStream is locked.");
|
2021-04-23 06:34:04 -04:00
|
|
|
}
|
2022-05-18 06:43:22 -04:00
|
|
|
reader = respBody.getReader(); // Aquire JS lock.
|
2021-04-23 06:34:04 -04:00
|
|
|
try {
|
2022-04-22 06:49:08 -04:00
|
|
|
await core.opAsync(
|
|
|
|
"op_http_write_resource",
|
|
|
|
streamRid,
|
2022-09-30 01:54:12 -04:00
|
|
|
resourceBacking.rid,
|
2022-04-22 06:49:08 -04:00
|
|
|
);
|
2022-09-30 01:54:12 -04:00
|
|
|
if (resourceBacking.autoClose) core.tryClose(resourceBacking.rid);
|
2022-04-22 06:49:08 -04:00
|
|
|
readableStreamClose(respBody); // Release JS lock.
|
2022-12-20 03:46:45 -05:00
|
|
|
success = true;
|
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-22 06:49:08 -04:00
|
|
|
} else {
|
2022-05-18 06:43:22 -04:00
|
|
|
reader = respBody.getReader();
|
2022-04-22 06:49:08 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await core.opAsync("op_http_write", streamRid, value);
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 03:46:45 -05:00
|
|
|
success = true;
|
2022-04-25 13:20:29 -04:00
|
|
|
}
|
2022-04-22 06:49:08 -04:00
|
|
|
|
2022-12-20 03:46:45 -05:00
|
|
|
if (success) {
|
|
|
|
try {
|
|
|
|
await core.opAsync("op_http_shutdown", streamRid);
|
|
|
|
} catch (error) {
|
|
|
|
await reader.cancel(error);
|
|
|
|
throw error;
|
|
|
|
}
|
2022-04-20 20:22:55 -04:00
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2022-03-16 09:54:18 -04: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);
|
2022-03-19 09:21:49 -04:00
|
|
|
} else if (res.connType === "unix") {
|
|
|
|
conn = new UnixConn(res.connRid, remoteAddr, localAddr);
|
2022-03-16 09:54:18 -04:00
|
|
|
} else {
|
|
|
|
throw new Error("unreachable");
|
|
|
|
}
|
|
|
|
|
|
|
|
deferred.resolve([conn, res.readBuf]);
|
|
|
|
}
|
2021-10-04 21:50:40 -04:00
|
|
|
const ws = resp[_ws];
|
|
|
|
if (ws) {
|
|
|
|
const wsRid = await core.opAsync(
|
|
|
|
"op_http_upgrade_websocket",
|
|
|
|
streamRid,
|
2021-07-08 07:33:01 -04:00
|
|
|
);
|
2021-10-04 21:50:40 -04:00
|
|
|
ws[_rid] = wsRid;
|
|
|
|
ws[_protocol] = resp.headers.get("sec-websocket-protocol");
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
httpConn.close();
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2022-05-23 07:21:11 -04:00
|
|
|
ws[_readyState] = WebSocket.OPEN;
|
|
|
|
const event = new Event("open");
|
|
|
|
ws.dispatchEvent(event);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2022-05-23 07:21:11 -04:00
|
|
|
ws[_eventLoop]();
|
|
|
|
if (ws[_idleTimeoutDuration]) {
|
|
|
|
ws.addEventListener(
|
|
|
|
"close",
|
|
|
|
() => clearTimeout(ws[_idleTimeoutTimeout]),
|
|
|
|
);
|
2021-10-04 21:50:40 -04:00
|
|
|
}
|
2022-05-23 07:21:11 -04:00
|
|
|
ws[_serverHandleIdleTimeout]();
|
2021-10-04 21:50:40 -04:00
|
|
|
}
|
|
|
|
} finally {
|
2022-03-23 03:35:34 -04:00
|
|
|
if (SetPrototypeDelete(httpConn.managedResources, streamRid)) {
|
2021-10-04 21:50:40 -04:00
|
|
|
core.close(streamRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-08 07:33:01 -04:00
|
|
|
const _ws = Symbol("[[associated_ws]]");
|
|
|
|
|
|
|
|
function upgradeWebSocket(request, options = {}) {
|
2021-08-24 18:55:32 -04:00
|
|
|
const upgrade = request.headers.get("upgrade");
|
2021-10-26 17:06:44 -04:00
|
|
|
const upgradeHasWebSocketOption = upgrade !== null &&
|
|
|
|
ArrayPrototypeSome(
|
|
|
|
StringPrototypeSplit(upgrade, /\s*,\s*/),
|
|
|
|
(option) => StringPrototypeToLowerCase(option) === "websocket",
|
|
|
|
);
|
|
|
|
if (!upgradeHasWebSocketOption) {
|
2021-07-08 07:33:01 -04:00
|
|
|
throw new TypeError(
|
2021-10-26 17:06:44 -04:00
|
|
|
"Invalid Header: 'upgrade' header must contain 'websocket'",
|
2021-07-08 07:33:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:14:03 -04:00
|
|
|
const connection = request.headers.get("connection");
|
|
|
|
const connectionHasUpgradeOption = connection !== null &&
|
|
|
|
ArrayPrototypeSome(
|
|
|
|
StringPrototypeSplit(connection, /\s*,\s*/),
|
|
|
|
(option) => StringPrototypeToLowerCase(option) === "upgrade",
|
|
|
|
);
|
|
|
|
if (!connectionHasUpgradeOption) {
|
2021-07-08 07:33:01 -04:00
|
|
|
throw new TypeError(
|
2021-10-26 17:06:44 -04:00
|
|
|
"Invalid Header: 'connection' header must contain 'Upgrade'",
|
2021-07-08 07:33:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const websocketKey = request.headers.get("sec-websocket-key");
|
|
|
|
if (websocketKey === null) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Invalid Header: 'sec-websocket-key' header must be set",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
const accept = ops.op_http_websocket_accept_header(websocketKey);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
|
|
|
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 {
|
|
|
|
throw new TypeError(
|
|
|
|
`Protocol '${options.protocol}' not in the request's protocol list (non negotiable)`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = fromInnerResponse(r, "immutable");
|
|
|
|
|
2021-07-28 07:13:14 -04:00
|
|
|
const socket = webidl.createBranded(WebSocket);
|
|
|
|
setEventTargetData(socket);
|
2021-08-09 04:45:59 -04:00
|
|
|
socket[_server] = true;
|
2021-07-28 07:13:14 -04:00
|
|
|
response[_ws] = socket;
|
2022-01-06 11:41:16 -05:00
|
|
|
socket[_idleTimeoutDuration] = options.idleTimeout ?? 120;
|
|
|
|
socket[_idleTimeoutTimeout] = null;
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-07-28 07:13:14 -04:00
|
|
|
return { response, socket };
|
2021-07-08 07:33:01 -04:00
|
|
|
}
|
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
function upgradeHttp(req) {
|
2022-08-18 08:05:02 -04:00
|
|
|
if (req[_flash]) {
|
2022-08-24 08:10:57 -04:00
|
|
|
throw new TypeError(
|
|
|
|
"Flash requests can not be upgraded with `upgradeHttp`. Use `upgradeHttpRaw` instead.",
|
|
|
|
);
|
2022-08-18 08:05:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-16 09:54:18 -04:00
|
|
|
req[_deferred] = new Deferred();
|
|
|
|
return req[_deferred].promise;
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
window.__bootstrap.http = {
|
2021-07-12 06:44:49 -04:00
|
|
|
HttpConn,
|
2021-07-08 07:33:01 -04:00
|
|
|
upgradeWebSocket,
|
2022-03-16 09:54:18 -04:00
|
|
|
upgradeHttp,
|
2022-08-18 08:05:02 -04:00
|
|
|
_ws,
|
2021-04-08 18:34:15 -04:00
|
|
|
};
|
|
|
|
})(this);
|