From 50a79584cb12129b3db1ef3e0eb9d0c8b9f20b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 30 May 2019 14:59:30 +0200 Subject: chore: Implement strict mode (denoland/deno_std#453) Original: https://github.com/denoland/deno_std/commit/be24677d15494e83eea2e99bfc5ccfdde31cb892 --- http/cookie.ts | 8 ++++---- http/cookie_test.ts | 2 +- http/file_server_test.ts | 7 ++++--- http/racing_server_test.ts | 6 +++--- http/server.ts | 37 ++++++++++++++++++------------------- http/server_test.ts | 37 ++++++++++++++++++------------------- 6 files changed, 48 insertions(+), 49 deletions(-) (limited to 'http') diff --git a/http/cookie.ts b/http/cookie.ts index 900d2941b..026ed1984 100644 --- a/http/cookie.ts +++ b/http/cookie.ts @@ -45,8 +45,8 @@ function toString(cookie: Cookie): string { if (cookie.httpOnly) { out.push("HttpOnly"); } - if (Number.isInteger(cookie.maxAge)) { - assert(cookie.maxAge > 0, "Max-Age must be an integer superior to 0"); + if (Number.isInteger(cookie.maxAge!)) { + assert(cookie.maxAge! > 0, "Max-Age must be an integer superior to 0"); out.push(`Max-Age=${cookie.maxAge}`); } if (cookie.domain) { @@ -75,10 +75,10 @@ function toString(cookie: Cookie): string { export function getCookies(req: ServerRequest): Cookies { if (req.headers.has("Cookie")) { const out: Cookies = {}; - const c = req.headers.get("Cookie").split(";"); + const c = req.headers.get("Cookie")!.split(";"); for (const kv of c) { const cookieVal = kv.split("="); - const key = cookieVal.shift().trim(); + const key = cookieVal.shift()!.trim(); out[key] = cookieVal.join("="); } return out; diff --git a/http/cookie_test.ts b/http/cookie_test.ts index 4b4e2759d..3d2e98afc 100644 --- a/http/cookie_test.ts +++ b/http/cookie_test.ts @@ -38,7 +38,7 @@ test({ let res: Response = {}; delCookie(res, "deno"); assertEquals( - res.headers.get("Set-Cookie"), + res.headers!.get("Set-Cookie"), "deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT" ); } diff --git a/http/file_server_test.ts b/http/file_server_test.ts index 1e2d86c4d..201524e7b 100644 --- a/http/file_server_test.ts +++ b/http/file_server_test.ts @@ -6,7 +6,8 @@ import { assert, assertEquals } from "../testing/asserts.ts"; import { BufReader, EOF } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -let fileServer; +let fileServer: Deno.Process; + async function startFileServer(): Promise { fileServer = run({ args: [ @@ -21,14 +22,14 @@ async function startFileServer(): Promise { stdout: "piped" }); // Once fileServer is ready it will write to its stdout. - const r = new TextProtoReader(new BufReader(fileServer.stdout)); + const r = new TextProtoReader(new BufReader(fileServer.stdout!)); const s = await r.readLine(); assert(s !== EOF && s.includes("server listening")); } function killFileServer(): void { fileServer.close(); - fileServer.stdout.close(); + fileServer.stdout!.close(); } test(async function serveFile(): Promise { diff --git a/http/racing_server_test.ts b/http/racing_server_test.ts index f98072c16..543366501 100644 --- a/http/racing_server_test.ts +++ b/http/racing_server_test.ts @@ -5,20 +5,20 @@ import { assert, assertEquals } from "../testing/asserts.ts"; import { BufReader, EOF } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -let server; +let server: Deno.Process; async function startServer(): Promise { server = run({ args: ["deno", "run", "-A", "http/racing_server.ts"], stdout: "piped" }); // Once fileServer is ready it will write to its stdout. - const r = new TextProtoReader(new BufReader(server.stdout)); + const r = new TextProtoReader(new BufReader(server.stdout!)); const s = await r.readLine(); assert(s !== EOF && s.includes("Racing server listening...")); } function killServer(): void { server.close(); - server.stdout.close(); + server.stdout!.close(); } let input = `GET / HTTP/1.1 diff --git a/http/server.ts b/http/server.ts index 5ea52a0b8..5c56a2ec2 100644 --- a/http/server.ts +++ b/http/server.ts @@ -73,11 +73,10 @@ export async function writeResponse(w: Writer, r: Response): Promise { let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`; setContentLength(r); + const headers = r.headers!; - if (r.headers) { - for (const [key, value] of r.headers) { - out += `${key}: ${value}\r\n`; - } + for (const [key, value] of headers!) { + out += `${key}: ${value}\r\n`; } out += "\r\n"; @@ -88,8 +87,8 @@ export async function writeResponse(w: Writer, r: Response): Promise { 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")); + } else if (headers.has("content-length")) { + const bodyLength = parseInt(headers.get("content-length")!); const n = await copy(writer, r.body); assert(n === bodyLength); } else { @@ -99,19 +98,19 @@ export async function writeResponse(w: Writer, r: Response): Promise { } export class ServerRequest { - url: string; - method: string; - proto: string; - protoMinor: number; - protoMajor: number; - headers: Headers; - r: BufReader; - w: BufWriter; + url!: string; + method!: string; + proto!: string; + protoMinor!: number; + protoMajor!: number; + headers!: Headers; + r!: BufReader; + w!: BufWriter; done: Deferred = deferred(); public async *bodyStream(): AsyncIterableIterator { if (this.headers.has("content-length")) { - const len = +this.headers.get("content-length"); + const len = +this.headers.get("content-length")!; if (Number.isNaN(len)) { return new Uint8Array(0); } @@ -128,7 +127,7 @@ export class ServerRequest { } else { if (this.headers.has("transfer-encoding")) { const transferEncodings = this.headers - .get("transfer-encoding") + .get("transfer-encoding")! .split(",") .map((e): string => e.trim().toLowerCase()); if (transferEncodings.includes("chunked")) { @@ -334,14 +333,14 @@ export class Server implements AsyncIterable { // Wait for the request to be processed before we accept a new request on // this connection. - await req.done; + await req!.done; } - if (req === EOF) { + if (req! === EOF) { // The connection was gracefully closed. } else if (err) { // An error was thrown while parsing request headers. - await writeResponse(req.w, { + await writeResponse(req!.w, { status: 400, body: new TextEncoder().encode(`${err.message}\r\n\r\n`) }); diff --git a/http/server_test.ts b/http/server_test.ts index e24ab0be9..2a6163d91 100644 --- a/http/server_test.ts +++ b/http/server_test.ts @@ -336,65 +336,64 @@ malformedHeader // 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 { - const testCases = { - 0: { + const testCases = [ + { in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n", headers: [{ key: "header", value: "foo" }] }, - 1: { + { in: "GET / HTTP/1.1\r\nheader:foo\r\n", err: UnexpectedEOFError }, - 2: { in: "", err: EOF }, - 3: { + { in: "", err: EOF }, + { 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: [] }, // 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", 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" }, // Setting an empty header is swallowed by textproto // see: readMIMEHeader() - // 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" }] }, - 11: { + { in: "POST / HTTP/1.1\r\nContent-Length:0\r\ntransfer-encoding: chunked\r\n\r\n", headers: [], err: "http: Transfer-Encoding and Content-Length cannot be send together" } - }; - for (const p in testCases) { - const test = testCases[p]; + ]; + for (const test of testCases) { const reader = new BufReader(new StringReader(test.in)); let err; let req; @@ -408,12 +407,12 @@ test(async function testReadRequestError(): Promise { } else if (typeof test.err === "string") { assertEquals(err.message, test.err); } else if (test.err) { - assert(err instanceof test.err); + assert(err instanceof (test.err as typeof UnexpectedEOFError)); } else { assertEquals(err, undefined); assertNotEquals(req, EOF); - for (const h of test.headers) { - assertEquals(req.headers.get(h.key), h.value); + for (const h of test.headers!) { + assertEquals((req! as ServerRequest).headers.get(h.key), h.value); } } } -- cgit v1.2.3