2023-02-10 09:13:30 +00:00
|
|
|
use bufio;
|
2023-02-10 13:09:23 +00:00
|
|
|
use encoding::utf8;
|
2023-02-11 10:03:51 +00:00
|
|
|
use errors;
|
2023-02-10 09:13:30 +00:00
|
|
|
use fmt;
|
|
|
|
use io;
|
|
|
|
use net::dial;
|
|
|
|
use net::uri;
|
|
|
|
use net;
|
|
|
|
use os;
|
2023-02-10 13:09:23 +00:00
|
|
|
use strconv;
|
|
|
|
use strings;
|
2023-02-11 10:30:20 +00:00
|
|
|
use types;
|
2023-02-10 09:13:30 +00:00
|
|
|
|
|
|
|
// Performs an HTTP [[request]] with the given [[client]]. The request is
|
|
|
|
// performed synchronously; this function blocks until the server has returned
|
|
|
|
// the response status and all HTTP headers associated with the response.
|
2023-10-07 10:43:58 +00:00
|
|
|
//
|
|
|
|
// If the provided [[response]] has a non-null body, the user must pass it to
|
|
|
|
// [[io::close]] before calling [[response_finish]].
|
2023-02-10 09:13:30 +00:00
|
|
|
export fn do(client: *client, req: *request) (response | error) = {
|
2023-02-10 09:16:18 +00:00
|
|
|
assert(req.target.scheme == "http"); // TODO: https
|
2023-02-10 09:13:30 +00:00
|
|
|
const conn = dial::dial_uri("tcp", req.target)?;
|
|
|
|
|
2023-08-25 20:23:38 +00:00
|
|
|
let buf: [os::BUFSZ]u8 = [0...];
|
|
|
|
let file = bufio::init(conn, [], buf);
|
2023-02-10 09:34:57 +00:00
|
|
|
bufio::setflush(&file, []);
|
|
|
|
|
|
|
|
fmt::fprintf(&file, "{} ", req.method)?;
|
2023-02-10 09:13:30 +00:00
|
|
|
|
|
|
|
// TODO: Support other request-targets than origin-form
|
2023-02-10 09:47:19 +00:00
|
|
|
const target = uri_origin_form(req.target);
|
2023-02-10 09:34:57 +00:00
|
|
|
uri::fmt(&file, &target)?;
|
|
|
|
fmt::fprintf(&file, " HTTP/1.1\r\n")?;
|
2023-02-10 09:13:30 +00:00
|
|
|
|
2023-02-10 09:34:57 +00:00
|
|
|
write_header(&file, &req.header)?;
|
|
|
|
fmt::fprintf(&file, "\r\n")?;
|
|
|
|
bufio::flush(&file)?;
|
2023-02-10 09:13:30 +00:00
|
|
|
|
2023-02-12 12:07:05 +00:00
|
|
|
const trans = match (req.transport) {
|
|
|
|
case let t: *transport =>
|
|
|
|
yield t;
|
|
|
|
case =>
|
|
|
|
yield &client.default_transport;
|
|
|
|
};
|
|
|
|
// TODO: Implement None
|
|
|
|
assert(trans.request_transport == transport_mode::AUTO);
|
|
|
|
assert(trans.response_transport == transport_mode::AUTO);
|
|
|
|
assert(trans.request_content == content_mode::AUTO);
|
|
|
|
assert(trans.response_content == content_mode::AUTO);
|
|
|
|
|
2023-02-10 09:13:30 +00:00
|
|
|
match (req.body) {
|
|
|
|
case let body: io::handle =>
|
|
|
|
io::copy(conn, body)?;
|
2023-02-10 13:09:23 +00:00
|
|
|
case void =>
|
|
|
|
yield;
|
2023-02-10 09:13:30 +00:00
|
|
|
};
|
|
|
|
|
2023-02-10 13:09:23 +00:00
|
|
|
let resp = response { ... };
|
2023-10-07 10:43:58 +00:00
|
|
|
const scan = bufio::newscanner(conn, 512);
|
2023-02-10 13:09:23 +00:00
|
|
|
read_statusline(&resp, &scan)?;
|
|
|
|
read_header(&resp.header, &scan)?;
|
2023-10-08 09:23:28 +00:00
|
|
|
|
|
|
|
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)!;
|
|
|
|
};
|
2023-02-10 13:09:23 +00:00
|
|
|
return resp;
|
|
|
|
};
|
|
|
|
|
|
|
|
fn read_statusline(
|
|
|
|
resp: *response,
|
|
|
|
scan: *bufio::scanner,
|
2023-02-11 10:03:51 +00:00
|
|
|
) (void | error) = {
|
2023-02-10 13:09:23 +00:00
|
|
|
const status = match (bufio::scan_string(scan, "\r\n")) {
|
|
|
|
case let line: const str =>
|
|
|
|
yield line;
|
|
|
|
case let err: io::error =>
|
|
|
|
return err;
|
|
|
|
case utf8::invalid =>
|
2023-02-11 10:03:51 +00:00
|
|
|
return protoerr;
|
2023-02-10 13:09:23 +00:00
|
|
|
case io::EOF =>
|
2023-02-11 10:03:51 +00:00
|
|
|
return protoerr;
|
2023-02-10 09:13:30 +00:00
|
|
|
};
|
2023-02-10 13:09:23 +00:00
|
|
|
|
|
|
|
const tok = strings::tokenize(status, " ");
|
|
|
|
|
2023-02-11 10:03:51 +00:00
|
|
|
const version = match (strings::next_token(&tok)) {
|
|
|
|
case let ver: str =>
|
|
|
|
yield ver;
|
2024-04-19 09:24:02 +00:00
|
|
|
case done =>
|
2023-02-11 10:03:51 +00:00
|
|
|
return protoerr;
|
|
|
|
};
|
|
|
|
|
|
|
|
const status = match (strings::next_token(&tok)) {
|
|
|
|
case let status: str =>
|
|
|
|
yield status;
|
2024-04-19 09:24:02 +00:00
|
|
|
case done =>
|
2023-02-11 10:03:51 +00:00
|
|
|
return protoerr;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reason = match (strings::next_token(&tok)) {
|
|
|
|
case let reason: str =>
|
|
|
|
yield reason;
|
2024-04-19 09:24:02 +00:00
|
|
|
case done =>
|
2023-02-11 10:03:51 +00:00
|
|
|
return protoerr;
|
|
|
|
};
|
|
|
|
|
2023-02-10 13:09:23 +00:00
|
|
|
const (_, version) = strings::cut(version, "/");
|
|
|
|
const (major, minor) = strings::cut(version, ".");
|
2023-10-07 10:26:10 +00:00
|
|
|
|
|
|
|
const major = match (strconv::stou(major)) {
|
|
|
|
case let u: uint =>
|
|
|
|
yield u;
|
|
|
|
case =>
|
|
|
|
return protoerr;
|
|
|
|
};
|
|
|
|
const minor = match (strconv::stou(minor)) {
|
|
|
|
case let u: uint =>
|
|
|
|
yield u;
|
|
|
|
case =>
|
|
|
|
return protoerr;
|
|
|
|
};
|
|
|
|
resp.version = (major, minor);
|
2023-02-11 10:03:51 +00:00
|
|
|
|
|
|
|
if (resp.version.0 > 1) {
|
|
|
|
return errors::unsupported;
|
|
|
|
};
|
|
|
|
|
2023-10-07 10:26:10 +00:00
|
|
|
resp.status = match (strconv::stou(status)) {
|
|
|
|
case let u: uint =>
|
|
|
|
yield u;
|
|
|
|
case =>
|
|
|
|
return protoerr;
|
|
|
|
};
|
|
|
|
|
2023-02-10 13:09:23 +00:00
|
|
|
resp.reason = strings::dup(reason);
|
2023-02-10 09:13:30 +00:00
|
|
|
};
|