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,
|
|
|
|
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-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-04-08 18:34:15 -04:00
|
|
|
"op_http_request_next",
|
|
|
|
this.#rid,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
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-06-28 19:43:03 -04:00
|
|
|
if (error instanceof BadResource) {
|
2021-04-08 18:34:15 -04:00
|
|
|
return null;
|
2021-06-28 19:43:03 -04:00
|
|
|
} else if (error instanceof Interrupted) {
|
2021-04-08 18:34:15 -04:00
|
|
|
return null;
|
2021-07-03 11:02:14 -04:00
|
|
|
} else if (
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (nextRequest === null) return null;
|
|
|
|
|
|
|
|
const [
|
2021-07-08 07:33:01 -04:00
|
|
|
requestRid,
|
2021-04-08 18:34:15 -04:00
|
|
|
responseSenderRid,
|
|
|
|
method,
|
|
|
|
headersList,
|
|
|
|
url,
|
|
|
|
] = nextRequest;
|
|
|
|
|
|
|
|
/** @type {ReadableStream<Uint8Array> | undefined} */
|
2021-04-20 08:47:22 -04:00
|
|
|
let body = null;
|
2021-07-08 07:33:01 -04:00
|
|
|
if (typeof requestRid === "number") {
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeAdd(this.managedResources, requestRid);
|
|
|
|
body = createRequestBodyStream(this, requestRid);
|
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-08-23 10:15:59 -04:00
|
|
|
SetPrototypeAdd(this.managedResources, responseSenderRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
const respondWith = createRespondWith(
|
|
|
|
this,
|
|
|
|
responseSenderRid,
|
|
|
|
requestRid,
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
return { request, respondWith };
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {void} */
|
|
|
|
close() {
|
2021-08-23 10:15:59 -04:00
|
|
|
for (const rid of SetPrototypeValues(this.managedResources)) {
|
2021-09-10 20:54:37 -04:00
|
|
|
core.tryClose(rid);
|
2021-08-23 10:15:59 -04:00
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
core.close(this.#rid);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return { value: reqEvt, done: reqEvt === null };
|
2021-04-08 18:34:15 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readRequest(requestRid, zeroCopyBuf) {
|
2021-07-03 11:02:14 -04:00
|
|
|
return core.opAsync(
|
2021-04-08 18:34:15 -04:00
|
|
|
"op_http_request_read",
|
|
|
|
requestRid,
|
|
|
|
zeroCopyBuf,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-08 07:33:01 -04:00
|
|
|
function createRespondWith(httpConn, responseSenderRid, requestRid) {
|
2021-04-13 07:41:47 -04:00
|
|
|
return async function respondWith(resp) {
|
2021-04-08 18:34:15 -04:00
|
|
|
if (resp instanceof Promise) {
|
|
|
|
resp = await resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(resp instanceof Response)) {
|
|
|
|
throw new TypeError(
|
|
|
|
"First argument to respondWith must be a Response or a promise resolving to a Response.",
|
|
|
|
);
|
|
|
|
}
|
2021-04-20 08:47:22 -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) {
|
2021-07-05 09:34:37 -04:00
|
|
|
if (
|
|
|
|
innerResp.body.length === null ||
|
|
|
|
innerResp.body.source instanceof Blob
|
|
|
|
) {
|
2021-04-20 08:47:22 -04:00
|
|
|
respBody = innerResp.body.stream;
|
|
|
|
} else {
|
|
|
|
const reader = innerResp.body.stream.getReader();
|
|
|
|
const r1 = await reader.read();
|
2021-05-20 21:11:53 -04:00
|
|
|
if (r1.done) {
|
|
|
|
respBody = new Uint8Array(0);
|
|
|
|
} else {
|
|
|
|
respBody = r1.value;
|
|
|
|
const r2 = await reader.read();
|
|
|
|
if (!r2.done) throw new TypeError("Unreachable");
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
innerResp.body.streamOrStatic.consumed = true;
|
|
|
|
respBody = innerResp.body.streamOrStatic.body;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
} else {
|
2021-04-20 08:47:22 -04:00
|
|
|
respBody = new Uint8Array(0);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeDelete(httpConn.managedResources, responseSenderRid);
|
2021-04-23 06:34:04 -04:00
|
|
|
let responseBodyRid;
|
|
|
|
try {
|
2021-07-03 11:02:14 -04:00
|
|
|
responseBodyRid = await core.opAsync("op_http_response", [
|
2021-04-23 06:34:04 -04:00
|
|
|
responseSenderRid,
|
|
|
|
innerResp.status ?? 200,
|
|
|
|
innerResp.headerList,
|
|
|
|
], respBody instanceof Uint8Array ? respBody : null);
|
|
|
|
} 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
|
|
|
if (respBody !== null && respBody instanceof ReadableStream) {
|
|
|
|
await respBody.cancel(error);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
// If `respond` returns a responseBodyRid, we should stream the body
|
|
|
|
// to that resource.
|
2021-04-20 08:47:22 -04:00
|
|
|
if (responseBodyRid !== null) {
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeAdd(httpConn.managedResources, responseBodyRid);
|
2021-04-23 06:34:04 -04:00
|
|
|
try {
|
|
|
|
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-07-03 11:02:14 -04:00
|
|
|
await core.opAsync(
|
2021-04-23 06:34:04 -04:00
|
|
|
"op_http_response_write",
|
|
|
|
responseBodyRid,
|
|
|
|
value,
|
|
|
|
);
|
|
|
|
} 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-04-23 06:34:04 -04:00
|
|
|
} finally {
|
|
|
|
// Once all chunks are sent, and the request body is closed, we can
|
|
|
|
// close the response body.
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeDelete(httpConn.managedResources, responseBodyRid);
|
2021-05-19 08:39:52 -04:00
|
|
|
try {
|
2021-07-03 11:02:14 -04:00
|
|
|
await core.opAsync("op_http_response_close", responseBodyRid);
|
2021-05-19 08:39:52 -04:00
|
|
|
} catch { /* pass */ }
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-08 07:33:01 -04:00
|
|
|
|
|
|
|
const ws = resp[_ws];
|
|
|
|
if (ws) {
|
|
|
|
if (typeof requestRid !== "number") {
|
|
|
|
throw new TypeError(
|
|
|
|
"This request can not be upgraded to a websocket connection.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const wsRid = await core.opAsync(
|
|
|
|
"op_http_upgrade_websocket",
|
|
|
|
requestRid,
|
|
|
|
);
|
|
|
|
ws[_rid] = wsRid;
|
|
|
|
ws[_protocol] = resp.headers.get("sec-websocket-protocol");
|
|
|
|
|
|
|
|
if (ws[_readyState] === WebSocket.CLOSING) {
|
|
|
|
await core.opAsync("op_ws_close", { rid: wsRid });
|
|
|
|
|
|
|
|
ws[_readyState] = WebSocket.CLOSED;
|
|
|
|
|
|
|
|
const errEvent = new ErrorEvent("error");
|
|
|
|
ws.dispatchEvent(errEvent);
|
|
|
|
|
|
|
|
const event = new CloseEvent("close");
|
|
|
|
ws.dispatchEvent(event);
|
|
|
|
|
2021-09-10 20:54:37 -04:00
|
|
|
core.tryClose(wsRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
} else {
|
|
|
|
ws[_readyState] = WebSocket.OPEN;
|
|
|
|
const event = new Event("open");
|
|
|
|
ws.dispatchEvent(event);
|
|
|
|
|
|
|
|
ws[_eventLoop]();
|
|
|
|
}
|
2021-09-10 18:47:09 -04:00
|
|
|
} else if (typeof requestRid === "number") {
|
2021-09-08 04:12:23 -04:00
|
|
|
// Try to close "request" resource. It might have been already consumed,
|
2021-09-10 18:47:09 -04:00
|
|
|
// but if it hasn't been we need to close it here to avoid resource
|
|
|
|
// leak.
|
|
|
|
SetPrototypeDelete(httpConn.managedResources, requestRid);
|
|
|
|
core.tryClose(requestRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-23 10:15:59 -04:00
|
|
|
function createRequestBodyStream(httpConn, requestRid) {
|
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);
|
|
|
|
const read = await readRequest(
|
2021-07-08 07:33:01 -04:00
|
|
|
requestRid,
|
2021-04-08 18:34:15 -04:00
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
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();
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeDelete(httpConn.managedResources, requestRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
core.close(requestRid);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// There was an error while reading a chunk of the body, so we
|
|
|
|
// error.
|
|
|
|
controller.error(err);
|
|
|
|
controller.close();
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeDelete(httpConn.managedResources, requestRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
core.close(requestRid);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cancel() {
|
2021-08-23 10:15:59 -04:00
|
|
|
SetPrototypeDelete(httpConn.managedResources, requestRid);
|
2021-07-08 07:33:01 -04:00
|
|
|
core.close(requestRid);
|
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");
|
|
|
|
if (!upgrade || StringPrototypeToLowerCase(upgrade) !== "websocket") {
|
2021-07-08 07:33:01 -04:00
|
|
|
throw new TypeError(
|
|
|
|
"Invalid Header: 'upgrade' header must be 'websocket'",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
"Invalid Header: 'connection' header must be 'Upgrade'",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|