1
Fork 0

net::http: implement response cases without body

This commit is contained in:
Drew DeVault 2023-10-08 11:23:28 +02:00
parent 2acf7fa873
commit a8da673321
2 changed files with 21 additions and 2 deletions

View file

@ -10,6 +10,7 @@ use strings;
const usage: [_]getopt::help = [ const usage: [_]getopt::help = [
"HTTP client", "HTTP client",
('H', "Name:value", "Sets an HTTP header"), ('H', "Name:value", "Sets an HTTP header"),
('X', "method", "Sets the HTTP method verb"),
"url" "url"
]; ];
@ -40,6 +41,8 @@ export fn main() void = {
case 'H' => case 'H' =>
const (name, val) = strings::cut(val, ":"); const (name, val) = strings::cut(val, ":");
http::header_add(&req.header, name, val); http::header_add(&req.header, name, val);
case 'X' =>
req.method = val;
case => abort(); case => abort();
}; };
}; };
@ -61,7 +64,12 @@ export fn main() void = {
log::printfln("{}: {}", name, val); 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::copy(os::stdout, body)!;
io::close(body)!; io::close(body)!;
}; };

View file

@ -59,7 +59,18 @@ export fn do(client: *client, req: *request) (response | error) = {
const scan = bufio::newscanner(conn, 512); const scan = bufio::newscanner(conn, 512);
read_statusline(&resp, &scan)?; read_statusline(&resp, &scan)?;
read_header(&resp.header, &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; return resp;
}; };