Skip to content

Commit

Permalink
fix(runtime/js/http): Correctly parse user response headers (denoland…
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Apr 12, 2021
1 parent 9d53dab commit a205046
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
3 changes: 2 additions & 1 deletion cli/tests/unit/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ unitTest({ perms: { net: true } }, async function httpServerBasic() {
const httpConn = Deno.serveHttp(conn);
for await (const { request, respondWith } of httpConn) {
assertEquals(await request.text(), "");
respondWith(new Response("Hello World"));
respondWith(new Response("Hello World", { headers: { "foo": "bar" } }));
}
break;
}
Expand All @@ -24,6 +24,7 @@ unitTest({ perms: { net: true } }, async function httpServerBasic() {
});
const text = await resp.text();
assertEquals(text, "Hello World");
assertEquals(resp.headers.get("foo"), "bar");
await promise;
});

Expand Down
31 changes: 12 additions & 19 deletions runtime/js/40_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
const core = window.Deno.core;
const { ReadableStream } = window.__bootstrap.streams;

function flatEntries(obj) {
const entries = [];
for (const key in obj) {
entries.push(key);
entries.push(obj[key]);
}
return entries;
}

function serveHttp(conn) {
const rid = Deno.core.jsonOpSync("op_http_start", conn.rid);
return new HttpConn(rid);
Expand Down Expand Up @@ -104,12 +95,14 @@
);
}

function respond(responseSenderRid, resp, zeroCopyBuf) {
return Deno.core.jsonOpSync("op_http_response", [
responseSenderRid,
resp.status ?? 200,
flatEntries(resp.headers ?? {}),
], zeroCopyBuf);
/** IMPORTANT: Equivalent to `Array.from(headers).flat()` but more performant.
* Please preserve. */
function flattenHeaders(headers) {
const array = [];
for (const pair of headers) {
array.push(pair[0], pair[1]);
}
return array;
}

function createRespondWith(responseSenderRid, connRid) {
Expand All @@ -136,11 +129,11 @@
zeroCopyBuf = null;
}

const responseBodyRid = respond(
const responseBodyRid = Deno.core.jsonOpSync("op_http_response", [
responseSenderRid,
resp,
zeroCopyBuf,
);
resp.status ?? 200,
flattenHeaders(resp.headers),
], zeroCopyBuf);

// If `respond` returns a responseBodyRid, we should stream the body
// to that resource.
Expand Down

0 comments on commit a205046

Please sign in to comment.