summaryrefslogtreecommitdiff
path: root/ext/http/01_http.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-04-08 22:02:49 +0100
committerGitHub <noreply@github.com>2024-04-08 15:02:49 -0600
commitd3b63bb315c7573974d8bd79dcfb6849cb29cf4e (patch)
tree6f216dc558642f06bd9439e1b696118eab4622b3 /ext/http/01_http.js
parentcb12a9350332860971387e3a1fb40dc77fa992d3 (diff)
fix(ext/http): Make `Deno.serveHttp()` work when proxying (#23269)
Closes https://github.com/denoland/deno/issues/21900
Diffstat (limited to 'ext/http/01_http.js')
-rw-r--r--ext/http/01_http.js38
1 files changed, 22 insertions, 16 deletions
diff --git a/ext/http/01_http.js b/ext/http/01_http.js
index 8bed444ca..a312cf60e 100644
--- a/ext/http/01_http.js
+++ b/ext/http/01_http.js
@@ -137,8 +137,10 @@ class HttpConn {
return null;
}
- const { 0: streamRid, 1: method, 2: url } = nextRequest;
- SetPrototypeAdd(this.#managedResources, streamRid);
+ const { 0: readStreamRid, 1: writeStreamRid, 2: method, 3: url } =
+ nextRequest;
+ SetPrototypeAdd(this.#managedResources, readStreamRid);
+ SetPrototypeAdd(this.#managedResources, writeStreamRid);
/** @type {ReadableStream<Uint8Array> | undefined} */
let body = null;
@@ -146,17 +148,16 @@ class HttpConn {
// 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);
+ body = readableStreamForRid(readStreamRid, false);
}
const innerRequest = newInnerRequest(
method,
url,
- () => op_http_headers(streamRid),
+ () => op_http_headers(readStreamRid),
body !== null ? new InnerBody(body) : null,
false,
);
- innerRequest[streamRid] = streamRid;
const abortController = new AbortController();
const request = fromInnerRequest(
innerRequest,
@@ -167,7 +168,8 @@ class HttpConn {
const respondWith = createRespondWith(
this,
- streamRid,
+ readStreamRid,
+ writeStreamRid,
abortController,
);
@@ -178,10 +180,10 @@ class HttpConn {
close() {
if (!this.#closed) {
this.#closed = true;
- core.close(this.#rid);
+ core.tryClose(this.#rid);
for (const rid of new SafeSetIterator(this.#managedResources)) {
SetPrototypeDelete(this.#managedResources, rid);
- core.close(rid);
+ core.tryClose(rid);
}
}
}
@@ -209,7 +211,8 @@ class HttpConn {
function createRespondWith(
httpConn,
- streamRid,
+ readStreamRid,
+ writeStreamRid,
abortController,
) {
return async function respondWith(resp) {
@@ -270,7 +273,7 @@ function createRespondWith(
);
try {
await op_http_write_headers(
- streamRid,
+ writeStreamRid,
innerResp.status ?? 200,
innerResp.headerList,
isStreamingResponseBody ? null : respBody,
@@ -310,7 +313,7 @@ function createRespondWith(
reader = respBody.getReader(); // Acquire JS lock.
try {
await op_http_write_resource(
- streamRid,
+ writeStreamRid,
resourceBacking.rid,
);
if (resourceBacking.autoClose) core.tryClose(resourceBacking.rid);
@@ -340,7 +343,7 @@ function createRespondWith(
break;
}
try {
- await op_http_write(streamRid, value);
+ await op_http_write(writeStreamRid, value);
} catch (error) {
const connError = httpConn[connErrorSymbol];
if (
@@ -359,7 +362,7 @@ function createRespondWith(
if (success) {
try {
- await op_http_shutdown(streamRid);
+ await op_http_shutdown(writeStreamRid);
} catch (error) {
await reader.cancel(error);
throw error;
@@ -370,7 +373,7 @@ function createRespondWith(
const ws = resp[_ws];
if (ws) {
const wsRid = await op_http_upgrade_websocket(
- streamRid,
+ readStreamRid,
);
ws[_rid] = wsRid;
ws[_protocol] = resp.headers.get("sec-websocket-protocol");
@@ -395,8 +398,11 @@ function createRespondWith(
abortController.abort(error);
throw error;
} finally {
- if (deleteManagedResource(httpConn, streamRid)) {
- core.close(streamRid);
+ if (deleteManagedResource(httpConn, readStreamRid)) {
+ core.tryClose(readStreamRid);
+ }
+ if (deleteManagedResource(httpConn, writeStreamRid)) {
+ core.tryClose(writeStreamRid);
}
}
};