Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhadeep Das committed May 23, 2019
2 parents a6d43a2 + e00e3fe commit c9b124e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 22 deletions.
2 changes: 1 addition & 1 deletion http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async function serveDir(
listEntry.push(
createDirEntryDisplay(
info.name,
fn,
fn.replace(currentDir, ""),
info.isFile() ? info.len : null,
mode,
info.isDirectory()
Expand Down
12 changes: 12 additions & 0 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ test(async function serveDirectory(): Promise<void> {
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
assert(page.includes("azure-pipelines.yml"));

// `Deno.FileInfo` is not completely compatible with Windows yet
// TODO: `mode` should work correctly in the future. Correct this test case accordingly.
Deno.platform.os !== "win" &&
assert(/<td class="mode">\([a-zA-Z-]{10}\)<\/td>/.test(page));
Deno.platform.os === "win" &&
assert(/<td class="mode">\(unknown mode\)<\/td>/.test(page));
assert(
page.includes(
`<td><a href="/azure-pipelines.yml">azure-pipelines.yml</a></td>`
)
);
} finally {
killFileServer();
}
Expand Down
43 changes: 25 additions & 18 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 Expand Up @@ -197,7 +196,7 @@ export class ServerRequest {
}
}

async function readRequest(
export async function readRequest(
bufr: BufReader
): Promise<[ServerRequest, BufState]> {
const req = new ServerRequest();
Expand Down Expand Up @@ -235,7 +234,11 @@ export class Server implements AsyncIterable<ServerRequest> {
let req: ServerRequest;

while (!this.closing) {
[req, bufStateErr] = await readRequest(bufr);
try {
[req, bufStateErr] = await readRequest(bufr);
} catch (err) {
bufStateErr = err;
}
if (bufStateErr) break;
req.w = w;
yield req;
Expand All @@ -247,7 +250,11 @@ export class Server implements AsyncIterable<ServerRequest> {
if (bufStateErr === "EOF") {
// The connection was gracefully closed.
} else if (bufStateErr instanceof Error) {
// TODO(ry): send something back like a HTTP 500 status.
// An error was thrown while parsing request headers.
await writeResponse(req.w, {
status: 400,
body: new TextEncoder().encode(`${bufStateErr.message}\r\n\r\n`)
});
} else if (this.closing) {
// There are more requests incoming but the server is closing.
// TODO(ry): send a back a HTTP 503 Service Unavailable status.
Expand Down
97 changes: 94 additions & 3 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

const { Buffer } = Deno;
import { test, runIfMain } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { Response, ServerRequest, writeResponse } from "./server.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import {
Response,
ServerRequest,
writeResponse,
readRequest
} from "./server.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { StringReader } from "../io/readers.ts";

Expand All @@ -26,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 Expand Up @@ -283,4 +295,83 @@ test(async function writeStringReaderResponse(): Promise<void> {
assertEquals(decoder.decode(line), "0");
});

test(async function readRequestError(): Promise<void> {
let input = `GET / HTTP/1.1
malformedHeader
`;
const reader = new BufReader(new StringReader(input));
let err;
try {
await readRequest(reader);
} catch (e) {
err = e;
}
assert(err instanceof Error);
assertEquals(err.message, "malformed MIME header line: malformedHeader");
});

// Ported from Go
// https://github.com/golang/go/blob/go1.12.5/src/net/http/request_test.go#L377-L443
// TODO(zekth) fix tests
test(async function testReadRequestError(): Promise<void> {
const testCases = {
0: {
in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n",
headers: [{ key: "header", value: "foo" }],
err: null
},
1: { in: "GET / HTTP/1.1\r\nheader:foo\r\n", err: "EOF", headers: [] },
2: { in: "", err: "EOF", headers: [] },
// 3: {
// in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
// err: "http: method cannot contain a Content-Length"
// },
4: {
in: "HEAD / HTTP/1.1\r\n\r\n",
headers: [],
err: null
}
// Multiple Content-Length values should either be
// deduplicated if same or reject otherwise
// See Issue 16490.
// 5: {
// in:
// "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\nGopher hey\r\n",
// err: "cannot contain multiple Content-Length headers"
// },
// 6: {
// in:
// "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\nGopher\r\n",
// err: "cannot contain multiple Content-Length headers"
// },
// 7: {
// in:
// "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\nContent-Length:6\r\n\r\nGopher\r\n",
// err: null,
// headers: [{ key: "Content-Length", value: "6" }]
// },
// 8: {
// in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n",
// err: "cannot contain multiple Content-Length headers"
// },
// 9: {
// in: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n",
// err: "cannot contain multiple Content-Length headers"
// },
// 10: {
// in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
// headers: [{ key: "Content-Length", value: "0" }],
// err: null
// }
};
for (const p in testCases) {
const test = testCases[p];
const reader = new BufReader(new StringReader(test.in));
const [req, err] = await readRequest(reader);
assertEquals(test.err, err);
for (const h of test.headers) {
assertEquals(req.headers.get(h.key), h.value);
}
}
});
runIfMain(import.meta);

0 comments on commit c9b124e

Please sign in to comment.