Skip to content

Commit

Permalink
fix(ext/http): skip auto-compression if content-encoding present (den…
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO authored May 17, 2022
1 parent 1fa75f7 commit 037e46b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cli/tests/unit/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,64 @@ Deno.test({
},
});

Deno.test({
name: "http server custom content-encoding is left untouched",
permissions: { net: true, run: true },
async fn() {
const hostname = "localhost";
const port = 4501;
let contentLength: string;

async function server() {
const listener = Deno.listen({ hostname, port });
const tcpConn = await listener.accept();
const httpConn = Deno.serveHttp(tcpConn);
const e = await httpConn.nextRequest();
assert(e);
const { request, respondWith } = e;
assertEquals(request.headers.get("Accept-Encoding"), "deflate, gzip");
const body = new Uint8Array([3, 1, 4, 1]);
contentLength = String(body.length);
const response = new Response(
body,
{
headers: {
"content-length": contentLength,
"content-encoding": "arbitrary",
},
},
);
await respondWith(response);
httpConn.close();
listener.close();
}

async function client() {
const url = `https://${hostname}:${port}/`;
const cmd = [
"curl",
"-i",
"--request",
"GET",
"--url",
url,
// "--compressed", // Windows curl does not support --compressed
"--header",
"Accept-Encoding: deflate, gzip",
];
const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
const status = await proc.status();
assert(status.success);
const output = decoder.decode(await proc.output());
assert(output.includes("vary: Accept-Encoding\r\n"));
assert(output.includes("content-encoding: arbitrary\r\n"));
proc.close();
}

await Promise.all([server(), client()]);
},
});

Deno.test("upgradeHttp tcp", async () => {
async function client() {
const tcpConn = await Deno.connect({ port: 4501 });
Expand Down
3 changes: 3 additions & 0 deletions ext/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,11 @@ fn should_compress(headers: &hyper::HeaderMap) -> bool {
// indicates the contents of the body were negotiated based directly
// with the user code and we can't compress the response
let content_range = headers.contains_key(hyper::header::CONTENT_RANGE);
// assume body is already compressed if Content-Encoding header present, thus avoid recompressing
let is_precompressed = headers.contains_key(hyper::header::CONTENT_ENCODING);

!content_range
&& !is_precompressed
&& !cache_control_no_transform(headers).unwrap_or_default()
&& headers
.get(hyper::header::CONTENT_TYPE)
Expand Down

0 comments on commit 037e46b

Please sign in to comment.