Files
Library/tests/standalone/Net/HttpClient.cpp
faumarayandClaude Fable 5 9a54f15e7e Net: actually enforce Request::timeout on HTTP socket I/O
Request::timeout was declared but never used — every sync Asio
operation (TLS handshake, read, write) blocked without bound, so a
stalled peer hung the calling thread indefinitely (observed: a stalled
CDN download blocking a host's shutdown join forever).

SO_RCVTIMEO cannot fix this: Asio's sync emulation treats the
resulting EAGAIN as would-block and poll()s with no deadline. Instead
a per-request DeadlineGuard watchdog ::shutdown()s the descriptor on
expiry, which wakes any blocked sync op with EOF and fails the
request. Declaration order in send() keeps the descriptor owners alive
past the guard. Resolution and connect() remain on kernel defaults.

Regression test: a stalled localhost server is bounded at ~1s.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
2026-07-12 00:20:49 +03:00

167 lines
5.0 KiB
C++

#include <Void/Net/Http.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
namespace
{
// Minimal one-shot HTTP/1.1 server on a chosen loopback port. Accepts one
// connection, reads the request (ignored beyond headers), and replies with
// a fixed 200 body. Runs on a detached helper thread; the test joins it.
struct OneShotServer {
int fd = -1;
std::uint16_t port = 0;
std::thread worker;
std::string last_request;
std::atomic<bool> served{false};
explicit OneShotServer(std::string body)
{
fd = ::socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = 0; // ephemeral
::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
socklen_t len = sizeof(addr);
::getsockname(fd, reinterpret_cast<sockaddr*>(&addr), &len);
port = ntohs(addr.sin_port);
::listen(fd, 1);
worker = std::thread([this, body = std::move(body)] {
int conn = ::accept(fd, nullptr, nullptr);
if (conn < 0) return;
char buf[4096];
const auto n = ::read(conn, buf, sizeof(buf));
if (n > 0) last_request.assign(buf, buf + n);
const std::string resp =
"HTTP/1.1 200 OK\r\nContent-Length: " +
std::to_string(body.size()) +
"\r\nConnection: close\r\n\r\n" + body;
[[maybe_unused]] auto w =
::write(conn, resp.data(), resp.size());
::close(conn);
served = true;
});
}
~OneShotServer()
{
if (worker.joinable()) worker.join();
if (fd >= 0) ::close(fd);
}
};
} // namespace
Void::Testing::Test http_client_get_localhost_round_trip()
{
OneShotServer server{"hello from void"};
const std::string url =
"http://127.0.0.1:" + std::to_string(server.port) + "/ping";
Void::Net::HttpClient client;
const auto resp = client.get(url);
Void::Testing::expect(resp.has_value());
Void::Testing::expect_eq(resp->status, static_cast<std::uint16_t>(200));
Void::Testing::expect_eq(resp->body, std::string{"hello from void"});
Void::Testing::expect(resp->ok());
// Request line reached the server with the right target.
Void::Testing::expect(server.last_request.find("GET /ping HTTP/1.1") !=
std::string::npos);
}
Void::Testing::Test http_client_post_sends_body()
{
OneShotServer server{"ok"};
const std::string url =
"http://127.0.0.1:" + std::to_string(server.port) + "/submit";
Void::Net::HttpClient client;
const auto resp = client.post(url, "payload=42");
Void::Testing::expect(resp.has_value());
Void::Testing::expect_eq(resp->status, static_cast<std::uint16_t>(200));
Void::Testing::expect(server.last_request.find("POST /submit") !=
std::string::npos);
Void::Testing::expect(server.last_request.find("payload=42") !=
std::string::npos);
}
Void::Testing::Test http_client_invalid_url_is_error()
{
Void::Net::HttpClient client;
const auto resp = client.get("not a url");
Void::Testing::expect(!resp.has_value());
}
Void::Testing::Test http_client_connection_refused_is_error()
{
Void::Net::HttpClient client;
// Port 1 on loopback: nothing listening.
const auto resp = client.get("http://127.0.0.1:1/");
Void::Testing::expect(!resp.has_value());
}
Void::Testing::Test http_client_timeout_bounds_a_stalled_server()
{
// A server that accepts and then never responds. Without an enforced
// Request::timeout the sync read blocks forever (Asio's sync emulation
// poll()s on would-block with no deadline).
struct StallServer {
int fd = -1;
std::uint16_t port = 0;
int conn = -1;
std::thread worker;
StallServer()
{
fd = ::socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = 0;
::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
socklen_t len = sizeof(addr);
::getsockname(fd, reinterpret_cast<sockaddr*>(&addr), &len);
port = ntohs(addr.sin_port);
::listen(fd, 1);
worker = std::thread([this] {
conn = ::accept(fd, nullptr, nullptr);
// Never write a byte; hold the connection open.
});
}
~StallServer()
{
if (conn >= 0) ::close(conn);
if (worker.joinable()) worker.join();
if (fd >= 0) ::close(fd);
}
};
StallServer server;
Void::Net::HttpClient client;
Void::Net::Request req;
req.url = "http://127.0.0.1:" + std::to_string(server.port) + "/stall";
req.timeout = std::chrono::seconds{1};
const auto start = std::chrono::steady_clock::now();
const auto resp = client.send(req);
const auto elapsed = std::chrono::steady_clock::now() - start;
Void::Testing::expect(!resp.has_value());
// Well under the old "forever": the deadline tripped around 1s.
Void::Testing::expect(elapsed < std::chrono::seconds{5});
}