summaryrefslogtreecommitdiff
path: root/http
diff options
context:
space:
mode:
authorAurélien Bertron <aurelienbertron@gmail.com>2019-05-23 04:33:17 +0200
committerBert Belder <bertbelder@gmail.com>2019-05-22 19:33:17 -0700
commita4346a3ac947f3599a15aaa71666705066407e3a (patch)
treefdd115d963023388df890b4fe7e87a329a6d2b62 /http
parent3cfc1244d8747a869fb370de2d057523852fbea9 (diff)
http: send an empty response body if none is provided (denoland/deno_std#429)
Fixes: denoland/deno_std#402 Original: https://github.com/denoland/deno_std/commit/e00e3fe33a4e57e8bebcf2b7cdd4f501674450d2
Diffstat (limited to 'http')
-rw-r--r--http/server.ts29
-rw-r--r--http/server_test.ts9
2 files changed, 22 insertions, 16 deletions
diff --git a/http/server.ts b/http/server.ts
index 809cf39ff..b49e23b15 100644
--- a/http/server.ts
+++ b/http/server.ts
@@ -66,6 +66,9 @@ export async function writeResponse(w: Writer, r: Response): Promise<void> {
if (!statusText) {
throw Error("bad status code");
}
+ if (!r.body) {
+ r.body = new Uint8Array();
+ }
let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`;
@@ -79,22 +82,18 @@ export async function writeResponse(w: Writer, r: Response): Promise<void> {
out += "\r\n";
const header = new TextEncoder().encode(out);
- let n = await writer.write(header);
- assert(header.byteLength == n);
+ const n = await writer.write(header);
+ assert(n === header.byteLength);
- if (r.body) {
- if (r.body instanceof Uint8Array) {
- n = await writer.write(r.body);
- assert(r.body.byteLength == n);
- } else {
- if (r.headers.has("content-length")) {
- const bodyLength = parseInt(r.headers.get("content-length"));
- const n = await copy(writer, r.body);
- assert(n == bodyLength);
- } else {
- await writeChunkedBody(writer, r.body);
- }
- }
+ if (r.body instanceof Uint8Array) {
+ const n = await writer.write(r.body);
+ assert(n === r.body.byteLength);
+ } else if (r.headers.has("content-length")) {
+ const bodyLength = parseInt(r.headers.get("content-length"));
+ const n = await copy(writer, r.body);
+ assert(n === bodyLength);
+ } else {
+ await writeChunkedBody(writer, r.body);
}
await writer.flush();
}
diff --git a/http/server_test.ts b/http/server_test.ts
index e3baebd53..e17b29a4d 100644
--- a/http/server_test.ts
+++ b/http/server_test.ts
@@ -31,7 +31,14 @@ const responseTests: ResponseTest[] = [
// Default response
{
response: {},
- raw: "HTTP/1.1 200 OK\r\n" + "\r\n"
+ raw: "HTTP/1.1 200 OK\r\n" + "content-length: 0" + "\r\n\r\n"
+ },
+ // Empty body with status
+ {
+ response: {
+ status: 404
+ },
+ raw: "HTTP/1.1 404 Not Found\r\n" + "content-length: 0" + "\r\n\r\n"
},
// HTTP/1.1, chunked coding; empty trailer; close
{