Add Void::Net — HTTP client + URL parser + SOCKS5/CONNECT proxies
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>
This commit is contained in:
1 parent
fb0506581f
commit
c92dff6ff7
8 files changed
+718
-5
No files matched your search
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Net
|
||||
{
|
||||
|
||||
enum class Method { Get, Post, Put, Delete, Patch, Head, Options };
|
||||
|
||||
struct Header {
|
||||
std::string name;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
struct Request {
|
||||
Method method = Method::Get;
|
||||
std::string url;
|
||||
std::vector<Header> headers;
|
||||
std::optional<std::string> body;
|
||||
std::chrono::seconds timeout{30};
|
||||
};
|
||||
|
||||
struct Response {
|
||||
std::uint16_t status = 0;
|
||||
std::vector<Header> headers;
|
||||
std::string body;
|
||||
std::chrono::milliseconds elapsed{0};
|
||||
|
||||
[[nodiscard]] bool ok() const noexcept
|
||||
{
|
||||
return status >= 200 && status < 300;
|
||||
}
|
||||
[[nodiscard]] std::optional<std::string_view>
|
||||
header(std::string_view name) const;
|
||||
};
|
||||
|
||||
/// Proxy configuration. `None` connects directly. `Http` uses CONNECT
|
||||
/// tunnelling for TLS targets (and request forwarding for plain HTTP).
|
||||
/// `Socks5` performs a full SOCKS5 handshake (with optional auth).
|
||||
struct ProxyConfig {
|
||||
enum class Kind { None, Http, Socks5 };
|
||||
Kind kind = Kind::None;
|
||||
std::string host;
|
||||
std::uint16_t port = 0;
|
||||
std::optional<std::string> username;
|
||||
std::optional<std::string> password;
|
||||
|
||||
[[nodiscard]] bool enabled() const noexcept
|
||||
{
|
||||
return kind != Kind::None && !host.empty() && port != 0;
|
||||
}
|
||||
[[nodiscard]] bool has_auth() const noexcept
|
||||
{
|
||||
return username.has_value() && password.has_value();
|
||||
}
|
||||
};
|
||||
|
||||
/// Synchronous HTTP/HTTPS client built on Boost.Beast/Asio + OpenSSL.
|
||||
/// Supports direct connections, HTTP-CONNECT and SOCKS5 proxies, and TLS.
|
||||
/// Generic: no application-specific defaults beyond a configurable
|
||||
/// User-Agent. Thread-safe for concurrent independent calls (each request
|
||||
/// uses its own io_context).
|
||||
class HttpClient
|
||||
{
|
||||
public:
|
||||
|
||||
HttpClient() = default;
|
||||
explicit HttpClient(ProxyConfig proxy) : m_proxy(std::move(proxy)) {}
|
||||
|
||||
void set_proxy(ProxyConfig proxy) { m_proxy = std::move(proxy); }
|
||||
[[nodiscard]] const ProxyConfig& proxy() const noexcept
|
||||
{
|
||||
return m_proxy;
|
||||
}
|
||||
|
||||
/// Default User-Agent applied when a request sets none.
|
||||
void set_user_agent(std::string ua) { m_user_agent = std::move(ua); }
|
||||
|
||||
/// Verify TLS peer certificates (default: false, matching common
|
||||
/// scraping/proxy use; enable for security-sensitive callers).
|
||||
void set_verify_tls(bool on) noexcept { m_verify_tls = on; }
|
||||
|
||||
/// Execute a request. A per-request `Request::proxy` is not modelled;
|
||||
/// the client-level proxy applies. Returns an Error only on transport
|
||||
/// failure — a non-2xx HTTP response is a successful Result.
|
||||
[[nodiscard]] Void::Result::Result<Response, Void::Result::Error>
|
||||
send(const Request& req) const;
|
||||
|
||||
// Convenience wrappers.
|
||||
[[nodiscard]] Void::Result::Result<Response, Void::Result::Error>
|
||||
get(std::string_view url,
|
||||
std::vector<Header> headers = {}) const;
|
||||
[[nodiscard]] Void::Result::Result<Response, Void::Result::Error>
|
||||
post(std::string_view url, std::string body,
|
||||
std::vector<Header> headers = {}) const;
|
||||
|
||||
private:
|
||||
|
||||
ProxyConfig m_proxy{};
|
||||
std::string m_user_agent = "Void.Net/1.0";
|
||||
bool m_verify_tls = false;
|
||||
};
|
||||
|
||||
} // namespace Void::Net
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Net
|
||||
{
|
||||
|
||||
/// Parsed components of a URL. `target` is the request-target
|
||||
/// (path + "?" + query) ready for an HTTP request line; `port` is filled
|
||||
/// with the scheme default (80/443) when absent.
|
||||
struct Url {
|
||||
std::string scheme;
|
||||
std::string host;
|
||||
std::string port;
|
||||
std::string path; ///< encoded path (defaults to "/")
|
||||
std::string query; ///< encoded query (no leading '?')
|
||||
std::string target; ///< path + ("?" + query)
|
||||
bool is_tls = false; ///< scheme is https/wss
|
||||
|
||||
/// Parse `url` (RFC 3986) into components. Returns nullopt on a
|
||||
/// malformed URL. Backed by Boost.URL.
|
||||
[[nodiscard]] static std::optional<Url> parse(std::string_view url);
|
||||
};
|
||||
|
||||
} // namespace Void::Net
|
||||
Reference in new issue
Block a user