Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set http response body as empty by default #429

Merged
merged 3 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand All @@ -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();
}
Expand Down
9 changes: 8 additions & 1 deletion http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down