summaryrefslogtreecommitdiff
path: root/examples
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 /examples
parentf04c9a908e796e5356e961d84158dfe6b1c25d7a (diff)
downloadmc-528ac41dc8b1b23ce8a22fee1c296bbe9a6742cc.tar.gz
Move stuff to examples.
Diffstat (limited to 'examples')
-rw-r--r--examples/bld.sub21
-rw-r--r--examples/h.myr90
-rw-r--r--examples/srvdot.myr66
3 files changed, 177 insertions, 0 deletions
diff --git a/examples/bld.sub b/examples/bld.sub
new file mode 100644
index 0000000..132a0e9
--- /dev/null
+++ b/examples/bld.sub
@@ -0,0 +1,21 @@
+# http examples
+bin h {noinst} =
+ h.myr
+
+ lib @/lib/sys:sys
+ lib @/lib/std:std
+ lib @/lib/bio:bio
+ lib @/lib/http:http
+ lib @/lib/thread:thread
+;;
+
+bin srvdot {noinst} =
+ srvdot.myr
+
+ lib @/lib/sys:sys
+ lib @/lib/std:std
+ lib @/lib/bio:bio
+ lib @/lib/http:http
+ lib @/lib/thread:thread
+;;
+
diff --git a/examples/h.myr b/examples/h.myr
new file mode 100644
index 0000000..9a00e2e
--- /dev/null
+++ b/examples/h.myr
@@ -0,0 +1,90 @@
+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/examples/srvdot.myr b/examples/srvdot.myr
new file mode 100644
index 0000000..e6c0a21
--- /dev/null
+++ b/examples/srvdot.myr
@@ -0,0 +1,66 @@
+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)
+}