summaryrefslogtreecommitdiff
path: root/http.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-11-07 13:16:07 -0500
committerRyan Dahl <ry@tinyclouds.org>2018-11-07 13:16:07 -0500
commit8610e3578c923be2b7d758e75ea370801abf8574 (patch)
tree605c8b5f400c6a0daf29282250573816341e2cef /http.ts
parentabe47d10c97f5cff671d3565a5a985c2ef203d4d (diff)
First pass at bufio.
Original: https://github.com/denoland/deno_std/commit/c5cc6959705c310f4f7a864d77aae54171707c04
Diffstat (limited to 'http.ts')
-rw-r--r--http.ts62
1 files changed, 46 insertions, 16 deletions
diff --git a/http.ts b/http.ts
index d4c496e15..c8b7d8d90 100644
--- a/http.ts
+++ b/http.ts
@@ -1,38 +1,67 @@
import * as deno from "deno";
+import * as bufio from "./bufio.ts";
+import { TextProtoReader } from "./textproto.ts";
+
+type Handler = (req: ServerRequest) => Promise<void>;
class Server {
_closing = false;
constructor(readonly listener: deno.Listener) {}
- async serveConn(conn: deno.Conn) {
+ async serve(handler: Handler) {
+ while (!this._closing) {
+ const c = await this.listener.accept();
+ const sc = new ServerConn(c);
+ sc.serve(handler);
+ }
+ }
+
+ close() {
+ this._closing = true;
+ this.listener.close();
+ }
+}
+
+class ServerConn {
+ constructor(readonly c: deno.Conn) {
+ // TODO Use memory pool to obtain bufr and bufw.
+ this.bufr = new bufio.Reader(c);
+ this.bufw = new bufio.Writer(c);
+ }
+
+ async serve(handler: Handler): Promise<void> {
const buffer = new Uint8Array(1024);
try {
while (true) {
- const r = await conn.read(buffer);
- if (r.eof) {
- break;
- }
+ const req = readRequest(this.bufr);
+
+ /*
const response = new TextEncoder().encode(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
);
- await conn.write(response);
+ await this.c.write(response);
+ */
}
} finally {
- conn.close();
+ this.c.close();
}
}
+}
- async serve() {
- while (!this._closing) {
- const conn = await this.listener.accept();
- this.serveConn(conn);
- }
- }
+function readRequest(b: bufio.Reader): ServerRequest {
+ const tp = new TextProtoReader(b);
+ const req = new ServerRequest();
- close() {
- this._closing = true;
- }
+ // First line: GET /index.html HTTP/1.0
+ const s = await tp.readLine();
+ const [ method, url, proto ] = parseRequestLine(s);
+ console.log("readRequest", method, url);
+}
+
+// Returns [method, url, proto]
+function parseRequestLine(line: string): [ string, string, string ] {
+ return line.split(" ", 3);
}
export function listen(addr: string): Server {
@@ -40,3 +69,4 @@ export function listen(addr: string): Server {
const s = new Server(listener);
return s;
}
+