1
Fork 0

net::http::do: remove context struct

This commit is contained in:
Drew DeVault 2023-02-10 10:34:57 +01:00
parent 859c8dc06e
commit a567ff4418

View file

@ -6,36 +6,30 @@ use net::uri;
use net; use net;
use os; use os;
type context = struct {
conn: io::handle,
file: bufio::bufstream,
buf: [os::BUFSIZ]u8,
};
// Performs an HTTP [[request]] with the given [[client]]. The request is // Performs an HTTP [[request]] with the given [[client]]. The request is
// performed synchronously; this function blocks until the server has returned // performed synchronously; this function blocks until the server has returned
// the response status and all HTTP headers associated with the response. // the response status and all HTTP headers associated with the response.
export fn do(client: *client, req: *request) (response | error) = { export fn do(client: *client, req: *request) (response | error) = {
let ctx = context { ... };
assert(req.target.scheme == "http"); // TODO: https assert(req.target.scheme == "http"); // TODO: https
const conn = dial::dial_uri("tcp", req.target)?; const conn = dial::dial_uri("tcp", req.target)?;
ctx.file = bufio::buffered(conn, [], ctx.buf);
bufio::setflush(&ctx.file, []);
fmt::fprintf(&ctx.file, "{} ", req.method)?; let buf: [os::BUFSIZ]u8 = [0...];
let file = bufio::buffered(conn, [], buf);
bufio::setflush(&file, []);
fmt::fprintf(&file, "{} ", req.method)?;
// TODO: Support other request-targets than origin-form // TODO: Support other request-targets than origin-form
let target = uri_origin_form(req.target); let target = uri_origin_form(req.target);
uri::fmt(&ctx.file, &target)?; uri::fmt(&file, &target)?;
fmt::fprintf(&ctx.file, " HTTP/1.1\r\n")?; fmt::fprintf(&file, " HTTP/1.1\r\n")?;
// TODO: Handle Content-Length and Transfer-Encoding chunked/gzip // TODO: Handle Content-Length and Transfer-Encoding chunked/gzip
// properly // properly
write_header(&ctx.file, &req.header)?; write_header(&file, &req.header)?;
fmt::fprintf(&ctx.file, "\r\n")?; fmt::fprintf(&file, "\r\n")?;
bufio::flush(&ctx.file)?; bufio::flush(&file)?;
match (req.body) { match (req.body) {
case void => case void =>