summaryrefslogtreecommitdiff
path: root/lib/http
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-07-02 21:25:10 -0700
committerOri Bernstein <ori@eigenstate.org>2018-07-03 21:28:33 -0700
commit528ac41dc8b1b23ce8a22fee1c296bbe9a6742cc (patch)
tree605816247a4b662cc04a642b958b62461bdf8dcd /lib/http
parentf04c9a908e796e5356e961d84158dfe6b1c25d7a (diff)
downloadmc-528ac41dc8b1b23ce8a22fee1c296bbe9a6742cc.tar.gz
Move stuff to examples.
Diffstat (limited to 'lib/http')
-rw-r--r--lib/http/h.myr90
-rw-r--r--lib/http/srvdot.myr66
2 files changed, 0 insertions, 156 deletions
diff --git a/lib/http/h.myr b/lib/http/h.myr
deleted file mode 100644
index 9a00e2e..0000000
--- a/lib/http/h.myr
+++ /dev/null
@@ -1,90 +0,0 @@
-use std
-use http
-
-const main = {args
- var data, method, showhdr, hdrs, out
- var s, u, r
- var cmd
-
- cmd = std.optparse(args, &[
- .argdesc = "url...",
- .minargs = 1,
- .opts = [
- [.opt='m', .arg="method", .desc="http method to use"],
- [.opt='d', .arg="data", .desc="data to put in request body"],
- [.opt='o', .arg="out", .desc="output file name"],
- [.opt='H', .desc="show headers"],
- [.opt='D', .arg="hdr", .desc="define custom header"]
- ][:]
- ])
-
- showhdr = false
- method = "get"
- data = ""
- hdrs = [][:]
- out = ""
- for opt : cmd.opts
- match opt
- | ('m', m): method = m
- | ('d', d): data = d
- | ('o', o): out = o
- | ('H', ""): showhdr = true
- | ('D', def): parsedef(&hdrs, def)
- | _: std.die("unreachable")
- ;;
- ;;
-
- for url : cmd.args
- if !std.hasprefix(url, "http")
- url = std.fmt("http://{}", url)
- ;;
- u = std.try(http.parseurl(url))
- s = std.try(http.mksession(u.schema, u.host, u.port))
-
- match method
- | "get": r = http.getreq(s, &[.url=u, .hdrs=hdrs])
- | "head": r = http.headreq(s, &[.url=u, .hdrs=hdrs])
- | "delete": r = http.deletereq(s, &[.url=u, .hdrs=hdrs])
- | "trace": r = http.tracereq(s, &[.url=u, .hdrs=hdrs])
- | "options": r = http.optionsreq(s, &[.url=u, .hdrs=hdrs])
- | "put": r = http.putreq(s, &[.url=u, .hdrs=hdrs], data)
- | "post": r = http.postreq(s, &[.url=u, .hdrs=hdrs], data)
- | unknown: std.fatal("unknown method '{}'\n", unknown)
- ;;
-
- match r
- | `std.Ok resp:
- if showhdr
- std.put("status: {}\n", resp.status)
- for (k, v) : resp.hdrs
- std.put("{}: {}\n", k, v)
- ;;
- ;;
- if out.len != 0
- if !std.blat(out, resp.body, 0o644)
- std.fatal("could not write output: {}\n", out)
- ;;
- else
- std.fblat(std.Out, resp.body)
- ;;
- http.freeresp(resp)
- | `std.Err e:
- std.put("{}\n", e)
- ;;
- http.urlfree(u)
- ;;
-}
-
-const parsedef = {hdrs, hdr
- var key, val
-
- match std.strfind(hdr, ":")
- | `std.None:
- std.fatal("bad header string {}\n", hdr)
- | `std.Some idx:
- key = std.sldup(std.strstrip(hdr[:idx]))
- val = std.sldup(std.strstrip(hdr[idx+1:]))
- std.slpush(hdrs, (key, val))
- ;;
-}
-
diff --git a/lib/http/srvdot.myr b/lib/http/srvdot.myr
deleted file mode 100644
index e6c0a21..0000000
--- a/lib/http/srvdot.myr
+++ /dev/null
@@ -1,66 +0,0 @@
-use std
-use http
-
-const main = {args
- var srv, ann, cmd
-
- cmd = std.optparse(args, &[
- .maxargs=0,
- .opts = [[.opt='a', .arg="ann", .desc="announce on `ann`"]][:]
- ])
- ann = "tcp!localhost!8080"
- for opt : cmd.opts
- match opt
- | ('a', a): ann = a
- | _: std.die("unreachable")
- ;;
- ;;
-
- match http.announce(ann)
- | `std.Ok s: srv = s
- | `std.Err e: std.fatal("unable to announce: {}\n", e)
- ;;
-
- http.serve(srv, route)
-}
-
-const route = {srv, sess, req
- std.put("Reading path {}\n", req.url.path)
- match req.url.path
- | "/ping": respond(srv, sess, 200, "pong")
- | "/quit": http.shutdown(srv)
- | fspath: showfile(srv, sess, req.url.path)
- ;;
-}
-
-const showfile = {srv, sess, path
- var eb : byte[128]
- var p
-
- p = std.pathcat(".", path)
- match std.slurp(p)
- | `std.Ok buf:
- respond(srv, sess, 200, buf)
- std.slfree(buf)
- | `std.Err e:
- respond(srv, sess, 404, std.bfmt(eb[:], "error reading {}: {}\n", p, e))
- ;;
- std.slfree(p)
-}
-
-
-const respond = {srv, sess, status, body
- var resp
-
- resp = std.mk([
- .status=status,
- .hdrs = [][:],
- .len = 0,
- .err = `std.None,
- .reason = "",
- .body = body,
- .enc = `http.Length
- ])
- http.respond(srv, sess, resp)
- std.free(resp)
-}