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 1 commit
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
Next Next commit
Set http response body as empty by default
Fix #402
  • Loading branch information
Nautigsam authored and piscisaureus committed May 23, 2019
commit cad11423dd327f6aef2f26029da2c21b20e79501
23 changes: 12 additions & 11 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 @@ -82,18 +85,16 @@ export async function writeResponse(w: Writer, r: Response): Promise<void> {
let n = await writer.write(header);
assert(header.byteLength == n);

if (r.body) {
if (r.body instanceof Uint8Array) {
n = await writer.write(r.body);
assert(r.body.byteLength == n);
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 {
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 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
1 change: 1 addition & 0 deletions io/bufio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ export class BufWriter implements Writer {
return err;
}
this.n = 0;
return null;
}

/** Returns how many bytes are unused in the buffer. */
Expand Down