2018-10-15 16:44:35 -04:00
|
|
|
// Used for benchmarking Deno's networking. See tools/http_benchmark.py
|
|
|
|
// TODO Replace this with a real HTTP server once
|
|
|
|
// https://github.com/denoland/deno/issues/726 is completed.
|
2018-10-23 18:02:30 -04:00
|
|
|
// Note: this is a keep-alive server.
|
2020-01-09 13:37:01 -05:00
|
|
|
const addr = Deno.args[0] || "127.0.0.1:4500";
|
2019-09-20 18:32:18 -04:00
|
|
|
const [hostname, port] = addr.split(":");
|
|
|
|
const listener = Deno.listen({ hostname, port: Number(port) });
|
2018-10-15 16:44:35 -04:00
|
|
|
const response = new TextEncoder().encode(
|
2020-07-14 15:24:17 -04:00
|
|
|
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
|
2018-10-15 16:44:35 -04:00
|
|
|
);
|
2019-02-12 10:08:56 -05:00
|
|
|
async function handle(conn: Deno.Conn): Promise<void> {
|
2018-10-15 16:44:35 -04:00
|
|
|
const buffer = new Uint8Array(1024);
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
const r = await conn.read(buffer);
|
2020-04-28 12:40:43 -04:00
|
|
|
if (r === null) {
|
2018-10-15 16:44:35 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
await conn.write(response);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:58:35 -04:00
|
|
|
console.log("Listening on", addr);
|
|
|
|
for await (const conn of listener) {
|
|
|
|
handle(conn);
|
2018-10-15 16:44:35 -04:00
|
|
|
}
|