New SHARED module built on Boost.Beast/Asio + OpenSSL (all generic, no application coupling): - Void::Net::Url — RFC 3986 parse into scheme/host/port/path/query/target (Boost.URL), with scheme-default ports and TLS detection. - Void::Net::HttpClient — synchronous GET/POST/send with TLS (SNI), configurable User-Agent and TLS verification, request/response types. - Proxy support: HTTP CONNECT tunnelling for TLS targets, plain-HTTP forwarding, and a full SOCKS5 handshake (auth, IPv4/IPv6/domain). - Reuses Void::Encoding::Base64 for proxy auth. Build wiring: Dependencies.cmake resolves Boost (headers + url) and OpenSSL + Threads via find_package (system Boost/OpenSSL). Tests cover URL parsing and live localhost HTTP GET/POST round-trips (full suite 1136 pass). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
113 lines
3.4 KiB
C++
113 lines
3.4 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());
|
|
}
|