ref: 528ac41dc8b1b23ce8a22fee1c296bbe9a6742cc
parent: f04c9a908e796e5356e961d84158dfe6b1c25d7a
author: Ori Bernstein <[email protected]>
date: Mon Jul 2 17:25:10 EDT 2018
Move stuff to examples.
--- a/bld.proj
+++ b/bld.proj
@@ -5,5 +5,6 @@
bench
support
doc
+ examples
;;
--- /dev/null
+++ b/examples/bld.sub
@@ -1,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
+;;
+
--- /dev/null
+++ b/examples/h.myr
@@ -1,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))
+ ;;
+}
+
--- /dev/null
+++ b/examples/srvdot.myr
@@ -1,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)
+}
--- a/lib/http/h.myr
+++ /dev/null
@@ -1,90 +1,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))
- ;;
-}
-
--- a/lib/http/srvdot.myr
+++ /dev/null
@@ -1,66 +1,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)
-}
--- a/mk/bootstrap/bootstrap+FreeBSD-amd64.sh
+++ b/mk/bootstrap/bootstrap+FreeBSD-amd64.sh
@@ -145,5 +145,5 @@
$pwd/6/6m -I lib/sys -I lib/std -I lib/bio -I lib/regex -I lib/thread mbld/test.myr
$pwd/6/6m -I lib/sys -I lib/std -I lib/bio -I lib/regex -I lib/thread mbld/deps.myr
$pwd/6/6m -I lib/sys -I lib/std -I lib/bio -I lib/regex -I lib/thread mbld/main.myr
- ld -o mbld/mbld $pwd/rt/_myrrt.o mbld/deps.o mbld/main.o mbld/util.o mbld/cpufeatures.o mbld/libs.o mbld/syssel.o mbld/config.o mbld/opts.o mbld/subtest.o mbld/types.o mbld/test.o mbld/install.o mbld/parse.o mbld/build.o -Llib/thread -lthread -Llib/bio -lbio -Llib/regex -lregex -Llib/std -lstd -Llib/sys -lsys
+ ld --gc-sections -o mbld/mbld $pwd/rt/_myrrt.o mbld/deps.o mbld/main.o mbld/util.o mbld/cpufeatures.o mbld/libs.o mbld/syssel.o mbld/config.o mbld/opts.o mbld/subtest.o mbld/types.o mbld/test.o mbld/install.o mbld/parse.o mbld/build.o -Llib/thread -lthread -Llib/bio -lbio -Llib/regex -lregex -Llib/std -lstd -Llib/sys -lsys
true