feat(net): complete P2 M-NET-01..05 codec, pool, safe static open, redirects, and RFC 6455 WebSocket
This commit is contained in:
1 parent
700024a24d
commit
86d777eae6
14 files changed
+4346
-19
No files matched your search
+54
-15
@@ -917,9 +917,15 @@ namespace Void::Net
|
||||
HttpClient::send(const Request& req) const
|
||||
{
|
||||
std::string target_url = req.url;
|
||||
Method active_method = req.method;
|
||||
std::optional<std::string> active_body = req.body;
|
||||
std::vector<Header> active_headers = req.headers;
|
||||
int redirects = 0;
|
||||
|
||||
const auto overall_start = std::chrono::steady_clock::now();
|
||||
const bool has_deadline = req.timeout > std::chrono::seconds::zero();
|
||||
const auto overall_deadline = overall_start + req.timeout;
|
||||
|
||||
while (true) {
|
||||
const auto url = Url::parse(target_url);
|
||||
if (!url) {
|
||||
@@ -928,11 +934,25 @@ namespace Void::Net
|
||||
std::format("Net: invalid URL '{}'",
|
||||
sanitize_url_for_log(target_url)))};
|
||||
}
|
||||
std::chrono::seconds remaining_timeout = req.timeout;
|
||||
if (has_deadline) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now >= overall_deadline) {
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::timeout,
|
||||
std::format("Net: request to '{}' timed out across "
|
||||
"redirects",
|
||||
sanitize_url_for_log(target_url)))};
|
||||
}
|
||||
const auto rem_ms =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
overall_deadline - now);
|
||||
remaining_timeout = std::chrono::seconds{
|
||||
std::max<long long>(1, (rem_ms.count() + 999) / 1000)};
|
||||
}
|
||||
bool cancelled = false;
|
||||
bool timed_out = false;
|
||||
try {
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
|
||||
asio::io_context ioc;
|
||||
ssl::context ctx = make_ssl_context(m_verify_tls, m_ca_file);
|
||||
|
||||
@@ -945,26 +965,26 @@ namespace Void::Net
|
||||
asio::connect(socket, resolver.resolve(
|
||||
m_proxy.host,
|
||||
std::to_string(m_proxy.port)));
|
||||
DeadlineGuard deadline{socket, req.timeout};
|
||||
DeadlineGuard deadline{socket, remaining_timeout};
|
||||
if (m_proxy.kind == ProxyConfig::Kind::Socks5) {
|
||||
socks5_connect(socket, *url, m_proxy);
|
||||
response = finish(socket, tls, ctx, *url, req.method,
|
||||
active_headers, req.body,
|
||||
response = finish(socket, tls, ctx, *url, active_method,
|
||||
active_headers, active_body,
|
||||
m_user_agent, m_max_body,
|
||||
m_verify_tls);
|
||||
} else {
|
||||
if (url->is_tls) {
|
||||
http_connect_tunnel(socket, *url, m_proxy);
|
||||
response = finish(socket, tls, ctx, *url,
|
||||
req.method, active_headers,
|
||||
req.body, m_user_agent,
|
||||
active_method, active_headers,
|
||||
active_body, m_user_agent,
|
||||
m_max_body, m_verify_tls);
|
||||
} else {
|
||||
const std::string absolute = std::format(
|
||||
"{}://{}{}", url->scheme, url->host,
|
||||
url->target);
|
||||
auto rq = build_request(*url, req.method,
|
||||
active_headers, req.body,
|
||||
auto rq = build_request(*url, active_method,
|
||||
active_headers, active_body,
|
||||
m_user_agent, absolute);
|
||||
if (m_proxy.has_auth()) {
|
||||
const auto enc = Void::Encoding::Base64::encode(
|
||||
@@ -981,17 +1001,21 @@ namespace Void::Net
|
||||
} else {
|
||||
asio::connect(socket,
|
||||
resolver.resolve(url->host, url->port));
|
||||
DeadlineGuard deadline{socket, req.timeout};
|
||||
DeadlineGuard deadline{socket, remaining_timeout};
|
||||
response =
|
||||
finish(socket, tls, ctx, *url, req.method,
|
||||
active_headers, req.body, m_user_agent,
|
||||
finish(socket, tls, ctx, *url, active_method,
|
||||
active_headers, active_body, m_user_agent,
|
||||
m_max_body, m_verify_tls);
|
||||
timed_out = deadline.timed_out();
|
||||
cancelled = deadline.cancelled();
|
||||
}
|
||||
|
||||
if (m_max_redirects > 0 && response.status >= 300 &&
|
||||
response.status < 400 && redirects < m_max_redirects) {
|
||||
const bool is_redirect_status =
|
||||
response.status == 301 || response.status == 302 ||
|
||||
response.status == 303 || response.status == 307 ||
|
||||
response.status == 308;
|
||||
if (m_max_redirects > 0 && is_redirect_status &&
|
||||
redirects < m_max_redirects) {
|
||||
if (auto loc = response.header("location")) {
|
||||
const std::string next_url =
|
||||
resolve_redirect(*url, *loc);
|
||||
@@ -1011,6 +1035,21 @@ namespace Void::Net
|
||||
if (!same_origin(*url, *next_parsed)) {
|
||||
strip_sensitive_headers(active_headers);
|
||||
}
|
||||
// RFC 9110 §15.4.4: 303 changes non-HEAD to GET and
|
||||
// drops body + representation headers.
|
||||
if (response.status == 303 &&
|
||||
active_method != Method::Head) {
|
||||
active_method = Method::Get;
|
||||
active_body.reset();
|
||||
std::erase_if(active_headers, [](const Header& h) {
|
||||
return equal_ascii_ci(h.name,
|
||||
"Content-Length") ||
|
||||
equal_ascii_ci(h.name,
|
||||
"Content-Type") ||
|
||||
equal_ascii_ci(h.name,
|
||||
"Transfer-Encoding");
|
||||
});
|
||||
}
|
||||
target_url = next_url;
|
||||
++redirects;
|
||||
continue;
|
||||
@@ -1019,7 +1058,7 @@ namespace Void::Net
|
||||
|
||||
response.elapsed =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - start);
|
||||
std::chrono::steady_clock::now() - overall_start);
|
||||
return response;
|
||||
} catch (const TlsError& e) {
|
||||
return std::unexpected{make_net_error(
|
||||
|
||||
Reference in new issue
Block a user