1
Fork 0
hare-http/cmd/http/main.ha
Drew DeVault ca93a48b12 Implement HTTP response parsing and reader
TODO: Handle Transfer-Encoding and Content-Length properly
2023-02-10 14:09:28 +01:00

41 lines
922 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 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 req = http::get(&client, &target);
const resp = match (http::do(&client, &req)) {
case let err: http::error =>
log::fatal("HTTP error:", http::strerror(err));
case let resp: http::response =>
yield resp;
};
defer http::request_finish(&req);
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 *http::reader;
io::copy(os::stdout, body)!;
};