1
Fork 0
hare-http/cmd/http/main.ha
2023-10-07 12:43:58 +02:00

41 lines
913 B
Hare

use io;
use log;
use net::dial;
use net::http;
use net::uri;
use os;
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])) {
case let u: uri::uri =>
yield u;
case uri::invalid =>
log::fatal("Invalid URI");
};
defer uri::finish(&target);
const resp = match (http::get(&client, &target)) {
case let err: http::error =>
log::fatal("HTTP error:", http::strerror(err));
case let resp: http::response =>
yield resp;
};
defer http::response_finish(&resp);
log::printfln("HTTP/{}.{}: {} {}",
resp.version.0, resp.version.1,
resp.status, resp.reason);
for (let i = 0z; i < len(resp.header); i += 1) {
const (name, val) = resp.header[i];
log::printfln("{}: {}", name, val);
};
const body = resp.body as *io::stream;
io::copy(os::stdout, body)!;
io::close(body)!;
};