From a8da673321d5b36b18fc67b90954c78f2711d3ca Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 8 Oct 2023 11:23:28 +0200 Subject: [PATCH] net::http: implement response cases without body --- cmd/http/main.ha | 10 +++++++++- net/http/do.ha | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/http/main.ha b/cmd/http/main.ha index 82bdf35..e266c29 100644 --- a/cmd/http/main.ha +++ b/cmd/http/main.ha @@ -10,6 +10,7 @@ use strings; const usage: [_]getopt::help = [ "HTTP client", ('H', "Name:value", "Sets an HTTP header"), + ('X', "method", "Sets the HTTP method verb"), "url" ]; @@ -40,6 +41,8 @@ export fn main() void = { case 'H' => const (name, val) = strings::cut(val, ":"); http::header_add(&req.header, name, val); + case 'X' => + req.method = val; case => abort(); }; }; @@ -61,7 +64,12 @@ export fn main() void = { log::printfln("{}: {}", name, val); }; - const body = resp.body as *io::stream; + const body = match (resp.body) { + case let st: *io::stream => + yield st; + case null => + return; + }; io::copy(os::stdout, body)!; io::close(body)!; }; diff --git a/net/http/do.ha b/net/http/do.ha index 3c01ceb..9ec723e 100644 --- a/net/http/do.ha +++ b/net/http/do.ha @@ -59,7 +59,18 @@ export fn do(client: *client, req: *request) (response | error) = { const scan = bufio::newscanner(conn, 512); read_statusline(&resp, &scan)?; read_header(&resp.header, &scan)?; - resp.body = new_reader(conn, &resp, &scan)?; + + const response_complete = + req.method == "HEAD" || + resp.status == STATUS_NO_CONTENT || + resp.status == STATUS_NOT_MODIFIED || + (resp.status >= 100 && resp.status < 200) || + (req.method == "CONNECT" && resp.status >= 200 && resp.status < 300); + if (!response_complete) { + resp.body = new_reader(conn, &resp, &scan)?; + } else if (req.method != "CONNECT") { + io::close(conn)!; + }; return resp; };