summaryrefslogtreecommitdiff
path: root/lib/http
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-08-27 16:11:12 -0700
committerOri Bernstein <ori@eigenstate.org>2016-08-27 16:11:12 -0700
commitf6e16f0c9f8d68a66c279059e4185d79a5b665a0 (patch)
tree3da24808759e35316c7d0ff35a786ebdc395efc5 /lib/http
parent1d4f6a7925c05705dda39c9fe799c64138d6d02a (diff)
downloadmc-f6e16f0c9f8d68a66c279059e4185d79a5b665a0.tar.gz
Update to new APIs.
Diffstat (limited to 'lib/http')
-rw-r--r--lib/http/bld.sub10
-rw-r--r--lib/http/client.myr44
-rw-r--r--lib/http/main.myr2
-rw-r--r--lib/http/parse.myr10
-rw-r--r--lib/http/server.myr37
-rw-r--r--lib/http/session.myr2
-rw-r--r--lib/http/types.myr22
-rw-r--r--lib/http/url.myr28
8 files changed, 108 insertions, 47 deletions
diff --git a/lib/http/bld.sub b/lib/http/bld.sub
index 40825bd..9e42214 100644
--- a/lib/http/bld.sub
+++ b/lib/http/bld.sub
@@ -1,5 +1,11 @@
bin h =
- main.myr
+ h.myr
+
+ lib http
+;;
+
+bin srvdot =
+ srvdot.myr
lib http
;;
@@ -9,7 +15,7 @@ lib http =
types.myr
url.myr
client.myr
+ router.myr
server.myr
session.myr
-
;;
diff --git a/lib/http/client.myr b/lib/http/client.myr
index adb9ee3..c5cb221 100644
--- a/lib/http/client.myr
+++ b/lib/http/client.myr
@@ -39,7 +39,7 @@ const trace = {s, path; -> tracereq(s, &[.url=&[.path=path]])}
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 getreq = {s, r
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 headreq = {s, r
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 putreq = {s, r, data
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 postreq = {s, r, data
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 deletereq = {s, r
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 optionsreq = {s, r
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 @@ const response = {s, body
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 @@ const request = {s, method, r, data
ioflush(s)
if s.err
- -> `std.Fail `Econn
+ -> `std.Err `Econn
else
-> `std.Ok void
;;
@@ -190,7 +190,7 @@ const readlenbody = {s, r
-> `std.Ok buf
:shortread
std.slfree(buf)
- -> `std.Fail `Eshort
+ -> `std.Err `Eshort
}
const __init__ = {
@@ -207,9 +207,9 @@ const readchunkedbody = {s, r
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 @@ const readchunkedbody = {s, r
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 @@ const checkendln = {s
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)
;;
diff --git a/lib/http/main.myr b/lib/http/main.myr
index c317f85..50ea8ea 100644
--- a/lib/http/main.myr
+++ b/lib/http/main.myr
@@ -68,7 +68,7 @@ const main = {args
std.fblat(std.Out, resp.body)
;;
http.freeresp(resp)
- | `std.Fail e:
+ | `std.Err e:
std.put("{}\n", e)
;;
http.urlfree(u)
diff --git a/lib/http/parse.myr b/lib/http/parse.myr
index 589dc60..8f6348c 100644
--- a/lib/http/parse.myr
+++ b/lib/http/parse.myr
@@ -84,7 +84,7 @@ const parseresp = {s, r
;;
| `std.Ok enc:
r.enc = enc
- | `std.Fail e:
+ | `std.Err e:
r.err = `std.Some e
-> false
;;
@@ -96,14 +96,14 @@ const parsechunksz = {s
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 @@ const getenc = {r
| `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
;;
}
diff --git a/lib/http/server.myr b/lib/http/server.myr
index 0b71c63..6e1aa4c 100644
--- 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
+ ;;
+}
+
diff --git a/lib/http/session.myr b/lib/http/session.myr
index 4ff8bfb..17833ea 100644
--- a/lib/http/session.myr
+++ b/lib/http/session.myr
@@ -25,7 +25,7 @@ const mksession = {schema, host, port
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"),
diff --git a/lib/http/types.myr b/lib/http/types.myr
index 93f1879..cd095fb 100644
--- a/lib/http/types.myr
+++ b/lib/http/types.myr
@@ -2,6 +2,10 @@ use std
use bio
pkg http =
+ type status = int
+
+ const Ok = 200
+
type session = struct
f : bio.file#
host : byte[:]
@@ -9,6 +13,24 @@ pkg http =
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
port : int
diff --git a/lib/http/url.myr b/lib/http/url.myr
index 12730cb..deaee5f 100644
--- a/lib/http/url.myr
+++ b/lib/http/url.myr
@@ -64,27 +64,27 @@ const parseurl = {url
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 @@ const parseschema = {url
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 @@ const parsepath = {url
url# = url#[len:]
-> `std.Ok p
else
- -> `std.Fail `Esyntax
+ -> `std.Err `Esyntax
;;
}
@@ -151,13 +151,13 @@ const parseparams = {url
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(&params, (std.sldup(kvp[0]), std.sldup(kvp[1])))
;;
@@ -178,7 +178,7 @@ const hostname = {url
if ishostchar(chr)
len += std.charlen(chr)
else
- -> `std.Fail `Esyntax
+ -> `std.Err `Esyntax
;;
;;
;;
@@ -192,13 +192,13 @@ const ipv6hostname = {url -> std.result(byte[:], err)
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 @@ const parseport = {url
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