diff options
-rwxr-xr-x | std/http/file_server.ts | 2 | ||||
-rw-r--r-- | std/http/io.ts | 33 | ||||
-rw-r--r-- | std/http/io_test.ts | 13 |
3 files changed, 23 insertions, 25 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts index d847642d9..cc92e0d47 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -11,7 +11,6 @@ import { posix, extname } from "../path/mod.ts"; import { listenAndServe, ServerRequest, Response } from "./server.ts"; import { parse } from "../flags/mod.ts"; import { assert } from "../testing/asserts.ts"; -import { setContentLength } from "./io.ts"; interface EntryInfo { mode: string; @@ -173,7 +172,6 @@ async function serveDir( body: page, headers, }; - setContentLength(res); return res; } diff --git a/std/http/io.ts b/std/http/io.ts index fbce44342..631adafd0 100644 --- a/std/http/io.ts +++ b/std/http/io.ts @@ -214,24 +214,6 @@ export async function writeTrailers( await writer.flush(); } -export function setContentLength(r: Response): void { - if (!r.headers) { - r.headers = new Headers(); - } - - if (r.body) { - if (!r.headers.has("content-length")) { - // typeof r.body === "string" handled in writeResponse. - if (r.body instanceof Uint8Array) { - const bodyLength = r.body.byteLength; - r.headers.set("content-length", bodyLength.toString()); - } else { - r.headers.set("transfer-encoding", "chunked"); - } - } - } -} - export async function writeResponse( w: Deno.Writer, r: Response @@ -253,14 +235,21 @@ export async function writeResponse( let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`; - setContentLength(r); - assert(r.headers != null); - const headers = r.headers; + const headers = r.headers ?? new Headers(); + + if (r.body && !headers.get("content-length")) { + if (r.body instanceof Uint8Array) { + out += `content-length: ${r.body.byteLength}\r\n`; + } else if (!headers.get("transfer-encoding")) { + out += "transfer-encoding: chunked\r\n"; + } + } for (const [key, value] of headers) { out += `${key}: ${value}\r\n`; } - out += "\r\n"; + + out += `\r\n`; const header = encoder.encode(out); const n = await writer.write(header); diff --git a/std/http/io_test.ts b/std/http/io_test.ts index fd41b474e..c0f57a1b7 100644 --- a/std/http/io_test.ts +++ b/std/http/io_test.ts @@ -19,7 +19,7 @@ import { chunkedBodyReader } from "./io.ts"; import { ServerRequest, Response } from "./server.ts"; import { StringReader } from "../io/readers.ts"; import { mockConn } from "./mock.ts"; -const { Buffer, test } = Deno; +const { Buffer, test, readAll } = Deno; test("bodyReader", async () => { const text = "Hello, Deno"; @@ -357,6 +357,17 @@ test("writeResponse with trailer", async () => { assertEquals(ret, exp); }); +test("writeResponseShouldNotModifyOriginHeaders", async () => { + const headers = new Headers(); + const buf = new Deno.Buffer(); + + await writeResponse(buf, { body: "foo", headers }); + assert(decode(await readAll(buf)).includes("content-length: 3")); + + await writeResponse(buf, { body: "hello", headers }); + assert(decode(await readAll(buf)).includes("content-length: 5")); +}); + test("readRequestError", async function (): Promise<void> { const input = `GET / HTTP/1.1 malformedHeader |