mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
http: send an empty response body if none is provided (denoland/deno_std#429)
Fixes: denoland/deno_std#402
Original: e00e3fe33a
This commit is contained in:
parent
3cfc1244d8
commit
a4346a3ac9
2 changed files with 22 additions and 16 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue