Skip to content

Commit 493faef

Browse files
authored
Merge pull request #6 from habibi-dev/develop
✨ refactor(ip): Simplify IP address extraction logic using chained `&&`
2 parents 06bad5a + a9f3b9d commit 493faef

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

src/services/ip/mod.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ pub fn routers_list() -> Vec<(&'static str, MethodRouter)> {
1010
}
1111

1212
pub fn get_client_ip(headers: &HeaderMap) -> Option<IpAddr> {
13-
// 1) X-Forwarded-For → first IP
14-
if let Some(val) = headers.get("x-forwarded-for").and_then(|v| v.to_str().ok()) {
15-
if let Some(first) = val.split(',').next() {
16-
if let Ok(ip) = first.trim().parse::<IpAddr>() {
17-
return Some(ip);
18-
}
19-
}
13+
// X-Forwarded-For: take the first IP
14+
if let Some(val) = headers.get("x-forwarded-for").and_then(|v| v.to_str().ok())
15+
&& let Some(first) = val.split(',').next()
16+
&& let Ok(ip) = first.trim().parse::<IpAddr>()
17+
{
18+
return Some(ip);
2019
}
2120

22-
// 2) X-Real-IP
23-
if let Some(val) = headers.get("x-real-ip").and_then(|v| v.to_str().ok()) {
24-
if let Ok(ip) = val.trim().parse::<IpAddr>() {
25-
return Some(ip);
26-
}
21+
// X-Real-IP
22+
if let Some(val) = headers.get("x-real-ip").and_then(|v| v.to_str().ok())
23+
&& let Ok(ip) = val.trim().parse::<IpAddr>()
24+
{
25+
return Some(ip);
2726
}
2827

2928
None

0 commit comments

Comments
 (0)