Net: configurable body limit, opt-in redirect following, tolerant CONNECT

HttpClient could not download release artifacts (e.g. yt-dlp self-update):

- Beast's default body limit (~1 MiB) rejected the ~30 MB binary with
  "body limit exceeded". Add set_max_body_size() (0 = unlimited; default
  64 MiB) and read the body through a response_parser with that limit.
- Through an HTTP proxy the CONNECT reply was parsed with empty_body, so any
  body on a non-2xx reply threw "unexpected body" before the status could be
  inspected. Read only the header (skip(true) + read_header) so the tunnel
  status is checked cleanly.
- Add set_max_redirects() to follow 3xx Location transparently (default 0 =
  off, so callers that inspect redirects keep working). Resolves absolute,
  host-relative and bare-relative Location targets.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-25 01:53:10 +03:00
1 parent 2c3ff56b67
commit 96b8b19970
2 files changed
+136 -65

No files matched your search

+121 -65
View File
@@ -18,6 +18,7 @@
#include <chrono>
#include <cstdint>
#include <format>
#include <limits>
#include <stdexcept>
#include <vector>
@@ -82,13 +83,20 @@ namespace Void::Net
}
template <typename Stream>
Response write_read(Stream& stream, http::request<http::string_body>& rq)
Response write_read(Stream& stream, http::request<http::string_body>& rq,
std::uint64_t max_body)
{
http::write(stream, rq);
beast::flat_buffer buffer;
http::response<http::dynamic_body> 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<http::dynamic_body> parser;
parser.body_limit(max_body == 0
? std::numeric_limits<std::uint64_t>::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<http::empty_body> 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<http::empty_body> 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<tcp::socket> 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<Response, Void::Result::Error>
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::milliseconds>(
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::milliseconds>(
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()));
}
}
}