From 4c7e0913c8a310b7fb563ddc262c44ffa461ec77 Mon Sep 17 00:00:00 2001 From: NexVeridian Date: Thu, 28 Nov 2024 12:20:30 -0800 Subject: [PATCH] use Option<&T> instead of &Option for maybe_get_forwarded --- src/controller/middleware/remote_ip.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/controller/middleware/remote_ip.rs b/src/controller/middleware/remote_ip.rs index 9419c919..77239231 100644 --- a/src/controller/middleware/remote_ip.rs +++ b/src/controller/middleware/remote_ip.rs @@ -129,7 +129,7 @@ impl MiddlewareLayer for RemoteIpMiddleware { // implementation reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For fn maybe_get_forwarded( headers: &HeaderMap, - trusted_proxies: &Option>, + trusted_proxies: Option<&Vec>, ) -> Option { /* > There may be multiple X-Forwarded-For headers present in a request. The IP addresses in these headers must be treated as a single list, @@ -163,9 +163,7 @@ fn maybe_get_forwarded( */ .filter(|ip| { // trusted proxies provided REPLACES our default local proxies - let proxies = trusted_proxies - .as_ref() - .unwrap_or_else(|| get_local_trusted_proxies()); + let proxies = trusted_proxies.unwrap_or_else(|| get_local_trusted_proxies()); !proxies .iter() .any(|trusted_proxy| trusted_proxy.contains(*ip)) @@ -279,7 +277,7 @@ where fn call(&mut self, mut req: Request) -> Self::Future { let layer = self.layer.clone(); - let xff_ip = maybe_get_forwarded(req.headers(), &layer.trusted_proxies); + let xff_ip = maybe_get_forwarded(req.headers(), layer.trusted_proxies.as_ref()); let remote_ip = xff_ip.map_or_else( || { let ip = req @@ -329,21 +327,21 @@ mod tests { #[test] pub fn test_parsing() { - let res = maybe_get_forwarded(&xff(""), &None); + let res = maybe_get_forwarded(&xff(""), None); assert_debug_snapshot!(res); - let res = maybe_get_forwarded(&xff("foobar"), &None); + let res = maybe_get_forwarded(&xff("foobar"), None); assert_debug_snapshot!(res); - let res = maybe_get_forwarded(&xff("192.1.1.1"), &None); + let res = maybe_get_forwarded(&xff("192.1.1.1"), None); assert_debug_snapshot!(res); - let res = maybe_get_forwarded(&xff("51.50.51.50,10.0.0.1,192.168.1.1"), &None); + let res = maybe_get_forwarded(&xff("51.50.51.50,10.0.0.1,192.168.1.1"), None); assert_debug_snapshot!(res); - let res = maybe_get_forwarded(&xff("19.84.19.84,192.168.0.1"), &None); + let res = maybe_get_forwarded(&xff("19.84.19.84,192.168.0.1"), None); assert_debug_snapshot!(res); - let res = maybe_get_forwarded(&xff("b51.50.51.50b,/10.0.0.1-,192.168.1.1"), &None); + let res = maybe_get_forwarded(&xff("b51.50.51.50b,/10.0.0.1-,192.168.1.1"), None); assert_debug_snapshot!(res); let res = maybe_get_forwarded( &xff("51.50.51.50,192.1.1.1"), - &Some(vec![IpNetwork::from_str("192.1.1.1/8").unwrap()]), + Some(vec![IpNetwork::from_str("192.1.1.1/8").unwrap()]).as_ref(), ); assert_debug_snapshot!(res); @@ -351,7 +349,7 @@ mod tests { // remote IP and not skipped let res = maybe_get_forwarded( &xff("51.50.51.50,192.168.1.1"), - &Some(vec![IpNetwork::from_str("192.1.1.1/16").unwrap()]), + Some(vec![IpNetwork::from_str("192.1.1.1/16").unwrap()]).as_ref(), ); assert_debug_snapshot!(res); }