2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-08-28 13:03:50 +00:00
|
|
|
// Used for benchmarking Deno's networking.
|
2021-01-16 23:32:59 +00:00
|
|
|
// TODO(bartlomieju): Replace this with a real HTTP server once
|
2018-10-15 20:44:35 +00:00
|
|
|
// https://github.com/denoland/deno/issues/726 is completed.
|
2018-10-23 22:02:30 +00:00
|
|
|
// Note: this is a keep-alive server.
|
2020-01-09 18:37:01 +00:00
|
|
|
const addr = Deno.args[0] || "127.0.0.1:4500";
|
2019-09-20 22:32:18 +00:00
|
|
|
const [hostname, port] = addr.split(":");
|
|
|
|
const listener = Deno.listen({ hostname, port: Number(port) });
|
2018-10-15 20:44:35 +00:00
|
|
|
const response = new TextEncoder().encode(
|
2020-07-14 19:24:17 +00:00
|
|
|
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
|
2018-10-15 20:44:35 +00:00
|
|
|
);
|
2019-02-12 15:08:56 +00:00
|
|
|
async function handle(conn: Deno.Conn): Promise<void> {
|
2018-10-15 20:44:35 +00:00
|
|
|
const buffer = new Uint8Array(1024);
|
|
|
|
try {
|
|
|
|
while (true) {
|
2021-04-08 16:04:02 +00:00
|
|
|
await conn.read(buffer);
|
2018-10-15 20:44:35 +00:00
|
|
|
await conn.write(response);
|
|
|
|
}
|
2021-04-08 16:04:02 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (
|
|
|
|
!(e instanceof Deno.errors.BrokenPipe) &&
|
|
|
|
!(e instanceof Deno.errors.ConnectionReset)
|
|
|
|
) {
|
|
|
|
throw e;
|
|
|
|
}
|
2018-10-15 20:44:35 +00:00
|
|
|
}
|
2021-04-08 16:04:02 +00:00
|
|
|
conn.close();
|
2018-10-15 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 19:58:35 +00:00
|
|
|
console.log("Listening on", addr);
|
|
|
|
for await (const conn of listener) {
|
|
|
|
handle(conn);
|
2018-10-15 20:44:35 +00:00
|
|
|
}
|