summaryrefslogtreecommitdiff
path: root/lib/http
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-06-19 23:44:17 -0700
committerOri Bernstein <ori@eigenstate.org>2016-06-29 21:01:50 -0700
commit308d6bb18b6389cfa8133cfbed139623a618e14e (patch)
tree041707f98f8533001e5b2ede7cfb0a96a56fcf9e /lib/http
parentc640ebcb9eca40978cd9b9075cda8b47abe814f2 (diff)
downloadmc-308d6bb18b6389cfa8133cfbed139623a618e14e.tar.gz
Add support for '-o' option.
Output to a file is nice.
Diffstat (limited to 'lib/http')
-rw-r--r--lib/http/main.myr15
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/http/main.myr b/lib/http/main.myr
index 1a14435..fa99a62 100644
--- a/lib/http/main.myr
+++ b/lib/http/main.myr
@@ -2,15 +2,17 @@ use std
use http
const main = {args
- var data, method, showhdr, hdrs
+ 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"]
][:]
@@ -20,17 +22,18 @@ const main = {args
method = "get"
data = ""
hdrs = [][:]
+ out = ""
for opt in 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 in cmd.args
u = std.try(http.parseurl(url))
s = std.try(http.mksession(u.schema, u.host, u.port))
@@ -54,7 +57,13 @@ const main = {args
std.put("{}: {}\n", k, v)
;;
;;
- std.put("{}\n", resp.body)
+ 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.Fail e:
std.put("{}\n", e)