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:
1 parent
2c3ff56b67
commit
96b8b19970
2 files changed
+136
-65
No files matched your search
@@ -87,6 +87,19 @@ namespace Void::Net
|
|||||||
/// scraping/proxy use; enable for security-sensitive callers).
|
/// scraping/proxy use; enable for security-sensitive callers).
|
||||||
void set_verify_tls(bool on) noexcept { m_verify_tls = on; }
|
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;
|
/// Execute a request. A per-request `Request::proxy` is not modelled;
|
||||||
/// the client-level proxy applies. Returns an Error only on transport
|
/// the client-level proxy applies. Returns an Error only on transport
|
||||||
/// failure — a non-2xx HTTP response is a successful Result.
|
/// failure — a non-2xx HTTP response is a successful Result.
|
||||||
@@ -106,6 +119,8 @@ namespace Void::Net
|
|||||||
ProxyConfig m_proxy{};
|
ProxyConfig m_proxy{};
|
||||||
std::string m_user_agent = "Void.Net/1.0";
|
std::string m_user_agent = "Void.Net/1.0";
|
||||||
bool m_verify_tls = false;
|
bool m_verify_tls = false;
|
||||||
|
std::uint64_t m_max_body = 64ull * 1024 * 1024;
|
||||||
|
int m_max_redirects = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Void::Net
|
} // namespace Void::Net
|
||||||
+121
-65
@@ -18,6 +18,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <limits>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -82,13 +83,20 @@ namespace Void::Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Stream>
|
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);
|
http::write(stream, rq);
|
||||||
beast::flat_buffer buffer;
|
beast::flat_buffer buffer;
|
||||||
http::response<http::dynamic_body> res;
|
// Use a parser so the body limit is configurable. Beast's default
|
||||||
http::read(stream, buffer, res);
|
// (~1 MiB) is too small for binary downloads (release artifacts);
|
||||||
return convert(res);
|
// 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.
|
// SOCKS5 CONNECT handshake on an already-connected socket.
|
||||||
@@ -196,11 +204,17 @@ namespace Void::Net
|
|||||||
}
|
}
|
||||||
http::write(socket, creq);
|
http::write(socket, creq);
|
||||||
beast::flat_buffer buffer;
|
beast::flat_buffer buffer;
|
||||||
http::response<http::empty_body> cres;
|
// A 2xx CONNECT reply has no body; the socket then becomes a raw
|
||||||
http::read(socket, buffer, cres);
|
// tunnel. Some proxies attach a body to non-2xx replies, which an
|
||||||
if (cres.result() != http::status::ok) {
|
// 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(
|
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
|
// Perform the request over an already-connected plain socket, doing a
|
||||||
// TLS upgrade in place when the target is https.
|
// TLS upgrade in place when the target is https.
|
||||||
Response finish(tcp::socket socket, ssl::context& ctx, const Url& url,
|
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) {
|
if (url.is_tls) {
|
||||||
ssl::stream<tcp::socket> stream{std::move(socket), ctx};
|
ssl::stream<tcp::socket> stream{std::move(socket), ctx};
|
||||||
tls_handshake(stream, url);
|
tls_handshake(stream, url);
|
||||||
auto rq = build_request(url, req, ua, url.target);
|
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);
|
auto rq = build_request(url, req, ua, url.target);
|
||||||
return write_read(socket, rq);
|
return write_read(socket, rq, max_body);
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@@ -246,71 +261,112 @@ namespace Void::Net
|
|||||||
return std::nullopt;
|
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>
|
Void::Result::Result<Response, Void::Result::Error>
|
||||||
HttpClient::send(const Request& req) const
|
HttpClient::send(const Request& req) const
|
||||||
{
|
{
|
||||||
const auto url = Url::parse(req.url);
|
std::string target_url = req.url;
|
||||||
if (!url) {
|
int redirects = 0;
|
||||||
return Void::Result::fail(
|
|
||||||
std::format("Net: invalid URL '{}'", req.url));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const auto start = std::chrono::steady_clock::now();
|
|
||||||
|
|
||||||
asio::io_context ioc;
|
while (true) {
|
||||||
ssl::context ctx{ssl::context::tlsv12_client};
|
const auto url = Url::parse(target_url);
|
||||||
ctx.set_default_verify_paths();
|
if (!url) {
|
||||||
ctx.set_verify_mode(m_verify_tls ? ssl::verify_peer
|
return Void::Result::fail(
|
||||||
: ssl::verify_none);
|
std::format("Net: invalid URL '{}'", target_url));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const auto start = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
Response response;
|
asio::io_context ioc;
|
||||||
tcp::resolver resolver{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()) {
|
Response response;
|
||||||
tcp::socket socket{ioc};
|
tcp::resolver resolver{ioc};
|
||||||
asio::connect(socket, resolver.resolve(
|
|
||||||
m_proxy.host,
|
if (m_proxy.enabled()) {
|
||||||
std::to_string(m_proxy.port)));
|
tcp::socket socket{ioc};
|
||||||
if (m_proxy.kind == ProxyConfig::Kind::Socks5) {
|
asio::connect(socket, resolver.resolve(
|
||||||
socks5_connect(socket, *url, m_proxy);
|
m_proxy.host,
|
||||||
response = finish(std::move(socket), ctx, *url, req,
|
std::to_string(m_proxy.port)));
|
||||||
m_user_agent);
|
if (m_proxy.kind == ProxyConfig::Kind::Socks5) {
|
||||||
} else { // Http proxy
|
socks5_connect(socket, *url, m_proxy);
|
||||||
if (url->is_tls) {
|
|
||||||
http_connect_tunnel(socket, *url, m_proxy);
|
|
||||||
response = finish(std::move(socket), ctx, *url, req,
|
response = finish(std::move(socket), ctx, *url, req,
|
||||||
m_user_agent);
|
m_user_agent, m_max_body);
|
||||||
} else {
|
} else { // Http proxy
|
||||||
// Plain HTTP through proxy: absolute-form target.
|
if (url->is_tls) {
|
||||||
const std::string absolute = std::format(
|
http_connect_tunnel(socket, *url, m_proxy);
|
||||||
"{}://{}{}", url->scheme, url->host, url->target);
|
response = finish(std::move(socket), ctx, *url, req,
|
||||||
auto rq = build_request(*url, req, m_user_agent,
|
m_user_agent, m_max_body);
|
||||||
absolute);
|
} else {
|
||||||
if (m_proxy.has_auth()) {
|
// Plain HTTP through proxy: absolute-form target.
|
||||||
const auto enc = Void::Encoding::Base64::encode(
|
const std::string absolute = std::format(
|
||||||
*m_proxy.username + ":" + *m_proxy.password);
|
"{}://{}{}", url->scheme, url->host,
|
||||||
rq.set(http::field::proxy_authorization,
|
url->target);
|
||||||
"Basic " + enc);
|
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 =
|
response.elapsed =
|
||||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::steady_clock::now() - start);
|
std::chrono::steady_clock::now() - start);
|
||||||
return response;
|
return response;
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
return Void::Result::fail(
|
return Void::Result::fail(
|
||||||
std::format("Net: request to '{}' failed: {}", req.url,
|
std::format("Net: request to '{}' failed: {}", target_url,
|
||||||
e.what()));
|
e.what()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user