From 9f50a0f41c24557e8273134c654816333b954228 Mon Sep 17 00:00:00 2001 From: Mauro D Date: Sat, 27 Aug 2022 13:35:19 +0000 Subject: [PATCH] Forwarded for support. --- src/client.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/client.rs b/src/client.rs index 6062ce9..5b2efdb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -68,6 +68,7 @@ pub struct Client { pub struct ClientBuilder { credentials: Option, trusted_hosts: AHashSet, + forwarded_for: Option, timeout: u64, } @@ -83,6 +84,7 @@ impl ClientBuilder { credentials: None, trusted_hosts: AHashSet::new(), timeout: DEFAULT_TIMEOUT_MS, + forwarded_for: None, } } @@ -104,6 +106,11 @@ impl ClientBuilder { self } + pub fn forwarded_for(mut self, forwarded_for: impl ToString) -> Self { + self.forwarded_for = Some(forwarded_for.to_string()); + self + } + #[cfg(feature = "async")] pub async fn connect(self, url: &str) -> crate::Result { let authorization = match self.credentials.expect("Missing credentials") { @@ -119,6 +126,12 @@ impl ClientBuilder { header::AUTHORIZATION, header::HeaderValue::from_str(&authorization).unwrap(), ); + if let Some(forwarded_for) = self.forwarded_for { + headers.insert( + header::FORWARDED, + header::HeaderValue::from_str(&format!("for={}", forwarded_for)).unwrap(), + ); + } let trusted_hosts = Arc::new(self.trusted_hosts); @@ -199,6 +212,12 @@ impl ClientBuilder { header::AUTHORIZATION, header::HeaderValue::from_str(&authorization).unwrap(), ); + if let Some(forwarded_for) = self.forwarded_for { + headers.insert( + header::FORWARDED, + header::HeaderValue::from_str(&format!("for={}", forwarded_for)).unwrap(), + ); + } let trusted_hosts = Arc::new(self.trusted_hosts);