cmd/http: add headers via getopt
This commit is contained in:
parent
d798088e9e
commit
380d174beb
|
@ -1,15 +1,35 @@
|
||||||
|
use getopt;
|
||||||
use io;
|
use io;
|
||||||
use log;
|
use log;
|
||||||
use net::dial;
|
use net::dial;
|
||||||
use net::http;
|
use net::http;
|
||||||
use net::uri;
|
use net::uri;
|
||||||
use os;
|
use os;
|
||||||
|
use strings;
|
||||||
|
|
||||||
export fn main() void = {
|
export fn main() void = {
|
||||||
const client = http::newclient("Hare net::http test client");
|
const client = http::newclient("Hare net::http test client");
|
||||||
defer http::client_finish(&client);
|
defer http::client_finish(&client);
|
||||||
|
|
||||||
const target = match (uri::parse(os::args[1])) {
|
const cmd = getopt::parse(os::args,
|
||||||
|
"HTTP client",
|
||||||
|
('H', "Name:value", "Sets an HTTP header"),
|
||||||
|
"url");
|
||||||
|
defer getopt::finish(&cmd);
|
||||||
|
|
||||||
|
let head = http::client_default_header(&client);
|
||||||
|
for (let i = 0z; i < len(cmd.opts); i += 1) {
|
||||||
|
const (opt, val) = cmd.opts[i];
|
||||||
|
switch (opt) {
|
||||||
|
case 'H' =>
|
||||||
|
const (name, val) = strings::cut(val, ":");
|
||||||
|
http::header_add(head, name, val);
|
||||||
|
case => abort();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const target = cmd.args[0];
|
||||||
|
const target = match (uri::parse(target)) {
|
||||||
case let u: uri::uri =>
|
case let u: uri::uri =>
|
||||||
yield u;
|
yield u;
|
||||||
case uri::invalid =>
|
case uri::invalid =>
|
||||||
|
|
Loading…
Reference in a new issue