Skip to content

Commit 3145e3a

Browse files
committed
Fix resolve the real IP address through the X-Forwarded-For header #31
1 parent 170ee36 commit 3145e3a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/http/request.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,13 @@ fn clear_path_end_slash(input: &str) -> &str {
278278
}
279279

280280
fn trust_addr_proxy(headers : &CIHashMap<String>,remote_addr : &str) -> String {
281-
if let Some(remote_ip) = headers.get("X-Real-IP") {
282-
remote_ip.to_string()
283-
} else {
284-
remote_addr.to_string()
285-
}
281+
headers.get("X-Real-IP")
282+
.map(|s| s.as_str())
283+
.or_else(|| {
284+
headers.get("X-Forwarded-For").and_then(|s| {
285+
s.split(',').next().map(|ip| ip.trim())
286+
})
287+
}).unwrap_or(remote_addr).to_string()
286288
}
287289

288290
//form-data-parser

0 commit comments

Comments
 (0)