commit 0d048b8704c0709506d3d14a26d540a4b8eb470e Author: Ryan Dahl Date: Wed Nov 7 01:08:51 2018 -0500 Init Original: https://github.com/denoland/deno_std/commit/60735e18043b54f51cf794ebe152459d5e066e6d diff --git a/http.ts b/http.ts new file mode 100644 index 0000000000..d4c496e152 --- /dev/null +++ b/http.ts @@ -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; +} diff --git a/http_test.ts b/http_test.ts new file mode 100644 index 0000000000..32c868ad5f --- /dev/null +++ b/http_test.ts @@ -0,0 +1,4 @@ +import { listen } from "./server.ts"; + +const s = listen("0.0.0.0:4500"); +s.serve();