summaryrefslogtreecommitdiff
path: root/lib/http/server.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-04-13 00:17:34 -0700
committerOri Bernstein <ori@eigenstate.org>2017-04-13 00:17:57 -0700
commitcdb2435ea25e9752a0b06f72fd699c46185bb7d1 (patch)
treee1e5fe2cfbd3e563ead0501fad300ddfd6e0abe0 /lib/http/server.myr
parentc492d277f96911cc6eecb0e9de72dbbfdf9f45e7 (diff)
downloadmc-cdb2435ea25e9752a0b06f72fd699c46185bb7d1.tar.gz
start of server.
Diffstat (limited to 'lib/http/server.myr')
-rw-r--r--lib/http/server.myr78
1 files changed, 72 insertions, 6 deletions
diff --git a/lib/http/server.myr b/lib/http/server.myr
index 6e1aa4c..220f56d 100644
--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -1,15 +1,15 @@
+use bio
use std
+use thread
use "types"
+use "session"
+use "parse"
pkg http =
const announce : (ds : byte[:] -> std.result(server#, err))
const shutdown : (srv : server# -> void)
-
- const waitconn : (srv : server# -> std.result(std.fd, err))
-// const readmsg : (srv : server# -> std.option(req#, err))
-// const writemsg : (srv : server# -> std.option(req#, err))
-// const writehdr : (srv : server# -> std.option(req#, err))
+ const serve : (srv : server# -> void)
;;
const announce = {ds
@@ -28,6 +28,72 @@ const announce = {ds
;;
}
+const serve = {srv
+ std.put("waiting for connection\n")
+ while true
+ match waitconn(srv)
+ | `std.Ok fd: communicate(srv, fd)
+ | `std.Err e: /* eh? */
+ ;;
+ ;;
+}
+
+const communicate = {srv, fd
+ var s
+
+ s = mksrvsession(fd)
+ while !srv.quit
+ match parsereq(s)
+ | `std.Ok req:
+ dispatch(srv, s, req)
+ | `std.Err e:
+ std.put("failed to parse request: {}\n", e)
+ break
+ ;;
+ ;;
+ std.close(fd)
+}
+
+const dispatch = {srv, sess, req
+ var resp : resp#
+
+ std.put("got req: {}\n", req)
+ resp = std.mk([
+ .status=200,
+ .hdrs = [][:],
+ .len = 0,
+ .err = `std.None,
+ .reason = "",
+ .body = "heard you loud and clear\n",
+ .enc = `Length
+ ])
+ respond(srv, sess, resp)
+}
+
+const respond = {srv, s, resp
+ var sb
+
+ sb = std.mksb()
+ bio.put(s.f, "HTTP/1.1 {} {}\r\n", resp.status, statusstr(resp.status))
+ bio.put(s.f, "Content-Length: {}\r\n", resp.body.len)
+ bio.put(s.f, "Encoding: {}\r\n", resp.enc)
+ for (k, v) in resp.hdrs
+ bio.put(s.f, "{}: {}\r\n", k, v)
+ ;;
+ bio.put(s.f, "\r\n")
+ bio.write(s.f, resp.body)
+ bio.flush(s.f)
+}
+
+const statusstr = {st
+ match st
+ | 200: -> "OK"
+ | 404: -> "Not Found"
+ | 503: -> "Internal Error"
+ | _: -> "Bad State"
+ ;;
+}
+
const shutdown = {srv
std.close(srv.lfd)
}
@@ -35,7 +101,7 @@ const shutdown = {srv
const waitconn = {srv
match std.accept(srv.lfd)
- | `std.Ok afd: -> `std.Ok afd
+ | `std.Ok fd: -> `std.Ok fd
| `std.Err e: -> `std.Err `Econn
;;
}