micromegas/servers/http_utils.rs
1//! HTTP utilities for server implementations
2
3/// Extracts the client IP address from HTTP headers and extensions, formatted as a string for
4/// logging.
5///
6/// A thin wrapper over [`micromegas_auth::client_ip::resolve_client_ip`], which owns the actual
7/// resolution logic (X-Forwarded-For / X-Real-IP / socket address priority, with the
8/// `to_canonical()` normalization) -- moved into the `auth` crate so `IpAllowlist::allows` can
9/// consult the same resolution for an authorization decision, not just for this function's
10/// audit-logging callers. See that function's doc comment for the full priority order and
11/// anti-spoofing rationale.
12///
13/// Returns "unknown" if no IP can be extracted.
14pub fn get_client_ip(headers: &http::HeaderMap, extensions: &http::Extensions) -> String {
15 micromegas_auth::client_ip::resolve_client_ip(headers, extensions)
16 .map(|ip| ip.to_string())
17 .unwrap_or_else(|| "unknown".to_string())
18}