This commit is contained in:
Ryan Dahl 2018-11-07 01:08:51 -05:00
commit 60735e1804
2 changed files with 46 additions and 0 deletions

42
http.ts Normal file
View File

@ -0,0 +1,42 @@
import * as deno from "deno";
class Server {
_closing = false;
constructor(readonly listener: deno.Listener) {}
async serveConn(conn: deno.Conn) {
const buffer = new Uint8Array(1024);
try {
while (true) {
const r = await conn.read(buffer);
if (r.eof) {
break;
}
const response = new TextEncoder().encode(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
);
await conn.write(response);
}
} finally {
conn.close();
}
}
async serve() {
while (!this._closing) {
const conn = await this.listener.accept();
this.serveConn(conn);
}
}
close() {
this._closing = true;
}
}
export function listen(addr: string): Server {
const listener = deno.listen("tcp", addr);
const s = new Server(listener);
return s;
}

4
http_test.ts Normal file
View File

@ -0,0 +1,4 @@
import { listen } from "./server.ts";
const s = listen("0.0.0.0:4500");
s.serve();