net::http: implement response cases without body
This commit is contained in:
parent
2acf7fa873
commit
a8da673321
|
@ -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)!;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue