summaryrefslogtreecommitdiff
path: root/lib/http/server.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-04-22 19:57:30 -0700
committerOri Bernstein <ori@eigenstate.org>2017-04-22 19:57:30 -0700
commit3d956b3adc6dc8e32cf17e2baa0a2408295ae12f (patch)
treeee3f99ce0e053f014741b022fc92a59ff83d68e8 /lib/http/server.myr
parentf37c8970f11e21b0b6f29c4aac10f92101d0c906 (diff)
downloadmc-3d956b3adc6dc8e32cf17e2baa0a2408295ae12f.tar.gz
Working demo server.
Diffstat (limited to 'lib/http/server.myr')
-rw-r--r--lib/http/server.myr30
1 files changed, 7 insertions, 23 deletions
diff --git a/lib/http/server.myr b/lib/http/server.myr
index 34c0eef..a892b1c 100644
--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -9,7 +9,8 @@ use "parse"
pkg http =
const announce : (ds : byte[:] -> std.result(server#, err))
const shutdown : (srv : server# -> void)
- const serve : (srv : server# -> void)
+ const serve : (srv : server#, fn : (srv : server#, s : session#, req : req# -> void) -> void)
+ const respond : (srv : server#, sess : session#, resp : resp# -> void)
;;
const announce = {ds
@@ -28,46 +29,29 @@ const announce = {ds
;;
}
-const serve = {srv
+const serve = {srv, fn
while true
match waitconn(srv)
| `std.Ok fd:
- thread.spawn({;communicate(srv, fd)})
+ thread.spawn({;communicate(srv, fd, fn)})
| `std.Err e: /* eh? */
;;
;;
}
-const communicate = {srv, fd
+const communicate = {srv, fd, fn
var s
s = mksrvsession(fd)
while !srv.quit
match parsereq(s)
- | `std.Ok req:
- dispatch(srv, s, req)
- | `std.Err e:
- break
+ | `std.Ok req: fn(srv, s, req)
+ | `std.Err e: break
;;
;;
std.close(fd)
}
-const dispatch = {srv, sess, req
- var resp : resp#
-
- resp = std.mk([
- .status=200,
- .hdrs = [][:],
- .len = 0,
- .err = `std.None,
- .reason = "",
- .body = "pong\n",
- .enc = `Length
- ])
- respond(srv, sess, resp)
-}
-
const respond = {srv, s, resp
var sb