Net: expose peer/client addresses and trusted-proxy policy on ServerRequest (#57)

This commit is contained in:
faumaray committed 2026-12-04 10:04:14 +03:00
1 parent b3c1246291
commit 55a1399e01
4 files changed
+317

No files matched your search

+9
View File
@@ -34,6 +34,15 @@ namespace Void::Net
/// once this much framed data is pending.
std::size_t duplex_write_buffer_bytes{64ULL * 1024};
int listen_backlog{128};
/// Reverse proxies whose forwarding headers are trusted. Entries are
/// exact IP literals ("127.0.0.1", "::1") or CIDR blocks
/// ("10.0.0.0/8", "fd00::/8"). Empty (the default) ignores
/// `X-Forwarded-For` from every peer. When the immediate peer matches
/// an entry, the header is parsed right-to-left and the first hop that
/// is not itself trusted becomes `ServerRequest::client`. Malformed
/// chains are ignored rather than trusted.
std::vector<std::string> trusted_proxies{};
};
/// Streaming request body reader passed to streaming HTTP route handlers.
+16
View File
@@ -24,6 +24,15 @@ namespace Void::Net
using RouteParameters = std::map<std::string, std::string, std::less<>>;
/// Numeric address of one HTTP peer. `port` is in host byte order and is
/// zero when the address was derived from a forwarding header.
struct PeerInfo {
std::string address;
std::uint16_t port{0};
auto operator<=>(const PeerInfo&) const = default;
};
struct ServerRequest {
Method method{Method::Get};
std::string path;
@@ -31,6 +40,13 @@ namespace Void::Net
std::string body;
RouteParameters parameters;
std::string query{};
/// Immediate TCP peer of the connection (numeric address).
PeerInfo peer{};
/// Effective client address. Equal to `peer` unless the immediate
/// peer is a configured trusted proxy, in which case the rightmost
/// non-trusted `X-Forwarded-For` hop is used. Forwarding headers
/// from untrusted peers are ignored.
PeerInfo client{};
[[nodiscard]] std::optional<std::string_view>
header(std::string_view name) const noexcept;