pub struct ForwardedPolicy(/* private fields */);Expand description
Trust policy for the Forwarded and X-Forwarded-* request headers.
Clients can send these headers themselves, so they are only meaningful if
they are set by a proxy you control. The policy is configured with
WebAppConfig::set_forwarded() and used by ConnectionInfo.
The default policy is ForwardedPolicy::ignore().
use ntex::web::{self, App, ForwardedPolicy, HttpRequest, WebAppConfig};
async fn index(req: HttpRequest) -> String {
format!("client: {:?}", req.connection_info().remote())
}
let config = WebAppConfig::new()
.set_forwarded(ForwardedPolicy::trust_proxies(["10.0.0.1".parse().unwrap()]));
App::default()
.with_config(config)
.route("/", web::get().to(index));Implementations§
Source§impl ForwardedPolicy
impl ForwardedPolicy
Sourcepub fn ignore() -> Self
pub fn ignore() -> Self
Ignore forwarding headers.
Connection information comes from the connection and the request only:
the peer address, the request target, the Host header and the server
configuration. This is the default.
Sourcepub fn trust_all() -> Self
pub fn trust_all() -> Self
Trust forwarding headers from every peer.
The first (client-most) value of each header is used, as ntex did before 4.7.0. Use this only if every connection comes through a proxy that removes or overwrites client-supplied forwarding headers.
Sourcepub fn trust_proxies<I>(proxies: I) -> Selfwhere
I: IntoIterator<Item = IpAddr>,
pub fn trust_proxies<I>(proxies: I) -> Selfwhere
I: IntoIterator<Item = IpAddr>,
Trust forwarding headers only from the listed proxy addresses.
See ForwardedPolicy::trust_fn() for how the headers are evaluated.
Sourcepub fn trust_fn<F>(f: F) -> Self
pub fn trust_fn<F>(f: F) -> Self
Trust forwarding headers from proxies for which f returns true.
The headers are used only if the peer of the connection is trusted. The
hops listed in Forwarded, or in X-Forwarded-For if there is no
Forwarded header, are then walked from the nearest one, skipping
trusted proxies. The first untrusted hop is the client, or the farthest
hop if all of them are trusted. With Forwarded the scheme and host
come from the client’s element. With X-Forwarded-Proto and
X-Forwarded-Host the values are matched to X-Forwarded-For
entries counting from the end.
Trusted proxies must add their own entries, and must not pass along a
client’s X-Forwarded-Proto or X-Forwarded-Host without setting them.
f receives addresses with IPv4-mapped IPv6 addresses converted to IPv4.