ref: f6e16f0c9f8d68a66c279059e4185d79a5b665a0
parent: 1d4f6a7925c05705dda39c9fe799c64138d6d02a
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Aug 27 12:11:12 EDT 2016
Update to new APIs.
--- a/lib/http/bld.sub
+++ b/lib/http/bld.sub
@@ -1,15 +1,21 @@
bin h =
- main.myr
+ h.myr
lib http
;;
+bin srvdot =
+ srvdot.myr
+
+ lib http
+;;
+
lib http =
parse.myr
types.myr
url.myr
client.myr
+ router.myr
server.myr
session.myr
-
;;
--- a/lib/http/client.myr
+++ b/lib/http/client.myr
@@ -39,7 +39,7 @@
const getreq = {s, r
match request(s, `Get, r, `std.None)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, true)
@@ -48,7 +48,7 @@
const headreq = {s, r
match request(s, `Head, r, `std.None)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, false)
@@ -57,7 +57,7 @@
const putreq = {s, r, data
match request(s, `Put, r, `std.Some data)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, true)
@@ -66,7 +66,7 @@
const postreq = {s, r, data
match request(s, `Post, r, `std.Some data)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, true)
@@ -75,7 +75,7 @@
const deletereq = {s, r
match request(s, `Delete, r, `std.None)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, true)
@@ -84,7 +84,7 @@
const optionsreq = {s, r
match request(s, `Options, r, `std.None)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, true)
@@ -93,7 +93,7 @@
const tracereq = {s, r
match request(s, `Trace, r, `std.None)
| `std.Ok _: /* nothing */
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
-> response(s, true)
@@ -117,13 +117,13 @@
else
match readbody(s, resp)
| `std.Ok buf: resp.body = buf
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
;;
else
match resp.err
- | `std.Some e: -> `std.Fail e
- | `std.None: -> `std.Fail `Ewat
+ | `std.Some e: -> `std.Err e
+ | `std.None: -> `std.Err `Ewat
;;
;;
-> `std.Ok resp
@@ -155,7 +155,7 @@
ioflush(s)
if s.err
- -> `std.Fail `Econn
+ -> `std.Err `Econn
else
-> `std.Ok void
;;
@@ -190,7 +190,7 @@
-> `std.Ok buf
:shortread
std.slfree(buf)
- -> `std.Fail `Eshort
+ -> `std.Err `Eshort
}
const __init__ = {
@@ -207,9 +207,9 @@
len = 0
while true
match parsechunksz(s)
- | `std.Fail e:
+ | `std.Err e:
std.slfree(buf)
- -> `std.Fail e
+ -> `std.Err e
| `std.Ok 0:
break
| `std.Ok sz:
@@ -217,21 +217,21 @@
match bio.read(s.f, buf[len:len + sz])
| `bio.Eof:
std.slfree(buf)
- -> `std.Fail `Eshort
+ -> `std.Err `Eshort
| `bio.Err e:
std.slfree(buf)
- -> `std.Fail `Econn
+ -> `std.Err `Econn
| `bio.Ok str:
if str.len != sz
std.slfree(buf)
- -> `std.Fail `Eshort
+ -> `std.Err `Eshort
;;
len += sz
match checkendln(s)
| `std.Ok _: /* nothing */
- | `std.Fail e:
+ | `std.Err e:
std.slfree(buf)
- -> `std.Fail e
+ -> `std.Err e
;;
;;
;;
@@ -243,13 +243,13 @@
var r
match bio.readln(s.f)
- | `bio.Err e: r = `std.Fail `Econn
- | `bio.Eof: r = `std.Fail `Econn
+ | `bio.Err e: r = `std.Err `Econn
+ | `bio.Eof: r = `std.Err `Econn
| `bio.Ok crlf:
if std.strstrip(crlf).len == 0
r = `std.Ok void
else
- r = `std.Fail `Eproto
+ r = `std.Err `Eproto
;;
std.slfree(crlf)
;;
--- a/lib/http/main.myr
+++ b/lib/http/main.myr
@@ -68,7 +68,7 @@
std.fblat(std.Out, resp.body)
;;
http.freeresp(resp)
- | `std.Fail e:
+ | `std.Err e:
std.put("{}\n", e)
;;
http.urlfree(u)
--- a/lib/http/parse.myr
+++ b/lib/http/parse.myr
@@ -84,7 +84,7 @@
;;
| `std.Ok enc:
r.enc = enc
- | `std.Fail e:
+ | `std.Err e:
r.err = `std.Some e
-> false
;;
@@ -96,14 +96,14 @@
var ret, str
match bio.readln(s.f)
- | `bio.Eof: ret = `std.Fail `Econn
- | `bio.Err e: ret = `std.Fail `Econn
+ | `bio.Eof: ret = `std.Err `Econn
+ | `bio.Err e: ret = `std.Err `Econn
| `bio.Ok ln:
str = ln
match parsenumber(&str, 16)
| `std.Some n: ret = `std.Ok (n : std.size)
| `std.None:
- ret = `std.Fail `Eproto
+ ret = `std.Err `Eproto
;;
std.slfree(ln)
;;
@@ -170,7 +170,7 @@
| `std.Some "compress": -> `std.Ok `Compress
| `std.Some "deflate": -> `std.Ok `Deflate
| `std.Some "gzip": -> `std.Ok `Gzip
- | `std.Some unknown: -> `std.Fail `Eenc
+ | `std.Some unknown: -> `std.Err `Eenc
;;
}
--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -1,9 +1,42 @@
use std
+use "types"
+
pkg http =
- const announce : (ds : byte[:] -> void)
+ 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 announce = {ds
- std.fatal("announce not yet implemented\n")
+ var afd
+
+ match std.announce(ds)
+ | `std.Ok f: afd = f
+ | `std.Err e: -> `std.Err `Econn
+ ;;
+
+ match std.listen(afd)
+ | `std.Err e: -> `std.Err `Econn
+ | `std.Ok lfd:
+ std.close(afd)
+ -> `std.Ok std.mk([.lfd=lfd])
+ ;;
}
+
+const shutdown = {srv
+ std.close(srv.lfd)
+}
+
+
+const waitconn = {srv
+ match std.accept(srv.lfd)
+ | `std.Ok afd: -> `std.Ok afd
+ | `std.Err e: -> `std.Err `Econn
+ ;;
+}
+
--- a/lib/http/session.myr
+++ b/lib/http/session.myr
@@ -25,7 +25,7 @@
s = std.fmt("tcp!{}!{}", host, port)
match std.dial(s)
- | `std.Fail e: sess = `std.Fail `Econn
+ | `std.Err e: sess = `std.Err `Econn
| `std.Ok fd: sess = `std.Ok std.mk([
.err = false,
.ua = std.sldup("Myrfoo HTTP"),
--- a/lib/http/types.myr
+++ b/lib/http/types.myr
@@ -2,6 +2,10 @@
use bio
pkg http =
+ type status = int
+
+ const Ok = 200
+
type session = struct
f : bio.file#
host : byte[:]
@@ -8,6 +12,24 @@
ua : byte[:]
err : bool
;;
+
+ type server = struct
+ lfd : std.fd
+ ;;
+
+ type router = struct
+ paths : route[:]
+ ;;
+
+ type ctx = struct
+ fd : byte[:]
+ ;;
+
+ type route = struct
+ path : byte[:]
+ render : (ctx : ctx# -> status)
+ ;;
+
type url = struct
schema : schema
--- a/lib/http/url.myr
+++ b/lib/http/url.myr
@@ -64,27 +64,27 @@
match parseschema(&url)
| `std.Ok s: schema = s
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
match parsehostname(&url)
| `std.Ok h: host = h
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
match parseport(&url)
| `std.Ok p: port = p
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
match parsepath(&url)
| `std.Ok p: path = p
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
match parseparams(&url)
| `std.Ok p: params = p
- | `std.Fail e: -> `std.Fail e
+ | `std.Err e: -> `std.Err e
;;
/* todo: params */
@@ -107,9 +107,9 @@
if std.chomp(url, "http://")
-> `std.Ok `Http
elif std.chomp(url, "https://")
- -> `std.Fail `Eunsupp
+ -> `std.Err `Eunsupp
else
- -> `std.Fail `Eproto
+ -> `std.Err `Eproto
;;
}
@@ -137,7 +137,7 @@
url# = url#[len:]
-> `std.Ok p
else
- -> `std.Fail `Esyntax
+ -> `std.Err `Esyntax
;;
}
@@ -151,13 +151,13 @@
match std.decode(url#)
| '?': (_, url#) = std.strstep(url#)
- | _: -> `std.Fail `Esyntax
+ | _: -> `std.Err `Esyntax
;;
params = [][:]
for sp in std.bysplit(url#, "&")
if std.bstrsplit(kvp[:], sp, "=").len != 2
- -> `std.Fail `Esyntax
+ -> `std.Err `Esyntax
;;
std.slpush(¶ms, (std.sldup(kvp[0]), std.sldup(kvp[1])))
;;
@@ -178,7 +178,7 @@
if ishostchar(chr)
len += std.charlen(chr)
else
- -> `std.Fail `Esyntax
+ -> `std.Err `Esyntax
;;
;;
;;
@@ -192,13 +192,13 @@
var ip
match std.strfind(url#, "]")
- | `std.None: -> `std.Fail `Esyntax
+ | `std.None: -> `std.Err `Esyntax
| `std.Some idx:
ip = url#[:idx]
url# = url#[idx+1:]
match std.ip6parse(url#[:idx])
| `std.Some _: -> `std.Ok ip
- | `std.None: -> `std.Fail `Esyntax
+ | `std.None: -> `std.Err `Esyntax
;;
;;
}
@@ -207,7 +207,7 @@
if std.chomp(url, ":")
match parsenumber(url, 10)
| `std.Some n: -> `std.Ok n
- | `std.None: -> `std.Fail `Esyntax
+ | `std.None: -> `std.Err `Esyntax
;;
else
-> `std.Ok 80