diff options
Diffstat (limited to 'std/http')
-rw-r--r-- | std/http/cookie.ts | 2 | ||||
-rw-r--r-- | std/http/cookie_test.ts | 28 | ||||
-rwxr-xr-x | std/http/file_server.ts | 37 | ||||
-rw-r--r-- | std/http/file_server_test.ts | 8 | ||||
-rw-r--r-- | std/http/http_bench.ts | 2 | ||||
-rw-r--r-- | std/http/http_status.ts | 8 | ||||
-rw-r--r-- | std/http/io.ts | 10 | ||||
-rw-r--r-- | std/http/io_test.ts | 48 | ||||
-rw-r--r-- | std/http/mock.ts | 6 | ||||
-rw-r--r-- | std/http/racing_server_test.ts | 4 | ||||
-rw-r--r-- | std/http/server.ts | 4 | ||||
-rw-r--r-- | std/http/server_test.ts | 38 |
12 files changed, 96 insertions, 99 deletions
diff --git a/std/http/cookie.ts b/std/http/cookie.ts index 10c9bd689..062019f14 100644 --- a/std/http/cookie.ts +++ b/std/http/cookie.ts @@ -130,6 +130,6 @@ export function delCookie(res: Response, name: string): void { setCookie(res, { name: name, value: "", - expires: new Date(0) + expires: new Date(0), }); } diff --git a/std/http/cookie_test.ts b/std/http/cookie_test.ts index 8ab862bb3..1b78b2bff 100644 --- a/std/http/cookie_test.ts +++ b/std/http/cookie_test.ts @@ -27,9 +27,9 @@ test({ assertEquals(getCookies(req), { PREF: "al=en-GB&f1=123", wide: "1", - SID: "123" + SID: "123", }); - } + }, }); test({ @@ -41,7 +41,7 @@ test({ res.headers?.get("Set-Cookie"), "deno=; Expires=Thu, 01 Jan 1970 00:00:00 GMT" ); - } + }, }); test({ @@ -66,7 +66,7 @@ test({ name: "Space", value: "Cat", httpOnly: true, - secure: true + secure: true, }); assertEquals(res.headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly"); @@ -76,7 +76,7 @@ test({ value: "Cat", httpOnly: true, secure: true, - maxAge: 2 + maxAge: 2, }); assertEquals( res.headers.get("Set-Cookie"), @@ -91,7 +91,7 @@ test({ value: "Cat", httpOnly: true, secure: true, - maxAge: 0 + maxAge: 0, }); } catch (e) { error = true; @@ -105,7 +105,7 @@ test({ httpOnly: true, secure: true, maxAge: 2, - domain: "deno.land" + domain: "deno.land", }); assertEquals( res.headers.get("Set-Cookie"), @@ -120,7 +120,7 @@ test({ secure: true, maxAge: 2, domain: "deno.land", - sameSite: "Strict" + sameSite: "Strict", }); assertEquals( res.headers.get("Set-Cookie"), @@ -136,7 +136,7 @@ test({ secure: true, maxAge: 2, domain: "deno.land", - sameSite: "Lax" + sameSite: "Lax", }); assertEquals( res.headers.get("Set-Cookie"), @@ -151,7 +151,7 @@ test({ secure: true, maxAge: 2, domain: "deno.land", - path: "/" + path: "/", }); assertEquals( res.headers.get("Set-Cookie"), @@ -167,7 +167,7 @@ test({ maxAge: 2, domain: "deno.land", path: "/", - unparsed: ["unparsed=keyvalue", "batman=Bruce"] + unparsed: ["unparsed=keyvalue", "batman=Bruce"], }); assertEquals( res.headers.get("Set-Cookie"), @@ -184,7 +184,7 @@ test({ maxAge: 2, domain: "deno.land", path: "/", - expires: new Date(Date.UTC(1983, 0, 7, 15, 32)) + expires: new Date(Date.UTC(1983, 0, 7, 15, 32)), }); assertEquals( res.headers.get("Set-Cookie"), @@ -200,11 +200,11 @@ test({ setCookie(res, { name: "__Host-Kitty", value: "Meow", - domain: "deno.land" + domain: "deno.land", }); assertEquals( res.headers.get("Set-Cookie"), "__Host-Kitty=Meow; Secure; Path=/" ); - } + }, }); diff --git a/std/http/file_server.ts b/std/http/file_server.ts index 20d5d61e6..5582afd07 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -108,7 +108,7 @@ async function serveFile( const res = { status: 200, body: file, - headers + headers, }; return res; } @@ -137,7 +137,7 @@ async function serveDir( mode: modeToString(fileInfo.isDirectory(), mode), size: fileInfo.isFile() ? fileLenToString(fileInfo.size) : "", name: fileInfo.name ?? "", - url: fileUrl + url: fileUrl, }); } listEntry.sort((a, b) => @@ -152,7 +152,7 @@ async function serveDir( const res = { status: 200, body: page, - headers + headers, }; setContentLength(res); return res; @@ -162,12 +162,12 @@ function serveFallback(req: ServerRequest, e: Error): Promise<Response> { if (e instanceof Deno.errors.NotFound) { return Promise.resolve({ status: 404, - body: encoder.encode("Not found") + body: encoder.encode("Not found"), }); } else { return Promise.resolve({ status: 500, - body: encoder.encode("Internal server error") + body: encoder.encode("Internal server error"), }); } } @@ -258,19 +258,20 @@ function dirViewerTemplate(dirname: string, entries: EntryInfo[]): string { <th>Name</th> </tr> ${entries.map( - entry => html` - <tr> - <td class="mode"> - ${entry.mode} - </td> - <td> - ${entry.size} - </td> - <td> - <a href="${entry.url}">${entry.name}</a> - </td> - </tr> - ` + (entry) => + html` + <tr> + <td class="mode"> + ${entry.mode} + </td> + <td> + ${entry.size} + </td> + <td> + <a href="${entry.url}">${entry.name}</a> + </td> + </tr> + ` )} </table> </main> diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index fcf776ea2..3a3817ce0 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -14,10 +14,10 @@ async function startFileServer(): Promise<void> { "--allow-net", "http/file_server.ts", ".", - "--cors" + "--cors", ], stdout: "piped", - stderr: "null" + stderr: "null", }); // Once fileServer is ready it will write to its stdout. assert(fileServer.stdout != null); @@ -106,7 +106,7 @@ test(async function servePermissionDenied(): Promise<void> { const deniedServer = Deno.run({ cmd: [Deno.execPath(), "run", "--allow-net", "http/file_server.ts"], stdout: "piped", - stderr: "piped" + stderr: "piped", }); assert(deniedServer.stdout != null); const reader = new TextProtoReader(new BufReader(deniedServer.stdout)); @@ -132,7 +132,7 @@ test(async function servePermissionDenied(): Promise<void> { test(async function printHelp(): Promise<void> { const helpProcess = Deno.run({ cmd: [Deno.execPath(), "run", "http/file_server.ts", "--help"], - stdout: "piped" + stdout: "piped", }); assert(helpProcess.stdout != null); const r = new TextProtoReader(new BufReader(helpProcess.stdout)); diff --git a/std/http/http_bench.ts b/std/http/http_bench.ts index 9d1912831..15f223323 100644 --- a/std/http/http_bench.ts +++ b/std/http/http_bench.ts @@ -9,7 +9,7 @@ console.log(`http://${addr}/`); for await (const req of server) { const res = { body, - headers: new Headers() + headers: new Headers(), }; res.headers.set("Date", new Date().toUTCString()); res.headers.set("Connection", "keep-alive"); diff --git a/std/http/http_status.ts b/std/http/http_status.ts index ead1e1ff0..ce4338705 100644 --- a/std/http/http_status.ts +++ b/std/http/http_status.ts @@ -125,14 +125,13 @@ export enum Status { /** RFC 2774, 7 */ NotExtended = 510, /** RFC 6585, 6 */ - NetworkAuthenticationRequired = 511 + NetworkAuthenticationRequired = 511, } export const STATUS_TEXT = new Map<Status, string>([ [Status.Continue, "Continue"], [Status.SwitchingProtocols, "Switching Protocols"], [Status.Processing, "Processing"], - [Status.OK, "OK"], [Status.Created, "Created"], [Status.Accepted, "Accepted"], @@ -143,7 +142,6 @@ export const STATUS_TEXT = new Map<Status, string>([ [Status.MultiStatus, "Multi-Status"], [Status.AlreadyReported, "Already Reported"], [Status.IMUsed, "IM Used"], - [Status.MultipleChoices, "Multiple Choices"], [Status.MovedPermanently, "Moved Permanently"], [Status.Found, "Found"], @@ -152,7 +150,6 @@ export const STATUS_TEXT = new Map<Status, string>([ [Status.UseProxy, "Use Proxy"], [Status.TemporaryRedirect, "Temporary Redirect"], [Status.PermanentRedirect, "Permanent Redirect"], - [Status.BadRequest, "Bad Request"], [Status.Unauthorized, "Unauthorized"], [Status.PaymentRequired, "Payment Required"], @@ -181,7 +178,6 @@ export const STATUS_TEXT = new Map<Status, string>([ [Status.TooManyRequests, "Too Many Requests"], [Status.RequestHeaderFieldsTooLarge, "Request Header Fields Too Large"], [Status.UnavailableForLegalReasons, "Unavailable For Legal Reasons"], - [Status.InternalServerError, "Internal Server Error"], [Status.NotImplemented, "Not Implemented"], [Status.BadGateway, "Bad Gateway"], @@ -192,5 +188,5 @@ export const STATUS_TEXT = new Map<Status, string>([ [Status.InsufficientStorage, "Insufficient Storage"], [Status.LoopDetected, "Loop Detected"], [Status.NotExtended, "Not Extended"], - [Status.NetworkAuthenticationRequired, "Network Authentication Required"] + [Status.NetworkAuthenticationRequired, "Network Authentication Required"], ]); diff --git a/std/http/io.ts b/std/http/io.ts index 6d5d1f665..2875be44f 100644 --- a/std/http/io.ts +++ b/std/http/io.ts @@ -9,7 +9,7 @@ export function emptyReader(): Deno.Reader { return { read(_: Uint8Array): Promise<number | Deno.EOF> { return Promise.resolve(Deno.EOF); - } + }, }; } @@ -83,7 +83,7 @@ export function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader { } else { chunks.push({ offset: 0, - data: restChunk + data: restChunk, }); } return buf.byteLength; @@ -116,7 +116,7 @@ export function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader { const kProhibitedTrailerHeaders = [ "transfer-encoding", "content-length", - "trailer" + "trailer", ]; /** @@ -147,7 +147,7 @@ function parseTrailer(field: string | null): Set<string> | undefined { if (field == null) { return undefined; } - const keys = field.split(",").map(v => v.trim()); + const keys = field.split(",").map((v) => v.trim()); if (keys.length === 0) { throw new Error("Empty trailer"); } @@ -196,7 +196,7 @@ export async function writeTrailers( const writer = BufWriter.create(w); const trailerHeaderFields = trailer .split(",") - .map(s => s.trim().toLowerCase()); + .map((s) => s.trim().toLowerCase()); for (const f of trailerHeaderFields) { assert( !kProhibitedTrailerHeaders.includes(f), diff --git a/std/http/io_test.ts b/std/http/io_test.ts index 6c96d0b95..7261964d0 100644 --- a/std/http/io_test.ts +++ b/std/http/io_test.ts @@ -4,7 +4,7 @@ import { assertEquals, assert, assertNotEOF, - assertNotEquals + assertNotEquals, } from "../testing/asserts.ts"; import { bodyReader, @@ -12,7 +12,7 @@ import { readTrailers, parseHTTPVersion, readRequest, - writeResponse + writeResponse, } from "./io.ts"; import { encode, decode } from "../strings/mod.ts"; import { BufReader, ReadLineResult } from "../io/bufio.ts"; @@ -39,7 +39,7 @@ test("chunkedBodyReader", async () => { chunkify(5, "b"), chunkify(11, "c"), chunkify(22, "d"), - chunkify(0, "") + chunkify(0, ""), ].join(""); const h = new Headers(); const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); @@ -64,10 +64,10 @@ test("chunkedBodyReader with trailers", async () => { chunkify(0, ""), "deno: land\r\n", "node: js\r\n", - "\r\n" + "\r\n", ].join(""); const h = new Headers({ - trailer: "deno,node" + trailer: "deno,node", }); const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); assertEquals(h.has("trailer"), true); @@ -83,7 +83,7 @@ test("chunkedBodyReader with trailers", async () => { test("readTrailers", async () => { const h = new Headers({ - trailer: "deno,node" + trailer: "deno,node", }); const trailer = ["deno: land", "node: js", "", ""].join("\r\n"); await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); @@ -96,11 +96,11 @@ test("readTrailer should throw if undeclared headers found in trailer", async () const patterns = [ ["deno,node", "deno: land\r\nnode: js\r\ngo: lang\r\n\r\n"], ["deno", "node: js\r\n\r\n"], - ["deno", "node:js\r\ngo: lang\r\n\r\n"] + ["deno", "node:js\r\ngo: lang\r\n\r\n"], ]; for (const [header, trailer] of patterns) { const h = new Headers({ - trailer: header + trailer: header, }); await assertThrowsAsync( async () => { @@ -115,7 +115,7 @@ test("readTrailer should throw if undeclared headers found in trailer", async () test("readTrailer should throw if trailer contains prohibited fields", async () => { for (const f of ["content-length", "trailer", "transfer-encoding"]) { const h = new Headers({ - trailer: f + trailer: f, }); await assertThrowsAsync( async () => { @@ -192,7 +192,7 @@ test("parseHttpVersion", (): void => { { in: "HTTP/-1.0", err: true }, { in: "HTTP/0.-1", err: true }, { in: "HTTP/", err: true }, - { in: "HTTP/1,0", err: true } + { in: "HTTP/1,0", err: true }, ]; for (const t of testCases) { let r, err; @@ -320,10 +320,10 @@ test("writeResponse with trailer", async () => { status: 200, headers: new Headers({ "transfer-encoding": "chunked", - trailer: "deno,node" + trailer: "deno,node", }), body, - trailers: () => new Headers({ deno: "land", node: "js" }) + trailers: () => new Headers({ deno: "land", node: "js" }), }); const ret = w.toString(); const exp = [ @@ -338,7 +338,7 @@ test("writeResponse with trailer", async () => { "deno: land", "node: js", "", - "" + "", ].join("\r\n"); assertEquals(ret, exp); }); @@ -365,20 +365,20 @@ test(async function testReadRequestError(): Promise<void> { const testCases = [ { in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n", - headers: [{ key: "header", value: "foo" }] + headers: [{ key: "header", value: "foo" }], }, { in: "GET / HTTP/1.1\r\nheader:foo\r\n", - err: Deno.errors.UnexpectedEof + err: Deno.errors.UnexpectedEof, }, { in: "", err: Deno.EOF }, { in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n", - err: "http: method cannot contain a Content-Length" + err: "http: method cannot contain a Content-Length", }, { in: "HEAD / HTTP/1.1\r\n\r\n", - headers: [] + headers: [], }, // Multiple Content-Length values should either be // deduplicated if same or reject otherwise @@ -387,23 +387,23 @@ test(async function testReadRequestError(): Promise<void> { in: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\n" + "Gopher hey\r\n", - err: "cannot contain multiple Content-Length headers" + err: "cannot contain multiple Content-Length headers", }, { in: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\n" + "Gopher\r\n", - err: "cannot contain multiple Content-Length headers" + err: "cannot contain multiple Content-Length headers", }, { in: "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\n" + "Content-Length:6\r\n\r\nGopher\r\n", - headers: [{ key: "Content-Length", value: "6" }] + headers: [{ key: "Content-Length", value: "6" }], }, { in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n", - err: "cannot contain multiple Content-Length headers" + err: "cannot contain multiple Content-Length headers", }, // Setting an empty header is swallowed by textproto // see: readMIMEHeader() @@ -413,15 +413,15 @@ test(async function testReadRequestError(): Promise<void> { // }, { in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n", - headers: [{ key: "Content-Length", value: "0" }] + headers: [{ key: "Content-Length", value: "0" }], }, { 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" - } + err: "http: Transfer-Encoding and Content-Length cannot be send together", + }, ]; for (const test of testCases) { const reader = new BufReader(new StringReader(test.in)); diff --git a/std/http/mock.ts b/std/http/mock.ts index cee697bed..64bd3fcb9 100644 --- a/std/http/mock.ts +++ b/std/http/mock.ts @@ -4,12 +4,12 @@ export function mockConn(base: Partial<Deno.Conn> = {}): Deno.Conn { localAddr: { transport: "tcp", hostname: "", - port: 0 + port: 0, }, remoteAddr: { transport: "tcp", hostname: "", - port: 0 + port: 0, }, rid: -1, closeRead: (): void => {}, @@ -21,6 +21,6 @@ export function mockConn(base: Partial<Deno.Conn> = {}): Deno.Conn { return Promise.resolve(-1); }, close: (): void => {}, - ...base + ...base, }; } diff --git a/std/http/racing_server_test.ts b/std/http/racing_server_test.ts index 865777599..037e91ef9 100644 --- a/std/http/racing_server_test.ts +++ b/std/http/racing_server_test.ts @@ -7,7 +7,7 @@ let server: Deno.Process; async function startServer(): Promise<void> { server = run({ cmd: [Deno.execPath(), "run", "-A", "http/racing_server.ts"], - stdout: "piped" + stdout: "piped", }); // Once racing server is ready it will write to its stdout. assert(server.stdout != null); @@ -27,7 +27,7 @@ const input = [ "POST / HTTP/1.1\r\ncontent-length: 4\r\n\r\ndeno", "POST / HTTP/1.1\r\ntransfer-encoding: chunked\r\n\r\n4\r\ndeno\r\n0\r\n\r\n", "POST / HTTP/1.1\r\ntransfer-encoding: chunked\r\ntrailer: deno\r\n\r\n4\r\ndeno\r\n0\r\n\r\ndeno: land\r\n\r\n", - "GET / HTTP/1.1\r\n\r\n" + "GET / HTTP/1.1\r\n\r\n", ].join(""); const HUGE_BODY_SIZE = 1024 * 1024; const output = `HTTP/1.1 200 OK diff --git a/std/http/server.ts b/std/http/server.ts index 2cd51b005..00f401f62 100644 --- a/std/http/server.ts +++ b/std/http/server.ts @@ -7,7 +7,7 @@ import { chunkedBodyReader, emptyReader, writeResponse, - readRequest + readRequest, } from "./io.ts"; import Listener = Deno.Listener; import Conn = Deno.Conn; @@ -298,7 +298,7 @@ export type HTTPSOptions = Omit<Deno.ListenTLSOptions, "transport">; export function serveTLS(options: HTTPSOptions): Server { const tlsOptions: Deno.ListenTLSOptions = { ...options, - transport: "tcp" + transport: "tcp", }; const listener = listenTLS(tlsOptions); return new Server(listener); diff --git a/std/http/server_test.ts b/std/http/server_test.ts index f66b190b2..d6b2be053 100644 --- a/std/http/server_test.ts +++ b/std/http/server_test.ts @@ -11,7 +11,7 @@ import { assertEquals, assertNotEOF, assertStrContains, - assertThrowsAsync + assertThrowsAsync, } from "../testing/asserts.ts"; import { Response, ServerRequest, Server, serve } from "./server.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; @@ -30,27 +30,27 @@ const responseTests: ResponseTest[] = [ // Default response { response: {}, - raw: "HTTP/1.1 200 OK\r\n" + "content-length: 0" + "\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 + status: 404, }, - raw: "HTTP/1.1 404 Not Found\r\n" + "content-length: 0" + "\r\n\r\n" + raw: "HTTP/1.1 404 Not Found\r\n" + "content-length: 0" + "\r\n\r\n", }, // HTTP/1.1, chunked coding; empty trailer; close { response: { status: 200, - body: new Buffer(new TextEncoder().encode("abcdef")) + body: new Buffer(new TextEncoder().encode("abcdef")), }, raw: "HTTP/1.1 200 OK\r\n" + "transfer-encoding: chunked\r\n\r\n" + - "6\r\nabcdef\r\n0\r\n\r\n" - } + "6\r\nabcdef\r\n0\r\n\r\n", + }, ]; test(async function responseWrite(): Promise<void> { @@ -118,7 +118,7 @@ function totalReader(r: Deno.Reader): TotalReader { read, get total(): number { return _total; - } + }, }; } test(async function requestBodyWithContentLength(): Promise<void> { @@ -169,7 +169,7 @@ test("ServerRequest.finalize() should consume unread body / chunked, trailers", "deno: land", "node: js", "", - "" + "", ].join("\r\n"); const req = new ServerRequest(); req.headers = new Headers(); @@ -357,7 +357,7 @@ test({ // Runs a simple server as another process const p = Deno.run({ cmd: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"], - stdout: "piped" + stdout: "piped", }); let serverIsRunning = true; @@ -387,7 +387,7 @@ test({ p.stdout!.close(); p.close(); } - } + }, }); test({ @@ -401,9 +401,9 @@ test({ Deno.execPath(), "--allow-net", "--allow-read", - "http/testdata/simple_https_server.ts" + "http/testdata/simple_https_server.ts", ], - stdout: "piped" + stdout: "piped", }); let serverIsRunning = true; @@ -425,7 +425,7 @@ test({ const conn = await Deno.connectTLS({ hostname: "localhost", port: 4503, - certFile: "http/testdata/tls/RootCA.pem" + certFile: "http/testdata/tls/RootCA.pem", }); await Deno.writeAll( conn, @@ -444,7 +444,7 @@ test({ p.stdout!.close(); p.close(); } - } + }, }); test("close server while iterating", async (): Promise<void> => { @@ -485,7 +485,7 @@ test({ const resources = Deno.resources(); assertEquals(resources[conn.rid], "tcpStream"); conn.close(); - } + }, }); test({ @@ -498,7 +498,7 @@ test({ await assertThrowsAsync(async () => { await req.respond({ status: 12345, - body: new TextEncoder().encode("Hello World") + body: new TextEncoder().encode("Hello World"), }); }, Deno.errors.InvalidData); // The connection should be destroyed @@ -509,7 +509,7 @@ test({ const p = serverRoutine(); const conn = await Deno.connect({ hostname: "127.0.0.1", - port: 8124 + port: 8124, }); await Deno.writeAll( conn, @@ -517,5 +517,5 @@ test({ ); conn.close(); await p; - } + }, }); |