2021-04-08 18:34:15 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
"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;
|
2021-07-08 07:33:01 -04:00
|
|
|
const { setEventTargetData } = window.__bootstrap.eventTarget;
|
|
|
|
const {
|
|
|
|
Response,
|
|
|
|
fromInnerRequest,
|
|
|
|
toInnerResponse,
|
|
|
|
newInnerRequest,
|
|
|
|
newInnerResponse,
|
|
|
|
fromInnerResponse,
|
|
|
|
} = window.__bootstrap.fetch;
|
2021-04-08 18:34:15 -04:00
|
|
|
const core = window.Deno.core;
|
2021-06-28 19:43:03 -04:00
|
|
|
const { BadResource, Interrupted } = core;
|
2021-04-08 18:34:15 -04:00
|
|
|
const { ReadableStream } = window.__bootstrap.streams;
|
2021-06-25 18:01:25 -04:00
|
|
|
const abortSignal = window.__bootstrap.abortSignal;
|
2021-08-09 04:45:59 -04:00
|
|
|
const { WebSocket, _rid, _readyState, _eventLoop, _protocol, _server } =
|
2021-07-08 07:33:01 -04:00
|
|
|
window.__bootstrap.webSocket;
|
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,
|
2021-07-03 11:02:14 -04:00
|
|
|
Promise,
|
2021-08-23 10:15:59 -04:00
|
|
|
Set,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeDelete,
|
2021-10-04 21:50:40 -04:00
|
|
|
SetPrototypeHas,
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeValues,
|
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,
|
|
|
|
TypedArrayPrototypeSubarray,
|
2021-07-08 07:33:01 -04:00
|
|
|
TypeError,
|
|
|
|
Uint8Array,
|
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");
|
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
class HttpConn {
|
|
|
|
#rid = 0;
|
2021-10-04 21:50:40 -04:00
|
|
|
#closed = false;
|
|
|
|
|
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
|
|
|
|
|
|
|
constructor(rid) {
|
|
|
|
this.#rid = rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {number} */
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {Promise<ResponseEvent | null>} */
|
|
|
|
async nextRequest() {
|
|
|
|
let nextRequest;
|
|
|
|
try {
|
2021-07-03 11:02:14 -04:00
|
|
|
nextRequest = await core.opAsync(
|
2021-10-04 21:50:40 -04:00
|
|
|
"op_http_accept",
|
2021-04-08 18:34:15 -04:00
|
|
|
this.#rid,
|
|
|
|
);
|
|
|
|
} 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 (
|
|
|
|
error instanceof BadResource ||
|
|
|
|
error instanceof Interrupted ||
|
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
|
|
|
|
|
|
|
const [
|
2021-10-04 21:50:40 -04:00
|
|
|
streamRid,
|
2021-04-08 18:34:15 -04:00
|
|
|
method,
|
|
|
|
headersList,
|
|
|
|
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") {
|
|
|
|
body = createRequestBodyStream(streamRid);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
const innerRequest = newInnerRequest(
|
2021-04-08 18:34:15 -04:00
|
|
|
method,
|
2021-04-20 08:47:22 -04:00
|
|
|
url,
|
|
|
|
headersList,
|
|
|
|
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();
|
|
|
|
const request = fromInnerRequest(innerRequest, signal, "immutable");
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
const respondWith = createRespondWith(this, streamRid);
|
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);
|
|
|
|
for (const rid of SetPrototypeValues(this.managedResources)) {
|
|
|
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
function readRequest(streamRid, buf) {
|
|
|
|
return core.opAsync("op_http_read", streamRid, buf);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
function createRespondWith(httpConn, streamRid) {
|
2021-04-13 07:41:47 -04:00
|
|
|
return async function respondWith(resp) {
|
2021-10-04 21:50:40 -04:00
|
|
|
try {
|
|
|
|
if (resp instanceof Promise) {
|
|
|
|
resp = await resp;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
if (!(resp instanceof Response)) {
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
if (innerResp.body.streamOrStatic instanceof ReadableStream) {
|
|
|
|
if (
|
|
|
|
innerResp.body.length === null ||
|
|
|
|
innerResp.body.source instanceof Blob
|
|
|
|
) {
|
|
|
|
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-10-04 21:50:40 -04:00
|
|
|
const isStreamingResponseBody =
|
|
|
|
!(typeof respBody === "string" || respBody instanceof Uint8Array);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
try {
|
|
|
|
await core.opAsync("op_http_write_headers", [
|
|
|
|
streamRid,
|
2021-10-26 16:00:01 -04:00
|
|
|
innerResp.status ?? 200,
|
|
|
|
innerResp.headerList,
|
2021-10-04 21:50:40 -04:00
|
|
|
], isStreamingResponseBody ? null : respBody);
|
|
|
|
} catch (error) {
|
|
|
|
const connError = httpConn[connErrorSymbol];
|
|
|
|
if (error instanceof BadResource && connError != null) {
|
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
|
|
|
if (respBody !== null && respBody instanceof ReadableStream) {
|
|
|
|
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) {
|
2021-04-23 06:34:04 -04:00
|
|
|
if (respBody === null || !(respBody instanceof ReadableStream)) {
|
|
|
|
throw new TypeError("Unreachable");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2021-04-23 06:34:04 -04:00
|
|
|
const reader = respBody.getReader();
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
if (!(value instanceof Uint8Array)) {
|
|
|
|
await reader.cancel(new TypeError("Value not a Uint8Array"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
2021-10-04 21:50:40 -04:00
|
|
|
await core.opAsync("op_http_write", streamRid, value);
|
2021-04-23 06:34:04 -04:00
|
|
|
} catch (error) {
|
2021-05-19 08:39:52 -04:00
|
|
|
const connError = httpConn[connErrorSymbol];
|
2021-06-28 19:43:03 -04:00
|
|
|
if (error instanceof BadResource && connError != null) {
|
2021-05-19 08:39:52 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2021-05-19 08:39:52 -04:00
|
|
|
try {
|
2021-10-04 21:50:40 -04:00
|
|
|
await core.opAsync("op_http_shutdown", streamRid);
|
|
|
|
} catch (error) {
|
|
|
|
await reader.cancel(error);
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
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
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
if (ws[_readyState] === WebSocket.CLOSING) {
|
|
|
|
await core.opAsync("op_ws_close", { rid: wsRid });
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
ws[_readyState] = WebSocket.CLOSED;
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
const errEvent = new ErrorEvent("error");
|
|
|
|
ws.dispatchEvent(errEvent);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
const event = new CloseEvent("close");
|
|
|
|
ws.dispatchEvent(event);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
core.tryClose(wsRid);
|
|
|
|
} else {
|
|
|
|
ws[_readyState] = WebSocket.OPEN;
|
|
|
|
const event = new Event("open");
|
|
|
|
ws.dispatchEvent(event);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
ws[_eventLoop]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (SetPrototypeHas(httpConn.managedResources, streamRid)) {
|
|
|
|
SetPrototypeDelete(httpConn.managedResources, streamRid);
|
|
|
|
core.close(streamRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
function createRequestBodyStream(streamRid) {
|
2021-04-08 18:34:15 -04:00
|
|
|
return new ReadableStream({
|
|
|
|
type: "bytes",
|
|
|
|
async pull(controller) {
|
|
|
|
try {
|
|
|
|
// This is the largest possible size for a single packet on a TLS
|
|
|
|
// stream.
|
|
|
|
const chunk = new Uint8Array(16 * 1024 + 256);
|
2021-10-04 21:50:40 -04:00
|
|
|
const read = await readRequest(streamRid, chunk);
|
2021-04-08 18:34:15 -04:00
|
|
|
if (read > 0) {
|
|
|
|
// We read some data. Enqueue it onto the stream.
|
2021-07-03 11:02:14 -04:00
|
|
|
controller.enqueue(TypedArrayPrototypeSubarray(chunk, 0, read));
|
2021-04-08 18:34:15 -04:00
|
|
|
} else {
|
|
|
|
// We have reached the end of the body, so we close the stream.
|
|
|
|
controller.close();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// There was an error while reading a chunk of the body, so we
|
|
|
|
// error.
|
|
|
|
controller.error(err);
|
|
|
|
controller.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const accept = core.opSync("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 {
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
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,
|
2021-04-08 18:34:15 -04:00
|
|
|
};
|
|
|
|
})(this);
|