2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-01-15 15:09:26 -05:00
|
|
|
import { Buffer, BufReader, BufWriter } from "../../../test_util/std/io/mod.ts";
|
2022-10-17 18:57:31 -04:00
|
|
|
import { TextProtoReader } from "../testdata/run/textproto.ts";
|
2022-03-19 09:21:49 -04:00
|
|
|
import { serve, serveTls } from "../../../test_util/std/http/server.ts";
|
2021-04-08 18:34:15 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-05 16:43:46 -04:00
|
|
|
assertRejects,
|
2021-10-04 21:50:40 -04:00
|
|
|
assertStrictEquals,
|
2021-08-24 18:55:32 -04:00
|
|
|
assertThrows,
|
2021-06-24 21:44:14 -04:00
|
|
|
delay,
|
2021-07-08 07:33:01 -04:00
|
|
|
fail,
|
2021-04-08 18:34:15 -04:00
|
|
|
} from "./test_util.ts";
|
2022-10-23 18:45:45 -04:00
|
|
|
import { join } from "../../../test_util/std/path/mod.ts";
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const listenPort = 4507;
|
|
|
|
const listenPort2 = 4508;
|
|
|
|
|
2023-02-12 15:51:07 -05:00
|
|
|
const {
|
|
|
|
buildCaseInsensitiveCommaValueFinder,
|
|
|
|
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
|
|
|
} = Deno[Deno.internal];
|
|
|
|
|
2021-06-14 08:52:49 -04:00
|
|
|
async function writeRequestAndReadResponse(conn: Deno.Conn): Promise<string> {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
2023-07-31 09:29:17 -04:00
|
|
|
const body = `GET / HTTP/1.1\r\nHost: 127.0.0.1:${listenPort}\r\n\r\n`;
|
2021-06-14 08:52:49 -04:00
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null);
|
2022-10-17 18:57:31 -04:00
|
|
|
const headers = await tpr.readMimeHeader();
|
2021-06-14 08:52:49 -04:00
|
|
|
assert(headers !== null);
|
|
|
|
|
|
|
|
const chunkedReader = chunkedBodyReader(headers, r);
|
|
|
|
const buf = new Uint8Array(5);
|
|
|
|
const dest = new Buffer();
|
|
|
|
let result: number | null;
|
|
|
|
while ((result = await chunkedReader.read(buf)) !== null) {
|
|
|
|
const len = Math.min(buf.byteLength, result);
|
|
|
|
await dest.write(buf.subarray(0, len));
|
|
|
|
}
|
|
|
|
return decoder.decode(dest.bytes());
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function httpServerBasic() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2021-04-08 18:34:15 -04:00
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2022-03-22 18:08:29 -04:00
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
2023-07-31 09:29:17 -04:00
|
|
|
assertEquals(new URL(request.url).href, `http://127.0.0.1:${listenPort}/`);
|
2022-03-22 18:08:29 -04:00
|
|
|
assertEquals(await request.text(), "");
|
|
|
|
await respondWith(
|
|
|
|
new Response("Hello World", { headers: { "foo": "bar" } }),
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2021-04-08 18:34:15 -04:00
|
|
|
headers: { "connection": "close" },
|
|
|
|
});
|
2021-06-25 18:01:25 -04:00
|
|
|
const clone = resp.clone();
|
2021-04-08 18:34:15 -04:00
|
|
|
const text = await resp.text();
|
|
|
|
assertEquals(text, "Hello World");
|
2021-04-12 10:24:45 -04:00
|
|
|
assertEquals(resp.headers.get("foo"), "bar");
|
2021-06-25 18:01:25 -04:00
|
|
|
const cloneText = await clone.text();
|
|
|
|
assertEquals(cloneText, "Hello World");
|
2021-04-08 18:34:15 -04:00
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
|
|
|
|
httpConn!.close();
|
2021-04-08 18:34:15 -04:00
|
|
|
});
|
|
|
|
|
2022-07-12 13:31:37 -04:00
|
|
|
// https://github.com/denoland/deno/issues/15107
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpLazyHeadersIssue15107() {
|
|
|
|
let headers: Headers;
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 2333 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request } = e;
|
|
|
|
request.text();
|
|
|
|
headers = request.headers;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-07-12 13:31:37 -04:00
|
|
|
})();
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ port: 2333 });
|
|
|
|
// Send GET request with a body + content-length.
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const body =
|
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:2333\r\nContent-Length: 5\r\n\r\n12345`;
|
|
|
|
const writeResult = await conn.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await promise;
|
|
|
|
conn.close();
|
|
|
|
assertEquals(headers!.get("content-length"), "5");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpReadHeadersAfterClose() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 2334 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
|
|
|
|
await request.text(); // Read body
|
|
|
|
await respondWith(new Response("Hello World")); // Closes request
|
|
|
|
|
|
|
|
assertThrows(() => request.headers, TypeError, "request closed");
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-07-12 13:31:37 -04:00
|
|
|
})();
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ port: 2334 });
|
|
|
|
// Send GET request with a body + content-length.
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const body =
|
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:2333\r\nContent-Length: 5\r\n\r\n12345`;
|
|
|
|
const writeResult = await conn.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await promise;
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-11 12:39:55 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerGetRequestBody() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2021-10-11 12:39:55 -04:00
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-10-11 12:39:55 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-10-11 12:39:55 -04:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.body, null);
|
|
|
|
await respondWith(new Response("", { headers: {} }));
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const conn = await Deno.connect({ port: listenPort });
|
2021-10-11 12:39:55 -04:00
|
|
|
// Send GET request with a body + content-length.
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const body =
|
2023-07-31 09:29:17 -04:00
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:${listenPort}\r\nContent-Length: 5\r\n\r\n12345`;
|
2021-10-11 12:39:55 -04:00
|
|
|
const writeResult = await conn.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
|
|
|
|
const resp = new Uint8Array(200);
|
|
|
|
const readResult = await conn.read(resp);
|
2022-03-04 00:04:39 -05:00
|
|
|
assertEquals(readResult, 138);
|
2021-10-11 12:39:55 -04:00
|
|
|
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-10-11 12:39:55 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-08 18:34:15 -04:00
|
|
|
async function httpServerStreamResponse() {
|
|
|
|
const stream = new TransformStream();
|
|
|
|
const writer = stream.writable.getWriter();
|
|
|
|
writer.write(new TextEncoder().encode("hello "));
|
|
|
|
writer.write(new TextEncoder().encode("world"));
|
|
|
|
writer.close();
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-04-08 18:34:15 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
|
|
|
const { request, respondWith } = evt;
|
|
|
|
assert(!request.body);
|
|
|
|
await respondWith(new Response(stream.readable));
|
|
|
|
})();
|
|
|
|
|
2023-09-26 06:46:06 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`);
|
2021-04-08 18:34:15 -04:00
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("hello world", respBody);
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2021-04-08 18:34:15 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-08 18:34:15 -04:00
|
|
|
async function httpServerStreamRequest() {
|
|
|
|
const stream = new TransformStream();
|
|
|
|
const writer = stream.writable.getWriter();
|
|
|
|
writer.write(new TextEncoder().encode("hello "));
|
|
|
|
writer.write(new TextEncoder().encode("world"));
|
|
|
|
writer.close();
|
|
|
|
|
2023-09-16 01:48:31 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-04-08 18:34:15 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
|
|
|
const { request, respondWith } = evt;
|
|
|
|
const reqBody = await request.text();
|
|
|
|
assertEquals("hello world", reqBody);
|
|
|
|
await respondWith(new Response(""));
|
|
|
|
|
|
|
|
// TODO(ry) If we don't call httpConn.nextRequest() here we get "error sending
|
2023-07-31 09:29:17 -04:00
|
|
|
// request for url (https://localhost:${listenPort}/): connection closed before
|
2021-04-08 18:34:15 -04:00
|
|
|
// message completed".
|
|
|
|
assertEquals(await httpConn.nextRequest(), null);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2021-04-08 18:34:15 -04:00
|
|
|
body: stream.readable,
|
|
|
|
method: "POST",
|
|
|
|
headers: { "connection": "close" },
|
|
|
|
});
|
|
|
|
|
|
|
|
await resp.arrayBuffer();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerStreamDuplex() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-09-22 19:50:50 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-09-22 19:50:50 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
|
|
|
const { request, respondWith } = evt;
|
|
|
|
assert(request.body);
|
|
|
|
await respondWith(new Response(request.body));
|
|
|
|
})();
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
const ts = new TransformStream();
|
|
|
|
const writable = ts.writable.getWriter();
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2021-09-22 19:50:50 -04:00
|
|
|
method: "POST",
|
|
|
|
body: ts.readable,
|
|
|
|
});
|
|
|
|
assert(resp.body);
|
|
|
|
const reader = resp.body.getReader();
|
|
|
|
await writable.write(new Uint8Array([1]));
|
|
|
|
const chunk1 = await reader.read();
|
|
|
|
assert(!chunk1.done);
|
|
|
|
assertEquals(chunk1.value, new Uint8Array([1]));
|
|
|
|
await writable.write(new Uint8Array([2]));
|
|
|
|
const chunk2 = await reader.read();
|
|
|
|
assert(!chunk2.done);
|
|
|
|
assertEquals(chunk2.value, new Uint8Array([2]));
|
2022-09-06 13:38:37 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
await writable.close();
|
|
|
|
const chunk3 = await reader.read();
|
|
|
|
assert(chunk3.done);
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2021-09-22 19:50:50 -04:00
|
|
|
},
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function httpServerClose() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
|
|
|
const client = await Deno.connect({ port: listenPort });
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(await listener.accept());
|
2021-04-08 18:34:15 -04:00
|
|
|
client.close();
|
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assertEquals(evt, null);
|
|
|
|
// Note httpConn is automatically closed when "done" is reached.
|
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerInvalidMethod() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
|
|
|
const client = await Deno.connect({ port: listenPort });
|
2021-09-22 19:50:50 -04:00
|
|
|
const httpConn = Deno.serveHttp(await listener.accept());
|
|
|
|
await client.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await httpConn.nextRequest();
|
|
|
|
},
|
|
|
|
Deno.errors.Http,
|
|
|
|
"invalid HTTP method parsed",
|
|
|
|
);
|
|
|
|
// Note httpConn is automatically closed when it errors.
|
|
|
|
client.close();
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function httpServerWithTls() {
|
2021-04-08 18:34:15 -04:00
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
2021-08-11 10:20:47 -04:00
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
2021-04-08 18:34:15 -04:00
|
|
|
});
|
|
|
|
const conn = await listener.accept();
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
2021-04-09 17:35:29 -04:00
|
|
|
const { respondWith } = evt;
|
2021-04-08 18:34:15 -04:00
|
|
|
await respondWith(new Response("Hello World"));
|
|
|
|
|
|
|
|
// TODO(ry) If we don't call httpConn.nextRequest() here we get "error sending
|
2023-07-31 09:29:17 -04:00
|
|
|
// request for url (https://localhost:${listenPort}/): connection closed before
|
2021-04-08 18:34:15 -04:00
|
|
|
// message completed".
|
|
|
|
assertEquals(await httpConn.nextRequest(), null);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
2021-09-30 03:26:15 -04:00
|
|
|
const caCert = Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
2021-04-08 18:34:15 -04:00
|
|
|
const resp = await fetch(`https://${hostname}:${port}/`, {
|
|
|
|
headers: { "connection": "close" },
|
2023-09-26 06:46:06 -04:00
|
|
|
client,
|
2021-04-08 18:34:15 -04:00
|
|
|
});
|
2023-09-26 06:46:06 -04:00
|
|
|
client.close();
|
2021-04-08 18:34:15 -04:00
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("Hello World", respBody);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-04-15 18:48:56 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-15 18:48:56 -04:00
|
|
|
async function httpServerRegressionHang() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-04-15 18:48:56 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-04-15 18:48:56 -04:00
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { request, respondWith } = event;
|
|
|
|
const reqBody = await request.text();
|
|
|
|
assertEquals("request", reqBody);
|
|
|
|
await respondWith(new Response("response"));
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2021-04-15 18:48:56 -04:00
|
|
|
method: "POST",
|
|
|
|
body: "request",
|
|
|
|
});
|
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("response", respBody);
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
|
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2021-04-15 18:48:56 -04:00
|
|
|
},
|
|
|
|
);
|
2021-04-23 06:34:04 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-23 06:34:04 -04:00
|
|
|
async function httpServerCancelBodyOnResponseFailure() {
|
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-04-23 06:34:04 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { respondWith } = event;
|
2021-09-05 16:43:46 -04:00
|
|
|
let cancelReason: string;
|
|
|
|
await assertRejects(
|
2021-04-23 06:34:04 -04:00
|
|
|
async () => {
|
|
|
|
let interval = 0;
|
|
|
|
await respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
const message = `data: ${Date.now()}\n\n`;
|
|
|
|
controller.enqueue(new TextEncoder().encode(message));
|
|
|
|
}, 200);
|
|
|
|
},
|
|
|
|
cancel(reason) {
|
|
|
|
cancelReason = reason;
|
|
|
|
clearInterval(interval);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Deno.errors.Http,
|
2021-09-05 16:43:46 -04:00
|
|
|
cancelReason!,
|
2021-04-23 06:34:04 -04:00
|
|
|
);
|
2021-09-05 16:43:46 -04:00
|
|
|
assert(cancelReason!);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-04-23 06:34:04 -04:00
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`);
|
2021-04-23 06:34:04 -04:00
|
|
|
await resp.body!.cancel();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-05-19 08:39:52 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-05-19 08:39:52 -04:00
|
|
|
async function httpServerNextRequestErrorExposedInResponse() {
|
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-05-19 08:39:52 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
// Start polling for the next request before awaiting response.
|
|
|
|
const nextRequestPromise = httpConn.nextRequest();
|
|
|
|
const { respondWith } = event;
|
2021-09-05 16:43:46 -04:00
|
|
|
await assertRejects(
|
2021-05-19 08:39:52 -04:00
|
|
|
async () => {
|
|
|
|
let interval = 0;
|
|
|
|
await respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
const message = `data: ${Date.now()}\n\n`;
|
|
|
|
controller.enqueue(new TextEncoder().encode(message));
|
|
|
|
}, 200);
|
|
|
|
},
|
|
|
|
cancel() {
|
|
|
|
clearInterval(interval);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Deno.errors.Http,
|
|
|
|
"connection closed",
|
|
|
|
);
|
2021-10-04 21:50:40 -04:00
|
|
|
// The error from `op_http_accept` reroutes to `respondWith()`.
|
2021-05-19 08:39:52 -04:00
|
|
|
assertEquals(await nextRequestPromise, null);
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`);
|
2021-05-19 08:39:52 -04:00
|
|
|
await resp.body!.cancel();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-05-20 21:11:53 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-05-20 21:11:53 -04:00
|
|
|
async function httpServerEmptyBlobResponse() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-05-20 21:11:53 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-05-20 21:11:53 -04:00
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { respondWith } = event;
|
|
|
|
await respondWith(new Response(new Blob([])));
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`);
|
2021-05-20 21:11:53 -04:00
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("", respBody);
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2021-05-20 21:11:53 -04:00
|
|
|
},
|
|
|
|
);
|
2021-06-03 19:32:36 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-03 19:32:36 -04:00
|
|
|
async function httpServerNextRequestResolvesOnClose() {
|
|
|
|
const httpConnList: Deno.HttpConn[] = [];
|
|
|
|
|
|
|
|
async function serve(l: Deno.Listener) {
|
|
|
|
for await (const conn of l) {
|
|
|
|
(async () => {
|
|
|
|
const c = Deno.serveHttp(conn);
|
|
|
|
httpConnList.push(c);
|
|
|
|
for await (const { respondWith } of c) {
|
|
|
|
respondWith(new Response("hello"));
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const l = Deno.listen({ port: listenPort });
|
2021-06-03 19:32:36 -04:00
|
|
|
serve(l);
|
|
|
|
|
|
|
|
await delay(300);
|
2023-07-31 09:29:17 -04:00
|
|
|
const res = await fetch(`http://localhost:${listenPort}/`);
|
2021-06-03 19:32:36 -04:00
|
|
|
const _text = await res.text();
|
|
|
|
|
|
|
|
// Close connection and listener.
|
|
|
|
httpConnList.forEach((conn) => conn.close());
|
|
|
|
l.close();
|
|
|
|
|
|
|
|
await delay(300);
|
|
|
|
},
|
|
|
|
);
|
2021-06-14 08:52:49 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-14 08:52:49 -04:00
|
|
|
// Issue: https://github.com/denoland/deno/issues/10870
|
|
|
|
async function httpServerHang() {
|
|
|
|
// Quick and dirty way to make a readable stream from a string. Alternatively,
|
|
|
|
// `readableStreamFromReader(file)` could be used.
|
|
|
|
function stream(s: string): ReadableStream<Uint8Array> {
|
|
|
|
return new Response(s).body!;
|
|
|
|
}
|
|
|
|
|
|
|
|
const httpConns: Deno.HttpConn[] = [];
|
|
|
|
const promise = (async () => {
|
|
|
|
let count = 0;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-06-14 08:52:49 -04:00
|
|
|
for await (const conn of listener) {
|
|
|
|
(async () => {
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
httpConns.push(httpConn);
|
|
|
|
for await (const { respondWith } of httpConn) {
|
|
|
|
respondWith(new Response(stream("hello")));
|
|
|
|
|
|
|
|
count++;
|
|
|
|
if (count >= 2) {
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const clientConn = await Deno.connect({ port: listenPort });
|
2021-06-14 08:52:49 -04:00
|
|
|
|
|
|
|
const r1 = await writeRequestAndReadResponse(clientConn);
|
|
|
|
assertEquals(r1, "hello");
|
|
|
|
|
|
|
|
const r2 = await writeRequestAndReadResponse(clientConn);
|
|
|
|
assertEquals(r2, "hello");
|
|
|
|
|
|
|
|
clientConn.close();
|
|
|
|
await promise;
|
|
|
|
for (const conn of httpConns) {
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-06-14 16:10:55 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-14 16:10:55 -04:00
|
|
|
// Issue: https://github.com/denoland/deno/issues/10930
|
|
|
|
async function httpServerStreamingResponse() {
|
|
|
|
// This test enqueues a single chunk for readable
|
|
|
|
// stream and waits for client to read that chunk and signal
|
|
|
|
// it before enqueueing subsequent chunk. Issue linked above
|
|
|
|
// presented a situation where enqueued chunks were not
|
|
|
|
// written to the HTTP connection until the next chunk was enqueued.
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
|
|
|
|
const deferreds = [
|
2023-11-22 06:11:20 -05:00
|
|
|
Promise.withResolvers<void>(),
|
|
|
|
Promise.withResolvers<void>(),
|
|
|
|
Promise.withResolvers<void>(),
|
2021-06-14 16:10:55 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
async function writeRequest(conn: Deno.Conn) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
2023-07-31 09:29:17 -04:00
|
|
|
const body = `GET / HTTP/1.1\r\nHost: 127.0.0.1:${listenPort}\r\n\r\n`;
|
2021-06-14 16:10:55 -04:00
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null);
|
2022-10-17 18:57:31 -04:00
|
|
|
const headers = await tpr.readMimeHeader();
|
2021-06-14 16:10:55 -04:00
|
|
|
assert(headers !== null);
|
|
|
|
|
|
|
|
const chunkedReader = chunkedBodyReader(headers, r);
|
|
|
|
const buf = new Uint8Array(5);
|
|
|
|
const dest = new Buffer();
|
|
|
|
let result: number | null;
|
|
|
|
while ((result = await chunkedReader.read(buf)) !== null) {
|
|
|
|
const len = Math.min(buf.byteLength, result);
|
|
|
|
await dest.write(buf.subarray(0, len));
|
|
|
|
// Resolve a deferred - this will make response stream to
|
|
|
|
// enqueue next chunk.
|
|
|
|
deferreds[counter - 1].resolve();
|
|
|
|
}
|
|
|
|
return decoder.decode(dest.bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
function periodicStream() {
|
|
|
|
return new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(`${counter}\n`);
|
|
|
|
counter++;
|
|
|
|
},
|
|
|
|
|
|
|
|
async pull(controller) {
|
|
|
|
if (counter >= 3) {
|
|
|
|
return controller.close();
|
|
|
|
}
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferreds[counter - 1].promise;
|
2021-06-14 16:10:55 -04:00
|
|
|
|
|
|
|
controller.enqueue(`${counter}\n`);
|
|
|
|
counter++;
|
|
|
|
},
|
|
|
|
}).pipeThrough(new TextEncoderStream());
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-06-14 16:10:55 -04:00
|
|
|
const finished = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-06-14 16:10:55 -04:00
|
|
|
const requestEvent = await httpConn.nextRequest();
|
|
|
|
const { respondWith } = requestEvent!;
|
|
|
|
await respondWith(new Response(periodicStream()));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// start a client
|
2023-07-31 09:29:17 -04:00
|
|
|
const clientConn = await Deno.connect({ port: listenPort });
|
2021-06-14 16:10:55 -04:00
|
|
|
|
|
|
|
const r1 = await writeRequest(clientConn);
|
|
|
|
assertEquals(r1, "0\n1\n2\n");
|
|
|
|
|
|
|
|
await finished;
|
|
|
|
clientConn.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
|
|
|
|
httpConn!.close();
|
2021-06-14 16:10:55 -04:00
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpRequestLatin1Headers() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2021-09-22 19:50:50 -04:00
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2022-03-22 18:08:29 -04:00
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
assertEquals(request.headers.get("X-Header-Test"), "á");
|
|
|
|
await respondWith(
|
|
|
|
new Response("", { headers: { "X-Header-Test": "Æ" } }),
|
|
|
|
);
|
2021-09-22 19:50:50 -04:00
|
|
|
})();
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const clientConn = await Deno.connect({ port: listenPort });
|
2021-09-22 19:50:50 -04:00
|
|
|
const requestText =
|
2023-07-31 09:29:17 -04:00
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:${listenPort}\r\nX-Header-Test: á\r\n\r\n`;
|
2021-09-22 19:50:50 -04:00
|
|
|
const requestBytes = new Uint8Array(requestText.length);
|
|
|
|
for (let i = 0; i < requestText.length; i++) {
|
|
|
|
requestBytes[i] = requestText.charCodeAt(i);
|
|
|
|
}
|
|
|
|
let written = 0;
|
|
|
|
while (written < requestBytes.byteLength) {
|
|
|
|
written += await clientConn.write(requestBytes.slice(written));
|
|
|
|
}
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
let responseText = "";
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
let read;
|
2022-09-06 13:38:37 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
while ((read = await clientConn.read(buf)) !== null) {
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-09-22 19:50:50 -04:00
|
|
|
for (let i = 0; i < read; i++) {
|
|
|
|
responseText += String.fromCharCode(buf[i]);
|
|
|
|
}
|
2021-06-26 20:29:01 -04:00
|
|
|
}
|
2022-09-06 13:38:37 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
clientConn.close();
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
assert(/\r\n[Xx]-[Hh]eader-[Tt]est: Æ\r\n/.test(responseText));
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-06-27 18:19:40 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-27 18:19:40 -04:00
|
|
|
async function httpServerRequestWithoutPath() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-06-27 18:19:40 -04:00
|
|
|
const promise = (async () => {
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2022-03-22 18:08:29 -04:00
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
2023-07-31 09:29:17 -04:00
|
|
|
assertEquals(
|
|
|
|
new URL(request.url).href,
|
|
|
|
`http://127.0.0.1:${listenPort}/`,
|
|
|
|
);
|
2022-03-22 18:08:29 -04:00
|
|
|
assertEquals(await request.text(), "");
|
|
|
|
await respondWith(new Response());
|
2021-06-27 18:19:40 -04:00
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const clientConn = await Deno.connect({ port: listenPort });
|
2021-06-27 18:19:40 -04:00
|
|
|
|
|
|
|
async function writeRequest(conn: Deno.Conn) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
|
|
|
const body =
|
2023-07-31 09:29:17 -04:00
|
|
|
`CONNECT 127.0.0.1:${listenPort} HTTP/1.1\r\nHost: 127.0.0.1:${listenPort}\r\n\r\n`;
|
2021-06-27 18:19:40 -04:00
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null);
|
|
|
|
const m = statusLine.match(/^(.+?) (.+?) (.+?)$/);
|
|
|
|
assert(m !== null, "must be matched");
|
|
|
|
const [_, _proto, status, _ok] = m;
|
|
|
|
assertEquals(status, "200");
|
2022-10-17 18:57:31 -04:00
|
|
|
const headers = await tpr.readMimeHeader();
|
2021-06-27 18:19:40 -04:00
|
|
|
assert(headers !== null);
|
|
|
|
}
|
|
|
|
|
|
|
|
await writeRequest(clientConn);
|
|
|
|
clientConn.close();
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-06-27 18:19:40 -04:00
|
|
|
},
|
|
|
|
);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function httpServerWebSocket() {
|
2021-07-08 07:33:01 -04:00
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
const {
|
|
|
|
response,
|
|
|
|
socket,
|
|
|
|
} = Deno.upgradeWebSocket(request);
|
|
|
|
socket.onerror = () => fail();
|
|
|
|
socket.onmessage = (m) => {
|
|
|
|
socket.send(m.data);
|
|
|
|
socket.close(1001);
|
|
|
|
};
|
2023-09-16 01:48:31 -04:00
|
|
|
const close = new Promise<void>((resolve) => {
|
|
|
|
socket.onclose = () => resolve();
|
|
|
|
});
|
2022-03-22 18:08:29 -04:00
|
|
|
await respondWith(response);
|
2023-09-16 01:48:31 -04:00
|
|
|
await close;
|
2021-07-08 07:33:01 -04:00
|
|
|
})();
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const def = Promise.withResolvers<void>();
|
2023-07-31 09:29:17 -04:00
|
|
|
const ws = new WebSocket(`ws://localhost:${listenPort}`);
|
2021-07-08 07:33:01 -04:00
|
|
|
ws.onmessage = (m) => assertEquals(m.data, "foo");
|
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onclose = () => def.resolve();
|
|
|
|
ws.onopen = () => ws.send("foo");
|
2023-11-22 06:11:20 -05:00
|
|
|
await def.promise;
|
2021-07-08 07:33:01 -04:00
|
|
|
await promise;
|
|
|
|
});
|
2021-07-16 10:31:16 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function httpUpgradeWebSocket() {
|
2021-07-22 08:28:55 -04:00
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "Upgrade",
|
|
|
|
upgrade: "websocket",
|
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
assertEquals(response.headers.get("connection"), "Upgrade");
|
|
|
|
assertEquals(response.headers.get("upgrade"), "websocket");
|
|
|
|
assertEquals(
|
|
|
|
response.headers.get("sec-websocket-accept"),
|
|
|
|
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function httpUpgradeWebSocketMultipleConnectionOptions() {
|
2021-07-22 08:28:55 -04:00
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
2021-10-26 17:06:44 -04:00
|
|
|
connection: "keep-alive, upgrade",
|
2021-07-22 08:28:55 -04:00
|
|
|
upgrade: "websocket",
|
2021-07-23 22:14:03 -04:00
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function httpUpgradeWebSocketMultipleUpgradeOptions() {
|
2021-07-23 22:14:03 -04:00
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
2021-10-26 17:06:44 -04:00
|
|
|
connection: "upgrade",
|
|
|
|
upgrade: "websocket, foo",
|
2021-07-22 08:28:55 -04:00
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function httpUpgradeWebSocketCaseInsensitiveUpgradeHeader() {
|
2021-08-24 18:55:32 -04:00
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
2021-10-26 17:06:44 -04:00
|
|
|
upgrade: "Websocket",
|
2021-08-24 18:55:32 -04:00
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function httpUpgradeWebSocketInvalidUpgradeHeader() {
|
2021-08-24 18:55:32 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
|
|
|
upgrade: "invalid",
|
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Deno.upgradeWebSocket(request);
|
|
|
|
},
|
|
|
|
TypeError,
|
2021-10-26 17:06:44 -04:00
|
|
|
"Invalid Header: 'upgrade' header must contain 'websocket'",
|
2021-08-24 18:55:32 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function httpUpgradeWebSocketWithoutUpgradeHeader() {
|
2021-08-24 18:55:32 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Deno.upgradeWebSocket(request);
|
|
|
|
},
|
|
|
|
TypeError,
|
2021-10-26 17:06:44 -04:00
|
|
|
"Invalid Header: 'upgrade' header must contain 'websocket'",
|
2021-08-24 18:55:32 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpCookieConcatenation() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2021-09-22 19:50:50 -04:00
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2022-03-22 18:08:29 -04:00
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
2023-07-31 09:29:17 -04:00
|
|
|
assertEquals(
|
|
|
|
new URL(request.url).href,
|
|
|
|
`http://127.0.0.1:${listenPort}/`,
|
|
|
|
);
|
2022-03-22 18:08:29 -04:00
|
|
|
assertEquals(await request.text(), "");
|
|
|
|
assertEquals(request.headers.get("cookie"), "foo=bar; bar=foo");
|
|
|
|
await respondWith(new Response("ok"));
|
2021-09-22 19:50:50 -04:00
|
|
|
})();
|
2021-07-16 10:31:16 -04:00
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2021-09-22 19:50:50 -04:00
|
|
|
headers: [
|
|
|
|
["connection", "close"],
|
|
|
|
["cookie", "foo=bar"],
|
|
|
|
["cookie", "bar=foo"],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const text = await resp.text();
|
|
|
|
assertEquals(text, "ok");
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-09-22 19:50:50 -04:00
|
|
|
},
|
|
|
|
);
|
2021-08-13 06:07:05 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11651
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function httpServerPanic() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
|
|
|
const client = await Deno.connect({ port: listenPort });
|
2021-08-13 06:07:05 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
|
|
|
|
// This message is incomplete on purpose, we'll forcefully close client connection
|
|
|
|
// after it's flushed to cause connection to error out on the server side.
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
await client.write(encoder.encode("GET / HTTP/1.1"));
|
|
|
|
|
|
|
|
httpConn.nextRequest();
|
|
|
|
await client.write(encoder.encode("\r\n\r\n"));
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-08-13 06:07:05 -04:00
|
|
|
|
|
|
|
client.close();
|
|
|
|
listener.close();
|
|
|
|
});
|
2021-08-16 07:43:43 -04:00
|
|
|
|
2022-04-25 13:20:29 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, write: true, read: true } },
|
|
|
|
async function httpServerCorrectSizeResponse() {
|
|
|
|
const tmpFile = await Deno.makeTempFile();
|
|
|
|
const file = await Deno.open(tmpFile, { write: true, read: true });
|
|
|
|
await file.write(new Uint8Array(70 * 1024).fill(1)); // 70kb sent in 64kb + 6kb chunks
|
|
|
|
file.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
|
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-04-25 13:20:29 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2022-04-25 13:20:29 -04:00
|
|
|
const ev = await httpConn.nextRequest();
|
|
|
|
const { respondWith } = ev!;
|
|
|
|
const f = await Deno.open(tmpFile, { read: true });
|
|
|
|
await respondWith(new Response(f.readable, { status: 200 }));
|
|
|
|
})();
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`);
|
2022-04-25 13:20:29 -04:00
|
|
|
const body = await resp.arrayBuffer();
|
|
|
|
assertEquals(body.byteLength, 70 * 1024);
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2022-04-25 13:20:29 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-04-22 06:49:08 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, write: true, read: true } },
|
|
|
|
async function httpServerClosedStream() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-04-22 06:49:08 -04:00
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const client = await Deno.connect({ port: listenPort });
|
2022-04-22 06:49:08 -04:00
|
|
|
await client.write(new TextEncoder().encode(
|
|
|
|
`GET / HTTP/1.0\r\n\r\n`,
|
|
|
|
));
|
|
|
|
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const ev = await httpConn.nextRequest();
|
|
|
|
const { respondWith } = ev!;
|
|
|
|
|
|
|
|
const tmpFile = await Deno.makeTempFile();
|
|
|
|
const file = await Deno.open(tmpFile, { write: true, read: true });
|
|
|
|
await file.write(new TextEncoder().encode("hello"));
|
|
|
|
|
|
|
|
const reader = await file.readable.getReader();
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
assert(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
|
|
|
await respondWith(new Response(file.readable));
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(didThrow);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-04-22 06:49:08 -04:00
|
|
|
listener.close();
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-08-16 07:43:43 -04:00
|
|
|
// https://github.com/denoland/deno/issues/11595
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-16 07:43:43 -04:00
|
|
|
async function httpServerIncompleteMessage() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-08-16 07:43:43 -04:00
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const client = await Deno.connect({ port: listenPort });
|
2021-08-16 07:43:43 -04:00
|
|
|
await client.write(new TextEncoder().encode(
|
|
|
|
`GET / HTTP/1.0\r\n\r\n`,
|
|
|
|
));
|
|
|
|
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const ev = await httpConn.nextRequest();
|
|
|
|
const { respondWith } = ev!;
|
|
|
|
|
|
|
|
const errors: Error[] = [];
|
|
|
|
|
2022-02-16 08:27:38 -05:00
|
|
|
const readable = new ReadableStream({
|
|
|
|
async pull(controller) {
|
|
|
|
client.close();
|
2022-02-16 13:53:17 -05:00
|
|
|
await delay(1000);
|
2022-02-16 08:27:38 -05:00
|
|
|
controller.enqueue(new TextEncoder().encode(
|
|
|
|
"written to the writable side of a TransformStream",
|
|
|
|
));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
cancel(error) {
|
2021-08-16 07:43:43 -04:00
|
|
|
errors.push(error);
|
2022-02-16 08:27:38 -05:00
|
|
|
},
|
|
|
|
});
|
2021-08-16 07:43:43 -04:00
|
|
|
|
|
|
|
const res = new Response(readable);
|
|
|
|
|
2022-02-16 08:27:38 -05:00
|
|
|
await respondWith(res).catch((error: Error) => errors.push(error));
|
2021-08-16 07:43:43 -04:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-08-16 07:43:43 -04:00
|
|
|
listener.close();
|
|
|
|
|
2021-09-13 11:05:00 -04:00
|
|
|
assert(errors.length >= 1);
|
2021-08-16 07:43:43 -04:00
|
|
|
for (const error of errors) {
|
|
|
|
assertEquals(error.name, "Http");
|
2021-10-09 16:37:19 -04:00
|
|
|
assert(error.message.includes("connection"));
|
2021-08-16 07:43:43 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-08-23 10:15:59 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11743
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-23 10:15:59 -04:00
|
|
|
async function httpServerDoesntLeakResources() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-08-23 10:15:59 -04:00
|
|
|
const [conn, clientConn] = await Promise.all([
|
|
|
|
listener.accept(),
|
2023-07-31 09:29:17 -04:00
|
|
|
Deno.connect({ port: listenPort }),
|
2021-08-23 10:15:59 -04:00
|
|
|
]);
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
httpConn.nextRequest(),
|
|
|
|
clientConn.write(new TextEncoder().encode(
|
2023-07-31 09:29:17 -04:00
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:${listenPort}\r\n\r\n`,
|
2021-08-23 10:15:59 -04:00
|
|
|
)),
|
|
|
|
]);
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-08-23 10:15:59 -04:00
|
|
|
listener.close();
|
|
|
|
clientConn.close();
|
|
|
|
},
|
|
|
|
);
|
2021-09-08 04:12:23 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11926
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-09-08 04:12:23 -04:00
|
|
|
async function httpServerDoesntLeakResources2() {
|
|
|
|
let listener: Deno.Listener;
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
|
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
listener = Deno.listen({ port: listenPort });
|
2021-09-08 04:12:23 -04:00
|
|
|
for await (const conn of listener) {
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
for await (const { request, respondWith } of httpConn) {
|
2023-07-31 09:29:17 -04:00
|
|
|
assertEquals(
|
|
|
|
new URL(request.url).href,
|
|
|
|
`http://127.0.0.1:${listenPort}/`,
|
|
|
|
);
|
2021-09-08 04:12:23 -04:00
|
|
|
// not reading request body on purpose
|
|
|
|
respondWith(new Response("ok"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resourcesBefore = Deno.resources();
|
2023-07-31 09:29:17 -04:00
|
|
|
const response = await fetch(`http://127.0.0.1:${listenPort}`, {
|
2021-09-08 04:12:23 -04:00
|
|
|
method: "POST",
|
|
|
|
body: "hello world",
|
|
|
|
});
|
|
|
|
await response.text();
|
|
|
|
const resourcesAfter = Deno.resources();
|
|
|
|
// verify that the only new resource is "httpConnection", to make
|
|
|
|
// sure "request" resource is closed even if its body was not read
|
|
|
|
// by server handler
|
|
|
|
|
|
|
|
for (const rid of Object.keys(resourcesBefore)) {
|
|
|
|
delete resourcesAfter[Number(rid)];
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(Object.keys(resourcesAfter).length, 1);
|
|
|
|
|
|
|
|
listener!.close();
|
|
|
|
httpConn!.close();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-09-25 07:22:19 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/pull/12216
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-25 07:22:19 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function droppedConnSenderNoPanic() {
|
2021-10-09 16:37:19 -04:00
|
|
|
async function server() {
|
2023-11-17 18:19:00 -05:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-09-25 07:22:19 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
const http = Deno.serveHttp(conn);
|
2021-10-09 16:37:19 -04:00
|
|
|
const evt = await http.nextRequest();
|
|
|
|
http.close();
|
2021-09-25 07:22:19 -04:00
|
|
|
try {
|
2021-10-09 16:37:19 -04:00
|
|
|
await evt!.respondWith(new Response("boom"));
|
2021-09-25 07:22:19 -04:00
|
|
|
} catch {
|
2021-10-09 16:37:19 -04:00
|
|
|
// Ignore error.
|
2021-09-25 07:22:19 -04:00
|
|
|
}
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
2021-10-09 16:37:19 -04:00
|
|
|
try {
|
2023-11-17 18:19:00 -05:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`);
|
2021-10-09 16:37:19 -04:00
|
|
|
await resp.body?.cancel();
|
|
|
|
} catch {
|
|
|
|
// Ignore error
|
|
|
|
}
|
2021-09-25 07:22:19 -04:00
|
|
|
}
|
|
|
|
|
2021-10-09 16:37:19 -04:00
|
|
|
await Promise.all([server(), client()]);
|
2021-09-25 07:22:19 -04:00
|
|
|
},
|
|
|
|
);
|
2021-11-10 10:48:46 -05:00
|
|
|
|
2021-10-04 21:50:40 -04:00
|
|
|
// https://github.com/denoland/deno/issues/12193
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-04 21:50:40 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpConnConcurrentNextRequestCalls() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2021-10-04 21:50:40 -04:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
2021-10-04 21:50:40 -04:00
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2021-10-04 21:50:40 -04:00
|
|
|
const promises = new Array(10).fill(null).map(async (_, i) => {
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { pathname } = new URL(event.request.url);
|
|
|
|
assertStrictEquals(pathname, `/${i}`);
|
|
|
|
const response = new Response(`Response #${i}`);
|
|
|
|
await event.respondWith(response);
|
|
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
const response = await fetch(`http://${hostname}:${port}/${i}`);
|
|
|
|
const body = await response.text();
|
|
|
|
assertStrictEquals(body, `Response #${i}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), delay(100).then(client)]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2021-10-04 21:50:40 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// https://github.com/denoland/deno/pull/12704
|
|
|
|
// https://github.com/denoland/deno/pull/12732
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-04 21:50:40 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpConnAutoCloseDelayedOnUpgrade() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2021-10-04 21:50:40 -04:00
|
|
|
|
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(tcpConn);
|
|
|
|
|
|
|
|
const event1 = await httpConn.nextRequest() as Deno.RequestEvent;
|
|
|
|
const event2Promise = httpConn.nextRequest();
|
|
|
|
|
|
|
|
const { socket, response } = Deno.upgradeWebSocket(event1.request);
|
|
|
|
socket.onmessage = (event) => socket.send(event.data);
|
2021-12-07 12:50:35 -05:00
|
|
|
const socketClosed = new Promise<void>((resolve) => {
|
|
|
|
socket.onclose = () => resolve();
|
|
|
|
});
|
2021-10-04 21:50:40 -04:00
|
|
|
event1.respondWith(response);
|
|
|
|
|
|
|
|
const event2 = await event2Promise;
|
|
|
|
assertStrictEquals(event2, null);
|
|
|
|
|
|
|
|
listener.close();
|
2021-12-07 12:50:35 -05:00
|
|
|
await socketClosed;
|
2021-10-04 21:50:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const socket = new WebSocket(`ws://${hostname}:${port}/`);
|
|
|
|
socket.onopen = () => socket.send("bla bla");
|
2023-09-16 01:48:31 -04:00
|
|
|
const closed = new Promise<void>((resolve) => {
|
|
|
|
socket.onclose = () => resolve();
|
|
|
|
});
|
2022-09-19 17:00:01 -04:00
|
|
|
const { data } = await new Promise<MessageEvent<string>>((res) =>
|
|
|
|
socket.onmessage = res
|
|
|
|
);
|
2021-10-04 21:50:40 -04:00
|
|
|
assertStrictEquals(data, "bla bla");
|
|
|
|
socket.close();
|
2023-09-16 01:48:31 -04:00
|
|
|
await closed;
|
2021-10-04 21:50:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-12 14:01:45 -05:00
|
|
|
// https://github.com/denoland/deno/issues/12741
|
|
|
|
// https://github.com/denoland/deno/pull/12746
|
|
|
|
// https://github.com/denoland/deno/pull/12798
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-12 14:01:45 -05:00
|
|
|
{ permissions: { net: true, run: true } },
|
|
|
|
async function httpServerDeleteRequestHasBody() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2021-11-12 14:01:45 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
2021-11-12 14:01:45 -05:00
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2021-11-12 14:01:45 -05:00
|
|
|
const event = await httpConn.nextRequest() as Deno.RequestEvent;
|
|
|
|
assert(event.request.body);
|
|
|
|
const response = new Response();
|
|
|
|
await event.respondWith(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = ["-X", "DELETE", url];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stdout: "null",
|
|
|
|
stderr: "null",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2021-11-12 14:01:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
|
|
|
listener.close();
|
2021-11-12 14:01:45 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-10 10:48:46 -05:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerRespondNonAsciiUint8Array() {
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2021-11-10 10:48:46 -05:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2021-11-10 10:48:46 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.body, null);
|
|
|
|
await respondWith(
|
|
|
|
new Response(new Uint8Array([128]), {}),
|
|
|
|
);
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://localhost:${listenPort}/`);
|
2021-11-10 10:48:46 -05:00
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
const body = await resp.arrayBuffer();
|
|
|
|
assertEquals(new Uint8Array(body), new Uint8Array([128]));
|
|
|
|
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-11-10 10:48:46 -05:00
|
|
|
},
|
|
|
|
);
|
2022-01-02 08:04:40 -05:00
|
|
|
|
2022-10-23 18:45:45 -04:00
|
|
|
function tmpUnixSocketPath(): string {
|
|
|
|
const folder = Deno.makeTempDirSync();
|
|
|
|
return join(folder, "socket");
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:16:12 -05:00
|
|
|
// https://github.com/denoland/deno/pull/13628
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
|
|
|
async function httpServerOnUnixSocket() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2022-02-15 18:16:12 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-02-15 18:16:12 -05:00
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ path: filePath, transport: "unix" });
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(conn);
|
2022-03-22 18:08:29 -04:00
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
const url = new URL(request.url);
|
|
|
|
assertEquals(url.protocol, "http+unix:");
|
|
|
|
assertEquals(decodeURIComponent(url.host), filePath);
|
|
|
|
assertEquals(url.pathname, "/path/name");
|
|
|
|
await respondWith(new Response("", { headers: {} }));
|
2022-02-15 18:16:12 -05:00
|
|
|
})();
|
|
|
|
|
|
|
|
// fetch() does not supports unix domain sockets yet https://github.com/denoland/deno/issues/8821
|
|
|
|
const conn = await Deno.connect({ path: filePath, transport: "unix" });
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
// The Host header must be present and empty if it is not a Internet host name (RFC2616, Section 14.23)
|
|
|
|
const body = `GET /path/name HTTP/1.1\r\nHost:\r\n\r\n`;
|
|
|
|
const writeResult = await conn.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
|
|
|
|
const resp = new Uint8Array(200);
|
|
|
|
const readResult = await conn.read(resp);
|
2022-03-04 00:04:39 -05:00
|
|
|
assertEquals(readResult, 138);
|
2022-02-15 18:16:12 -05:00
|
|
|
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-02-15 18:16:12 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-03-04 00:04:39 -05:00
|
|
|
/* Automatic Body Compression */
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
Deno.test({
|
2022-04-21 03:07:49 -04:00
|
|
|
name: "http server compresses body - check headers",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-04-21 03:07:49 -04:00
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
|
|
|
|
const data = { hello: "deno", now: "with", compressed: "body" };
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
2022-04-21 03:07:49 -04:00
|
|
|
const response = new Response(JSON.stringify(data), {
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
});
|
2022-03-04 00:04:39 -05:00
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-04-21 03:07:49 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "http server compresses body - check body",
|
|
|
|
permissions: { net: true, run: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-04-21 03:07:49 -04:00
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
|
|
|
|
const data = { hello: "deno", now: "with", compressed: "body" };
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-04-21 03:07:49 -04:00
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-04-21 03:07:49 -04:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(JSON.stringify(data), {
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
});
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-21 03:07:49 -04:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-12 23:12:19 -05:00
|
|
|
const proc = new Deno.Command("curl", {
|
|
|
|
args,
|
|
|
|
stderr: "null",
|
|
|
|
stdout: "piped",
|
|
|
|
}).spawn();
|
2022-05-18 16:00:11 -04:00
|
|
|
const status = await proc.status;
|
2022-04-21 03:07:49 -04:00
|
|
|
assert(status.success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const stdout = proc.stdout
|
2022-04-21 03:07:49 -04:00
|
|
|
.pipeThrough(new DecompressionStream("gzip"))
|
|
|
|
.pipeThrough(new TextDecoderStream());
|
|
|
|
let body = "";
|
|
|
|
for await (const chunk of stdout) {
|
|
|
|
body += chunk;
|
|
|
|
}
|
|
|
|
assertEquals(JSON.parse(body), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-04-21 03:07:49 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-04 00:04:39 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "http server doesn't compress small body",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno" }),
|
|
|
|
{
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout).toLocaleLowerCase();
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: accept-encoding\r\n"));
|
|
|
|
assert(!output.includes("content-encoding: "));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server respects accept-encoding weights",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(
|
|
|
|
request.headers.get("Accept-Encoding"),
|
|
|
|
"gzip;q=0.8, br;q=1.0, *;q=0.1",
|
|
|
|
);
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
|
|
|
|
{
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip;q=0.8, br;q=1.0, *;q=0.1",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(output.includes("content-encoding: br\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server augments vary header",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
|
|
|
|
{
|
|
|
|
headers: { "content-type": "application/json", vary: "Accept" },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding, Accept\r\n"));
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server weakens etag header",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-type": "application/json",
|
|
|
|
etag: "33a64df551425fcc55e4d42a148795d9f25f89d4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-03-04 00:04:39 -05:00
|
|
|
"curl",
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(
|
|
|
|
output.includes("etag: W/33a64df551425fcc55e4d42a148795d9f25f89d4\r\n"),
|
|
|
|
);
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server passes through weak etag header",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-type": "application/json",
|
|
|
|
etag: "W/33a64df551425fcc55e4d42a148795d9f25f89d4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(
|
|
|
|
output.includes("etag: W/33a64df551425fcc55e4d42a148795d9f25f89d4\r\n"),
|
|
|
|
);
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server doesn't compress body when no-transform is set",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-type": "application/json",
|
|
|
|
"cache-control": "no-transform",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(!output.includes("content-encoding: "));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server doesn't compress body when content-range is set",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const response = new Response(
|
|
|
|
JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-type": "application/json",
|
|
|
|
"content-range": "bytes 200-100/67589",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-04 00:04:39 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(!output.includes("content-encoding: "));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2022-04-21 03:07:49 -04:00
|
|
|
name: "http server compresses streamed bodies - check headers",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-04 00:04:39 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-04 00:04:39 -05:00
|
|
|
|
2022-04-21 03:07:49 -04:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
|
|
|
|
const data = { hello: "deno", now: "with", compressed: "body" };
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-04 00:04:39 -05:00
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-04 00:04:39 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const bodyInit = new ReadableStream({
|
|
|
|
start(controller) {
|
2022-04-21 03:07:49 -04:00
|
|
|
controller.enqueue(encoder.encode(JSON.stringify(data)));
|
2022-03-04 00:04:39 -05:00
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const response = new Response(
|
|
|
|
bodyInit,
|
2022-04-21 03:07:49 -04:00
|
|
|
{ headers: { "content-type": "application/json" } },
|
2022-03-04 00:04:39 -05:00
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-03-04 00:04:39 -05:00
|
|
|
"curl",
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-04 00:04:39 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-04-21 03:07:49 -04:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-04-21 03:07:49 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "http server compresses streamed bodies - check body",
|
|
|
|
permissions: { net: true, run: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-04-21 03:07:49 -04:00
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
|
|
|
|
const data = { hello: "deno", now: "with", compressed: "body" };
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-04-21 03:07:49 -04:00
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-04-21 03:07:49 -04:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const bodyInit = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(encoder.encode(JSON.stringify(data)));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const response = new Response(
|
|
|
|
bodyInit,
|
|
|
|
{ headers: { "content-type": "application/json" } },
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-21 03:07:49 -04:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-12 23:12:19 -05:00
|
|
|
const proc = new Deno.Command("curl", {
|
|
|
|
args,
|
|
|
|
stderr: "null",
|
|
|
|
stdout: "piped",
|
|
|
|
}).spawn();
|
2022-05-18 16:00:11 -04:00
|
|
|
const status = await proc.status;
|
2022-04-21 03:07:49 -04:00
|
|
|
assert(status.success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const stdout = proc.stdout
|
2022-04-21 03:07:49 -04:00
|
|
|
.pipeThrough(new DecompressionStream("gzip"))
|
|
|
|
.pipeThrough(new TextDecoderStream());
|
|
|
|
let body = "";
|
|
|
|
for await (const chunk of stdout) {
|
|
|
|
body += chunk;
|
|
|
|
}
|
|
|
|
assertEquals(JSON.parse(body), data);
|
2022-03-04 00:04:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-04 00:04:39 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-07 12:13:15 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "http server updates content-length header if compression is applied",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { net: true, run: true },
|
2022-03-07 12:13:15 -05:00
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-03-07 12:13:15 -05:00
|
|
|
let contentLength: string;
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-03-07 12:13:15 -05:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-03-07 12:13:15 -05:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const body = JSON.stringify({
|
|
|
|
hello: "deno",
|
|
|
|
now: "with",
|
|
|
|
compressed: "body",
|
|
|
|
});
|
|
|
|
contentLength = String(body.length);
|
|
|
|
const response = new Response(
|
|
|
|
body,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-type": "application/json",
|
|
|
|
"content-length": contentLength,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
2022-05-18 16:00:11 -04:00
|
|
|
const args = [
|
2022-04-22 09:36:02 -04:00
|
|
|
"-i",
|
2022-03-07 12:13:15 -05:00
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
];
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args,
|
|
|
|
stderr: "null",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assert(success);
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = decoder.decode(stdout);
|
2022-03-07 12:13:15 -05:00
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
2023-08-05 17:43:16 -04:00
|
|
|
// Ensure the content-length header is updated (but don't check the exact length).
|
2022-03-07 12:13:15 -05:00
|
|
|
assert(!output.includes(`content-length: ${contentLength}\r\n`));
|
2023-08-05 17:43:16 -04:00
|
|
|
assert(output.includes("content-length: "));
|
2022-03-07 12:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-07 12:13:15 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:10:05 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "http server compresses when accept-encoding is deflate, gzip",
|
|
|
|
permissions: { net: true, run: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-05-13 08:10:05 -04:00
|
|
|
let contentLength: string;
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-05-13 08:10:05 -04:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-05-13 08:10:05 -04:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "deflate, gzip");
|
|
|
|
const body = "x".repeat(10000);
|
|
|
|
contentLength = String(body.length);
|
|
|
|
const response = new Response(
|
|
|
|
body,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-length": contentLength,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
|
|
|
const cmd = [
|
|
|
|
"curl",
|
|
|
|
"-i",
|
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
// "--compressed", // Windows curl does not support --compressed
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: deflate, gzip",
|
|
|
|
];
|
2023-04-26 22:52:52 -04:00
|
|
|
// deno-lint-ignore no-deprecated-deno-api
|
2022-05-13 08:10:05 -04:00
|
|
|
const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
|
|
|
const status = await proc.status();
|
|
|
|
assert(status.success);
|
|
|
|
const output = decoder.decode(await proc.output());
|
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(output.includes("content-encoding: gzip\r\n"));
|
|
|
|
// Ensure the content-length header is updated.
|
|
|
|
assert(!output.includes(`content-length: ${contentLength}\r\n`));
|
2023-08-05 17:43:16 -04:00
|
|
|
assert(output.includes("content-length: "));
|
2022-05-13 08:10:05 -04:00
|
|
|
proc.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-05-13 08:10:05 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-17 08:02:45 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "http server custom content-encoding is left untouched",
|
|
|
|
permissions: { net: true, run: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-05-17 08:02:45 -04:00
|
|
|
let contentLength: string;
|
|
|
|
|
2022-09-06 13:38:37 -04:00
|
|
|
let httpConn: Deno.HttpConn;
|
2022-05-17 08:02:45 -04:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const tcpConn = await listener.accept();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
2022-05-17 08:02:45 -04:00
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "deflate, gzip");
|
|
|
|
const body = new Uint8Array([3, 1, 4, 1]);
|
|
|
|
contentLength = String(body.length);
|
|
|
|
const response = new Response(
|
|
|
|
body,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"content-length": contentLength,
|
|
|
|
"content-encoding": "arbitrary",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await respondWith(response);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
|
|
|
const cmd = [
|
|
|
|
"curl",
|
|
|
|
"-i",
|
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
// "--compressed", // Windows curl does not support --compressed
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: deflate, gzip",
|
|
|
|
];
|
2023-04-26 22:52:52 -04:00
|
|
|
// deno-lint-ignore no-deprecated-deno-api
|
2022-05-17 08:02:45 -04:00
|
|
|
const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
|
|
|
const status = await proc.status();
|
|
|
|
assert(status.success);
|
|
|
|
const output = decoder.decode(await proc.output());
|
|
|
|
assert(output.includes("vary: Accept-Encoding\r\n"));
|
|
|
|
assert(output.includes("content-encoding: arbitrary\r\n"));
|
|
|
|
proc.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), client()]);
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-05-17 08:02:45 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-19 09:21:49 -04:00
|
|
|
Deno.test("upgradeHttp tcp", async () => {
|
2022-03-16 09:54:18 -04:00
|
|
|
async function client() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const tcpConn = await Deno.connect({ port: listenPort });
|
2022-03-16 09:54:18 -04:00
|
|
|
await tcpConn.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"CONNECT server.example.com:80 HTTP/1.1\r\n\r\nbla bla bla\nbla bla\nbla\n",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setTimeout(async () => {
|
|
|
|
await tcpConn.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"bla bla bla\nbla bla\nbla\n",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
tcpConn.close();
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
const abortController = new AbortController();
|
|
|
|
const signal = abortController.signal;
|
|
|
|
|
|
|
|
const server = serve((req) => {
|
|
|
|
const p = Deno.upgradeHttp(req);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const [conn, firstPacket] = await p;
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const firstPacketText = new TextDecoder().decode(firstPacket);
|
|
|
|
assertEquals(firstPacketText, "bla bla bla\nbla bla\nbla\n");
|
|
|
|
const n = await conn.read(buf);
|
|
|
|
assert(n != null);
|
|
|
|
const secondPacketText = new TextDecoder().decode(buf.slice(0, n));
|
|
|
|
assertEquals(secondPacketText, "bla bla bla\nbla bla\nbla\n");
|
|
|
|
abortController.abort();
|
|
|
|
conn.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
return new Response(null, { status: 101 });
|
2023-07-31 09:29:17 -04:00
|
|
|
}, { port: listenPort, signal });
|
2022-03-16 09:54:18 -04:00
|
|
|
|
|
|
|
await Promise.all([server, client()]);
|
|
|
|
});
|
|
|
|
|
2022-03-19 09:21:49 -04:00
|
|
|
Deno.test(
|
|
|
|
"upgradeHttp tls",
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
async () => {
|
|
|
|
async function client() {
|
|
|
|
const caCerts = [
|
|
|
|
await Deno.readTextFile("cli/tests/testdata/tls/RootCA.pem"),
|
|
|
|
];
|
|
|
|
const tlsConn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
2023-07-31 09:29:17 -04:00
|
|
|
port: listenPort,
|
2022-03-19 09:21:49 -04:00
|
|
|
caCerts,
|
|
|
|
});
|
|
|
|
await tlsConn.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"CONNECT server.example.com:80 HTTP/1.1\r\n\r\nbla bla bla\nbla bla\nbla\n",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setTimeout(async () => {
|
|
|
|
await tlsConn.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"bla bla bla\nbla bla\nbla\n",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
tlsConn.close();
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
const abortController = new AbortController();
|
|
|
|
const signal = abortController.signal;
|
|
|
|
const certFile = "cli/tests/testdata/tls/localhost.crt";
|
|
|
|
const keyFile = "cli/tests/testdata/tls/localhost.key";
|
|
|
|
|
|
|
|
const server = serveTls((req) => {
|
|
|
|
const p = Deno.upgradeHttp(req);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const [conn, firstPacket] = await p;
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const firstPacketText = new TextDecoder().decode(firstPacket);
|
|
|
|
assertEquals(firstPacketText, "bla bla bla\nbla bla\nbla\n");
|
|
|
|
const n = await conn.read(buf);
|
|
|
|
assert(n != null);
|
|
|
|
const secondPacketText = new TextDecoder().decode(buf.slice(0, n));
|
|
|
|
assertEquals(secondPacketText, "bla bla bla\nbla bla\nbla\n");
|
|
|
|
abortController.abort();
|
|
|
|
conn.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
return new Response(null, { status: 101 });
|
2023-07-31 09:29:17 -04:00
|
|
|
}, { hostname: "localhost", port: listenPort, signal, keyFile, certFile });
|
2022-03-19 09:21:49 -04:00
|
|
|
|
|
|
|
await Promise.all([server, client()]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test("upgradeHttp unix", {
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
}, async () => {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2022-03-19 09:21:49 -04:00
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const unixConn = await Deno.connect({ path: filePath, transport: "unix" });
|
|
|
|
await unixConn.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"CONNECT server.example.com:80 HTTP/1.1\r\n\r\nbla bla bla\nbla bla\nbla\n",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setTimeout(async () => {
|
|
|
|
await unixConn.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"bla bla bla\nbla bla\nbla\n",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
unixConn.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2022-03-19 09:21:49 -04:00
|
|
|
}, 500);
|
|
|
|
await promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = (async () => {
|
|
|
|
const listener = Deno.listen({ path: filePath, transport: "unix" });
|
2022-03-22 18:08:29 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
const p = Deno.upgradeHttp(request);
|
2022-03-19 09:21:49 -04:00
|
|
|
|
2022-03-22 18:08:29 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const [conn, firstPacket] = await p;
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const firstPacketText = new TextDecoder().decode(firstPacket);
|
|
|
|
assertEquals(firstPacketText, "bla bla bla\nbla bla\nbla\n");
|
|
|
|
const n = await conn.read(buf);
|
|
|
|
assert(n != null);
|
|
|
|
const secondPacketText = new TextDecoder().decode(buf.slice(0, n));
|
|
|
|
assertEquals(secondPacketText, "bla bla bla\nbla bla\nbla\n");
|
|
|
|
conn.close();
|
|
|
|
})();
|
2022-03-19 09:21:49 -04:00
|
|
|
|
2022-03-22 18:08:29 -04:00
|
|
|
const resp = new Response(null, { status: 101 });
|
|
|
|
await respondWith(resp);
|
|
|
|
await promise;
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2022-03-19 09:21:49 -04:00
|
|
|
})();
|
|
|
|
|
|
|
|
await Promise.all([server, client()]);
|
|
|
|
});
|
|
|
|
|
2022-10-04 09:48:50 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerReadLargeBodyWithContentLength() {
|
|
|
|
const TLS_PACKET_SIZE = 16 * 1024 + 256;
|
|
|
|
// We want the body to be read in multiple packets
|
|
|
|
const body = "aa\n" + "deno.land large body\n".repeat(TLS_PACKET_SIZE) +
|
|
|
|
"zz";
|
|
|
|
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-10-04 09:48:50 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
assertEquals(await request.text(), body);
|
|
|
|
await respondWith(new Response(body));
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2022-10-04 09:48:50 -04:00
|
|
|
method: "POST",
|
|
|
|
headers: { "connection": "close" },
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
const text = await resp.text();
|
|
|
|
assertEquals(text, body);
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
httpConn!.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerReadLargeBodyWithTransferChunked() {
|
|
|
|
const TLS_PACKET_SIZE = 16 * 1024 + 256;
|
|
|
|
|
|
|
|
// We want the body to be read in multiple packets
|
|
|
|
const chunks = [
|
|
|
|
"aa\n",
|
|
|
|
"deno.land large body\n".repeat(TLS_PACKET_SIZE),
|
|
|
|
"zz",
|
|
|
|
];
|
|
|
|
|
|
|
|
const body = chunks.join("");
|
|
|
|
|
|
|
|
const stream = new TransformStream();
|
|
|
|
const writer = stream.writable.getWriter();
|
|
|
|
for (const chunk of chunks) {
|
|
|
|
writer.write(new TextEncoder().encode(chunk));
|
|
|
|
}
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-10-04 09:48:50 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
assertEquals(await request.text(), body);
|
|
|
|
await respondWith(new Response(body));
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const resp = await fetch(`http://127.0.0.1:${listenPort}/`, {
|
2022-10-04 09:48:50 -04:00
|
|
|
method: "POST",
|
|
|
|
headers: { "connection": "close" },
|
|
|
|
body: stream.readable,
|
|
|
|
});
|
|
|
|
const text = await resp.text();
|
|
|
|
assertEquals(text, body);
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
httpConn!.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-10-17 22:28:27 -04:00
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true },
|
|
|
|
},
|
|
|
|
async function httpServerWithoutExclusiveAccessToTcp() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-10-17 22:28:27 -04:00
|
|
|
const listener = Deno.listen({ port });
|
|
|
|
|
|
|
|
const [clientConn, serverConn] = await Promise.all([
|
|
|
|
Deno.connect({ port }),
|
|
|
|
listener.accept(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const buf = new Uint8Array(128);
|
|
|
|
const readPromise = serverConn.read(buf);
|
|
|
|
assertThrows(() => Deno.serveHttp(serverConn), Deno.errors.BadResource);
|
|
|
|
|
|
|
|
clientConn.close();
|
|
|
|
listener.close();
|
|
|
|
await readPromise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true, read: true },
|
|
|
|
},
|
|
|
|
async function httpServerWithoutExclusiveAccessToTls() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-10-17 22:28:27 -04:00
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
|
|
|
});
|
|
|
|
|
|
|
|
const caCerts = [
|
|
|
|
await Deno.readTextFile("cli/tests/testdata/tls/RootCA.pem"),
|
|
|
|
];
|
|
|
|
const [clientConn, serverConn] = await Promise.all([
|
|
|
|
Deno.connectTls({ hostname, port, caCerts }),
|
|
|
|
listener.accept(),
|
|
|
|
]);
|
|
|
|
await Promise.all([clientConn.handshake(), serverConn.handshake()]);
|
|
|
|
|
|
|
|
const buf = new Uint8Array(128);
|
|
|
|
const readPromise = serverConn.read(buf);
|
|
|
|
assertThrows(() => Deno.serveHttp(serverConn), Deno.errors.BadResource);
|
|
|
|
|
|
|
|
clientConn.close();
|
|
|
|
listener.close();
|
|
|
|
await readPromise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
|
|
|
async function httpServerWithoutExclusiveAccessToUnixSocket() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2022-10-17 22:28:27 -04:00
|
|
|
const listener = Deno.listen({ path: filePath, transport: "unix" });
|
|
|
|
|
|
|
|
const [clientConn, serverConn] = await Promise.all([
|
|
|
|
Deno.connect({ path: filePath, transport: "unix" }),
|
|
|
|
listener.accept(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const buf = new Uint8Array(128);
|
|
|
|
const readPromise = serverConn.read(buf);
|
|
|
|
assertThrows(() => Deno.serveHttp(serverConn), Deno.errors.BadResource);
|
|
|
|
|
|
|
|
clientConn.close();
|
|
|
|
listener.close();
|
|
|
|
await readPromise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-10-24 07:13:20 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerRequestResponseClone() {
|
|
|
|
const body = "deno".repeat(64 * 1024);
|
|
|
|
let httpConn: Deno.HttpConn;
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2022-10-24 07:13:20 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
const reqEvent = await httpConn.nextRequest();
|
|
|
|
assert(reqEvent);
|
|
|
|
const { request, respondWith } = reqEvent;
|
|
|
|
const clone = request.clone();
|
|
|
|
const reader = clone.body!.getReader();
|
|
|
|
|
|
|
|
// get first chunk from branch2
|
|
|
|
const clonedChunks = [];
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
assert(!done);
|
|
|
|
clonedChunks.push(value);
|
|
|
|
|
|
|
|
// consume request after first chunk single read
|
|
|
|
// readAll should read correctly the rest of the body.
|
|
|
|
// firstChunk should be in the stream internal buffer
|
|
|
|
const body1 = await request.text();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
clonedChunks.push(value);
|
|
|
|
}
|
|
|
|
let offset = 0;
|
|
|
|
const body2 = new Uint8Array(body.length);
|
|
|
|
for (const chunk of clonedChunks) {
|
|
|
|
body2.set(chunk, offset);
|
|
|
|
offset += chunk.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(body1, body);
|
|
|
|
assertEquals(body1, new TextDecoder().decode(body2));
|
|
|
|
await respondWith(new Response(body));
|
|
|
|
})();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const response = await fetch(`http://localhost:${listenPort}`, {
|
2022-10-24 07:13:20 -04:00
|
|
|
body,
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
const clone = response.clone();
|
|
|
|
assertEquals(await response.text(), await clone.text());
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
httpConn!.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-11-04 13:59:07 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "http server compresses and flushes each chunk of a streamed resource",
|
|
|
|
permissions: { net: true, run: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
|
|
|
const port2 = listenPort2;
|
2022-11-04 13:59:07 -04:00
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const listener2 = Deno.listen({ hostname, port: port2 });
|
|
|
|
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
async function server() {
|
|
|
|
const tcpConn = await listener.accept();
|
|
|
|
httpConn = Deno.serveHttp(tcpConn);
|
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
|
|
|
|
const resp = await fetch(`http://${hostname}:${port2}/`);
|
|
|
|
await respondWith(resp);
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ts = new TransformStream();
|
|
|
|
const writer = ts.writable.getWriter();
|
|
|
|
writer.write(encoder.encode("hello"));
|
|
|
|
|
|
|
|
let httpConn2: Deno.HttpConn;
|
|
|
|
async function server2() {
|
|
|
|
const tcpConn = await listener2.accept();
|
|
|
|
httpConn2 = Deno.serveHttp(tcpConn);
|
|
|
|
const e = await httpConn2.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
await e.respondWith(
|
|
|
|
new Response(ts.readable, {
|
|
|
|
headers: { "Content-Type": "text/plain" },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
listener2.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
|
|
|
const url = `http://${hostname}:${port}/`;
|
|
|
|
const args = [
|
|
|
|
"--request",
|
|
|
|
"GET",
|
|
|
|
"--url",
|
|
|
|
url,
|
|
|
|
"--header",
|
|
|
|
"Accept-Encoding: gzip, deflate, br",
|
|
|
|
"--no-buffer",
|
|
|
|
];
|
2022-12-12 23:12:19 -05:00
|
|
|
const proc = new Deno.Command("curl", {
|
|
|
|
args,
|
|
|
|
stderr: "null",
|
|
|
|
stdout: "piped",
|
|
|
|
}).spawn();
|
2022-11-04 13:59:07 -04:00
|
|
|
const stdout = proc.stdout
|
|
|
|
.pipeThrough(new DecompressionStream("gzip"))
|
|
|
|
.pipeThrough(new TextDecoderStream());
|
|
|
|
let body = "";
|
|
|
|
for await (const chunk of stdout) {
|
|
|
|
body += chunk;
|
|
|
|
if (body === "hello") {
|
|
|
|
writer.write(encoder.encode(" world"));
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(body, "hello world");
|
|
|
|
const status = await proc.status;
|
|
|
|
assert(status.success);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), server2(), client()]);
|
|
|
|
httpConn!.close();
|
|
|
|
httpConn2!.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-02-12 15:51:07 -05:00
|
|
|
Deno.test("case insensitive comma value finder", async (t) => {
|
|
|
|
const cases = /** @type {[string, boolean][]} */ ([
|
|
|
|
["websocket", true],
|
|
|
|
["wEbSOcKET", true],
|
|
|
|
[",wEbSOcKET", true],
|
|
|
|
[",wEbSOcKET,", true],
|
|
|
|
[", wEbSOcKET ,", true],
|
|
|
|
["test, wEbSOcKET ,", true],
|
|
|
|
["test ,\twEbSOcKET\t\t ,", true],
|
|
|
|
["test , wEbSOcKET", true],
|
|
|
|
["test, asdf,web,wEbSOcKET", true],
|
|
|
|
["test, asdf,web,wEbSOcKETs", false],
|
|
|
|
["test, asdf,awebsocket,wEbSOcKETs", false],
|
|
|
|
]);
|
|
|
|
|
|
|
|
const findValue = buildCaseInsensitiveCommaValueFinder("websocket");
|
|
|
|
for (const [input, expected] of cases) {
|
|
|
|
await t.step(input.toString(), () => {
|
|
|
|
const actual = findValue(input);
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-20 03:46:45 -05:00
|
|
|
async function httpServerWithErrorBody(
|
|
|
|
listener: Deno.Listener,
|
|
|
|
compression: boolean,
|
|
|
|
): Promise<Deno.HttpConn> {
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { respondWith } = e;
|
|
|
|
const originalErr = new Error("boom");
|
|
|
|
const rs = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([65]));
|
|
|
|
await delay(1000);
|
|
|
|
controller.error(originalErr);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const init = compression ? { headers: { "content-type": "text/plain" } } : {};
|
|
|
|
const response = new Response(rs, init);
|
|
|
|
const err = await assertRejects(() => respondWith(response));
|
|
|
|
assert(err === originalErr);
|
|
|
|
return httpConn;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const compression of [true, false]) {
|
|
|
|
Deno.test({
|
|
|
|
name: `http server errors stream if response body errors (http/1.1${
|
|
|
|
compression ? " + compression" : ""
|
|
|
|
})`,
|
|
|
|
permissions: { net: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-12-20 03:46:45 -05:00
|
|
|
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const server = httpServerWithErrorBody(listener, compression);
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ hostname, port });
|
|
|
|
const msg = new TextEncoder().encode(
|
|
|
|
`GET / HTTP/1.1\r\nHost: ${hostname}:${port}\r\n\r\n`,
|
|
|
|
);
|
|
|
|
const nwritten = await conn.write(msg);
|
|
|
|
assertEquals(nwritten, msg.byteLength);
|
|
|
|
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const nread = await conn.read(buf);
|
|
|
|
assert(nread);
|
|
|
|
const data = new TextDecoder().decode(buf.subarray(0, nread));
|
|
|
|
assert(data.endsWith("1\r\nA\r\n"));
|
|
|
|
const nread2 = await conn.read(buf); // connection should be closed now because the stream errored
|
|
|
|
assertEquals(nread2, null);
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
const httpConn = await server;
|
|
|
|
httpConn.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: `http server errors stream if response body errors (http/1.1 + fetch${
|
|
|
|
compression ? " + compression" : ""
|
|
|
|
})`,
|
|
|
|
permissions: { net: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-12-20 03:46:45 -05:00
|
|
|
|
|
|
|
const listener = Deno.listen({ hostname, port });
|
|
|
|
const server = httpServerWithErrorBody(listener, compression);
|
|
|
|
|
|
|
|
const resp = await fetch(`http://${hostname}:${port}/`);
|
|
|
|
assert(resp.body);
|
|
|
|
const reader = resp.body.getReader();
|
|
|
|
const result = await reader.read();
|
|
|
|
assert(!result.done);
|
|
|
|
assertEquals(result.value, new Uint8Array([65]));
|
|
|
|
const err = await assertRejects(() => reader.read());
|
|
|
|
assert(err instanceof TypeError);
|
|
|
|
assert(err.message.includes("unexpected EOF"));
|
|
|
|
|
|
|
|
const httpConn = await server;
|
|
|
|
httpConn.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: `http server errors stream if response body errors (http/2 + fetch${
|
|
|
|
compression ? " + compression" : ""
|
|
|
|
}))`,
|
|
|
|
permissions: { net: true, read: true },
|
|
|
|
async fn() {
|
|
|
|
const hostname = "localhost";
|
2023-07-31 09:29:17 -04:00
|
|
|
const port = listenPort;
|
2022-12-20 03:46:45 -05:00
|
|
|
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
|
|
|
alpnProtocols: ["h2"],
|
|
|
|
});
|
|
|
|
const server = httpServerWithErrorBody(listener, compression);
|
|
|
|
|
|
|
|
const caCert = Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
|
|
|
const resp = await fetch(`https://${hostname}:${port}/`, { client });
|
|
|
|
client.close();
|
|
|
|
assert(resp.body);
|
|
|
|
const reader = resp.body.getReader();
|
|
|
|
const result = await reader.read();
|
|
|
|
assert(!result.done);
|
|
|
|
assertEquals(result.value, new Uint8Array([65]));
|
|
|
|
const err = await assertRejects(() => reader.read());
|
|
|
|
assert(err instanceof TypeError);
|
|
|
|
assert(err.message.includes("unexpected internal error encountered"));
|
|
|
|
|
|
|
|
const httpConn = await server;
|
|
|
|
httpConn.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-15 18:37:06 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "request signal is aborted when response errors",
|
|
|
|
permissions: { net: true },
|
|
|
|
async fn() {
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
const promise = (async () => {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ port: listenPort });
|
2023-03-15 18:37:06 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
const ev = await httpConn.nextRequest();
|
|
|
|
const { request, respondWith } = ev!;
|
|
|
|
|
|
|
|
await delay(300);
|
|
|
|
await assertRejects(() => respondWith(new Response("Hello World")));
|
|
|
|
assert(request.signal.aborted);
|
|
|
|
})();
|
|
|
|
|
|
|
|
const abortController = new AbortController();
|
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
fetch(`http://127.0.0.1:${listenPort}/`, {
|
2023-03-15 18:37:06 -04:00
|
|
|
signal: abortController.signal,
|
|
|
|
}).catch(() => {
|
|
|
|
// ignore
|
|
|
|
});
|
|
|
|
|
|
|
|
await delay(100);
|
|
|
|
abortController.abort();
|
|
|
|
await promise;
|
|
|
|
httpConn!.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-01 15:26:12 -04:00
|
|
|
Deno.test(
|
|
|
|
async function httpConnExplicitResourceManagement() {
|
|
|
|
let promise;
|
|
|
|
|
|
|
|
{
|
|
|
|
const listen = Deno.listen({ port: listenPort });
|
|
|
|
promise = fetch(`http://localhost:${listenPort}/`).catch(() => null);
|
|
|
|
const serverConn = await listen.accept();
|
|
|
|
listen.close();
|
|
|
|
|
|
|
|
using _httpConn = Deno.serveHttp(serverConn);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await promise;
|
|
|
|
assertEquals(response, null);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-02 08:04:40 -05:00
|
|
|
function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader {
|
|
|
|
// Based on https://tools.ietf.org/html/rfc2616#section-19.4.6
|
|
|
|
const tp = new TextProtoReader(r);
|
|
|
|
let finished = false;
|
|
|
|
const chunks: Array<{
|
|
|
|
offset: number;
|
|
|
|
data: Uint8Array;
|
|
|
|
}> = [];
|
|
|
|
async function read(buf: Uint8Array): Promise<number | null> {
|
|
|
|
if (finished) return null;
|
|
|
|
const [chunk] = chunks;
|
|
|
|
if (chunk) {
|
|
|
|
const chunkRemaining = chunk.data.byteLength - chunk.offset;
|
|
|
|
const readLength = Math.min(chunkRemaining, buf.byteLength);
|
|
|
|
for (let i = 0; i < readLength; i++) {
|
|
|
|
buf[i] = chunk.data[chunk.offset + i];
|
|
|
|
}
|
|
|
|
chunk.offset += readLength;
|
|
|
|
if (chunk.offset === chunk.data.byteLength) {
|
|
|
|
chunks.shift();
|
|
|
|
// Consume \r\n;
|
|
|
|
if ((await tp.readLine()) === null) {
|
|
|
|
throw new Deno.errors.UnexpectedEof();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readLength;
|
|
|
|
}
|
|
|
|
const line = await tp.readLine();
|
|
|
|
if (line === null) throw new Deno.errors.UnexpectedEof();
|
|
|
|
// TODO(bartlomieju): handle chunk extension
|
|
|
|
const [chunkSizeString] = line.split(";");
|
|
|
|
const chunkSize = parseInt(chunkSizeString, 16);
|
|
|
|
if (Number.isNaN(chunkSize) || chunkSize < 0) {
|
|
|
|
throw new Deno.errors.InvalidData("Invalid chunk size");
|
|
|
|
}
|
|
|
|
if (chunkSize > 0) {
|
|
|
|
if (chunkSize > buf.byteLength) {
|
|
|
|
let eof = await r.readFull(buf);
|
|
|
|
if (eof === null) {
|
|
|
|
throw new Deno.errors.UnexpectedEof();
|
|
|
|
}
|
|
|
|
const restChunk = new Uint8Array(chunkSize - buf.byteLength);
|
|
|
|
eof = await r.readFull(restChunk);
|
|
|
|
if (eof === null) {
|
|
|
|
throw new Deno.errors.UnexpectedEof();
|
|
|
|
} else {
|
|
|
|
chunks.push({
|
|
|
|
offset: 0,
|
|
|
|
data: restChunk,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return buf.byteLength;
|
|
|
|
} else {
|
|
|
|
const bufToFill = buf.subarray(0, chunkSize);
|
|
|
|
const eof = await r.readFull(bufToFill);
|
|
|
|
if (eof === null) {
|
|
|
|
throw new Deno.errors.UnexpectedEof();
|
|
|
|
}
|
|
|
|
// Consume \r\n
|
|
|
|
if ((await tp.readLine()) === null) {
|
|
|
|
throw new Deno.errors.UnexpectedEof();
|
|
|
|
}
|
|
|
|
return chunkSize;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(chunkSize === 0);
|
|
|
|
// Consume \r\n
|
|
|
|
if ((await r.readLine()) === null) {
|
|
|
|
throw new Deno.errors.UnexpectedEof();
|
|
|
|
}
|
|
|
|
await readTrailers(h, r);
|
|
|
|
finished = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { read };
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readTrailers(
|
|
|
|
headers: Headers,
|
|
|
|
r: BufReader,
|
|
|
|
) {
|
|
|
|
const trailers = parseTrailer(headers.get("trailer"));
|
|
|
|
if (trailers == null) return;
|
|
|
|
const trailerNames = [...trailers.keys()];
|
|
|
|
const tp = new TextProtoReader(r);
|
2022-10-17 18:57:31 -04:00
|
|
|
const result = await tp.readMimeHeader();
|
2022-01-02 08:04:40 -05:00
|
|
|
if (result == null) {
|
|
|
|
throw new Deno.errors.InvalidData("Missing trailer header.");
|
|
|
|
}
|
|
|
|
const undeclared = [...result.keys()].filter(
|
|
|
|
(k) => !trailerNames.includes(k),
|
|
|
|
);
|
|
|
|
if (undeclared.length > 0) {
|
|
|
|
throw new Deno.errors.InvalidData(
|
|
|
|
`Undeclared trailers: ${Deno.inspect(undeclared)}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const [k, v] of result) {
|
|
|
|
headers.append(k, v);
|
|
|
|
}
|
|
|
|
const missingTrailers = trailerNames.filter((k) => !result.has(k));
|
|
|
|
if (missingTrailers.length > 0) {
|
|
|
|
throw new Deno.errors.InvalidData(
|
|
|
|
`Missing trailers: ${Deno.inspect(missingTrailers)}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
headers.delete("trailer");
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseTrailer(field: string | null): Headers | undefined {
|
|
|
|
if (field == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const trailerNames = field.split(",").map((v) => v.trim().toLowerCase());
|
|
|
|
if (trailerNames.length === 0) {
|
|
|
|
throw new Deno.errors.InvalidData("Empty trailer header.");
|
|
|
|
}
|
|
|
|
const prohibited = trailerNames.filter((k) => isProhibitedForTrailer(k));
|
|
|
|
if (prohibited.length > 0) {
|
|
|
|
throw new Deno.errors.InvalidData(
|
|
|
|
`Prohibited trailer names: ${Deno.inspect(prohibited)}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return new Headers(trailerNames.map((key) => [key, ""]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function isProhibitedForTrailer(key: string): boolean {
|
|
|
|
const s = new Set(["transfer-encoding", "content-length", "trailer"]);
|
|
|
|
return s.has(key.toLowerCase());
|
|
|
|
}
|