1
Fork 0

cmd/http: add headers via getopt

This commit is contained in:
Drew DeVault 2023-10-07 12:57:35 +02:00
parent d798088e9e
commit 380d174beb

View file

@ -1,15 +1,35 @@
use getopt;
use io;
use log;
use net::dial;
use net::http;
use net::uri;
use os;
use strings;
export fn main() void = {
const client = http::newclient("Hare net::http test 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 =>
yield u;
case uri::invalid =>