From 1bbb27d1a9c12a1d17df30e66637ed167c2891a9 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 28 Sep 2023 10:48:26 +0200 Subject: [PATCH] cmd/hget: use URI from os::args --- cmd/hget/main.ha | 30 ++++++++++++++++++++---------- diff --git a/cmd/hget/main.ha b/cmd/hget/main.ha index 30ad67c22006904f8e1435a723551260fa4d9d37..4a6592d115b0ba0b5b0d7821b4a3b8533c383985 100644 --- a/cmd/hget/main.ha +++ b/cmd/hget/main.ha @@ -2,28 +2,31 @@ use ev; use ev::dial; use fmt; use io; +use net::ip; use net::uri; use os; use strings; type state = struct { loop: *ev::loop, + uri: *uri::uri, exit: int, - buf: []u8, + buf: [os::BUFSZ]u8, }; export fn main() void = { const loop = ev::newloop()!; defer ev::finish(&loop); + const uri = net::uri::parse(os::args[1])!; + defer uri::finish(&uri); + let state = state { loop = &loop, exit = os::status::SUCCESS, - buf = [], + uri = &uri, + buf = [0...], }; - - const uri = net::uri::parse("http://example.org")!; - defer uri::finish(&uri); dial::dial_uri(&loop, "tcp", &uri, &dialcb, &state)!; @@ -48,10 +51,18 @@ error(state, dial::strerror(err)); return; }; ev::setuser(file, state); - state.buf = strings::toutf8("GET / HTTP/1.1\r\n" - "Host: example.org\r\n" - "Connection: close\r\n\r\n"); - ev::write(file, &writecb, state.buf); + const s = fmt::bsprintf(state.buf, "GET {} HTTP/1.1\r\n" + "Host: {}\r\n" + "Connection: close\r\n\r\n", + if (state.uri.path == "") "/" else state.uri.path, + match (state.uri.host) { + case let s: str => + yield s; + case let ip: ip::addr => + yield ip::string(ip); + }); + + ev::write(file, &writecb, state.buf[..len(s)]); }; fn writecb(file: *ev::file, r: (size | io::error)) void = { @@ -63,7 +74,6 @@ case let err: io::error => error(state, io::strerror(err)); return; }; - assert(z == len(state.buf)); ev::read(file, &readcb, state.buf); }; -- 2.48.1