diff --git a/include/Void/Net/Http.hpp b/include/Void/Net/Http.hpp index 648e270..7fe1b30 100644 --- a/include/Void/Net/Http.hpp +++ b/include/Void/Net/Http.hpp @@ -87,6 +87,19 @@ namespace Void::Net /// scraping/proxy use; enable for security-sensitive callers). void set_verify_tls(bool on) noexcept { m_verify_tls = on; } + /// Maximum response body size in bytes. 0 means unlimited. Default + /// is 64 MiB (Beast's own default of ~1 MiB is too small for binary + /// downloads such as release artifacts). Raise for large downloads. + void set_max_body_size(std::uint64_t bytes) noexcept + { + m_max_body = bytes; + } + + /// Follow up to `n` HTTP 3xx redirects transparently (default 0 = + /// do not follow; the caller sees the 3xx response and its Location + /// header). Opt-in so callers that inspect redirects keep working. + void set_max_redirects(int n) noexcept { m_max_redirects = n; } + /// Execute a request. A per-request `Request::proxy` is not modelled; /// the client-level proxy applies. Returns an Error only on transport /// failure — a non-2xx HTTP response is a successful Result. @@ -106,6 +119,8 @@ namespace Void::Net ProxyConfig m_proxy{}; std::string m_user_agent = "Void.Net/1.0"; bool m_verify_tls = false; + std::uint64_t m_max_body = 64ull * 1024 * 1024; + int m_max_redirects = 0; }; } // namespace Void::Net diff --git a/src/Net/Http.cpp b/src/Net/Http.cpp index 8189787..3ef6ec6 100644 --- a/src/Net/Http.cpp +++ b/src/Net/Http.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -82,13 +83,20 @@ namespace Void::Net } template - Response write_read(Stream& stream, http::request& rq) + Response write_read(Stream& stream, http::request& rq, + std::uint64_t max_body) { http::write(stream, rq); beast::flat_buffer buffer; - http::response res; - http::read(stream, buffer, res); - return convert(res); + // Use a parser so the body limit is configurable. Beast's default + // (~1 MiB) is too small for binary downloads (release artifacts); + // max_body == 0 means unlimited. + http::response_parser parser; + parser.body_limit(max_body == 0 + ? std::numeric_limits::max() + : max_body); + http::read(stream, buffer, parser); + return convert(parser.get()); } // SOCKS5 CONNECT handshake on an already-connected socket. @@ -196,11 +204,17 @@ namespace Void::Net } http::write(socket, creq); beast::flat_buffer buffer; - http::response cres; - http::read(socket, buffer, cres); - if (cres.result() != http::status::ok) { + // A 2xx CONNECT reply has no body; the socket then becomes a raw + // tunnel. Some proxies attach a body to non-2xx replies, which an + // empty_body reader rejects ("unexpected body") before we can even + // read the status. skip(true) + read_header reads only the headers. + http::response_parser cparser; + cparser.skip(true); + http::read_header(socket, buffer, cparser); + if (cparser.get().result() != http::status::ok) { throw std::runtime_error(std::format( - "Proxy CONNECT failed: {}", int(cres.result_int()))); + "Proxy CONNECT failed: {}", + int(cparser.get().result_int()))); } } @@ -216,16 +230,17 @@ namespace Void::Net // Perform the request over an already-connected plain socket, doing a // TLS upgrade in place when the target is https. Response finish(tcp::socket socket, ssl::context& ctx, const Url& url, - const Request& req, std::string_view ua) + const Request& req, std::string_view ua, + std::uint64_t max_body) { if (url.is_tls) { ssl::stream stream{std::move(socket), ctx}; tls_handshake(stream, url); auto rq = build_request(url, req, ua, url.target); - return write_read(stream, rq); + return write_read(stream, rq, max_body); } auto rq = build_request(url, req, ua, url.target); - return write_read(socket, rq); + return write_read(socket, rq, max_body); } } // namespace @@ -246,71 +261,112 @@ namespace Void::Net return std::nullopt; } + namespace + { + // Resolve a redirect Location against the URL it came from. Handles + // absolute URLs and host-relative ("/path") and bare-relative targets. + std::string resolve_redirect(const Url& base, std::string_view location) + { + if (location.starts_with("http://") || + location.starts_with("https://")) { + return std::string(location); + } + std::string origin = + std::format("{}://{}", base.scheme, base.host); + const bool standard_port = + (base.scheme == "https" && base.port == "443") || + (base.scheme == "http" && base.port == "80"); + if (!base.port.empty() && !standard_port) { + origin += ":" + base.port; + } + if (location.starts_with("/")) { + return origin + std::string(location); + } + return origin + "/" + std::string(location); + } + } // namespace + Void::Result::Result HttpClient::send(const Request& req) const { - const auto url = Url::parse(req.url); - if (!url) { - return Void::Result::fail( - std::format("Net: invalid URL '{}'", req.url)); - } - try { - const auto start = std::chrono::steady_clock::now(); + std::string target_url = req.url; + int redirects = 0; - asio::io_context ioc; - ssl::context ctx{ssl::context::tlsv12_client}; - ctx.set_default_verify_paths(); - ctx.set_verify_mode(m_verify_tls ? ssl::verify_peer - : ssl::verify_none); + while (true) { + const auto url = Url::parse(target_url); + if (!url) { + return Void::Result::fail( + std::format("Net: invalid URL '{}'", target_url)); + } + try { + const auto start = std::chrono::steady_clock::now(); - Response response; - tcp::resolver resolver{ioc}; + asio::io_context ioc; + ssl::context ctx{ssl::context::tlsv12_client}; + ctx.set_default_verify_paths(); + ctx.set_verify_mode(m_verify_tls ? ssl::verify_peer + : ssl::verify_none); - if (m_proxy.enabled()) { - tcp::socket socket{ioc}; - asio::connect(socket, resolver.resolve( - m_proxy.host, - std::to_string(m_proxy.port))); - if (m_proxy.kind == ProxyConfig::Kind::Socks5) { - socks5_connect(socket, *url, m_proxy); - response = finish(std::move(socket), ctx, *url, req, - m_user_agent); - } else { // Http proxy - if (url->is_tls) { - http_connect_tunnel(socket, *url, m_proxy); + Response response; + tcp::resolver resolver{ioc}; + + if (m_proxy.enabled()) { + tcp::socket socket{ioc}; + asio::connect(socket, resolver.resolve( + m_proxy.host, + std::to_string(m_proxy.port))); + if (m_proxy.kind == ProxyConfig::Kind::Socks5) { + socks5_connect(socket, *url, m_proxy); response = finish(std::move(socket), ctx, *url, req, - m_user_agent); - } else { - // Plain HTTP through proxy: absolute-form target. - const std::string absolute = std::format( - "{}://{}{}", url->scheme, url->host, url->target); - auto rq = build_request(*url, req, m_user_agent, - absolute); - if (m_proxy.has_auth()) { - const auto enc = Void::Encoding::Base64::encode( - *m_proxy.username + ":" + *m_proxy.password); - rq.set(http::field::proxy_authorization, - "Basic " + enc); + m_user_agent, m_max_body); + } else { // Http proxy + if (url->is_tls) { + http_connect_tunnel(socket, *url, m_proxy); + response = finish(std::move(socket), ctx, *url, req, + m_user_agent, m_max_body); + } else { + // Plain HTTP through proxy: absolute-form target. + const std::string absolute = std::format( + "{}://{}{}", url->scheme, url->host, + url->target); + auto rq = build_request(*url, req, m_user_agent, + absolute); + if (m_proxy.has_auth()) { + const auto enc = Void::Encoding::Base64::encode( + *m_proxy.username + ":" + *m_proxy.password); + rq.set(http::field::proxy_authorization, + "Basic " + enc); + } + response = write_read(socket, rq, m_max_body); } - response = write_read(socket, rq); + } + } else { + tcp::socket socket{ioc}; + asio::connect(socket, + resolver.resolve(url->host, url->port)); + response = finish(std::move(socket), ctx, *url, req, + m_user_agent, m_max_body); + } + + // Transparently follow redirects when enabled. + if (m_max_redirects > 0 && response.status >= 300 && + response.status < 400 && redirects < m_max_redirects) { + if (auto loc = response.header("location")) { + target_url = resolve_redirect(*url, *loc); + ++redirects; + continue; } } - } else { - tcp::socket socket{ioc}; - asio::connect(socket, - resolver.resolve(url->host, url->port)); - response = finish(std::move(socket), ctx, *url, req, - m_user_agent); - } - response.elapsed = - std::chrono::duration_cast( - std::chrono::steady_clock::now() - start); - return response; - } catch (const std::exception& e) { - return Void::Result::fail( - std::format("Net: request to '{}' failed: {}", req.url, - e.what())); + response.elapsed = + std::chrono::duration_cast( + std::chrono::steady_clock::now() - start); + return response; + } catch (const std::exception& e) { + return Void::Result::fail( + std::format("Net: request to '{}' failed: {}", target_url, + e.what())); + } } }