Skip to content

Commit 754b325

Browse files
committed
🐛 fix origin infer order
1 parent 947b775 commit 754b325

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

src/registry.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,15 @@ fn infer_origin(headers: &HeaderMap) -> Result<String> {
1313
let mut scheme = "http".to_string();
1414
let mut host = "".to_string();
1515

16-
if let Some(uri_str) = headers.get("x-forwarded-uri").and_then(|v| v.to_str().ok()) {
17-
if let Ok(u) = uri::Uri::try_from(uri_str) {
18-
if let Some(s) = u.scheme_str() {
19-
scheme = s.to_string();
20-
}
21-
if let Some(h) = u.host() {
22-
host = h.to_string();
23-
}
24-
}
25-
}
26-
16+
// directly return if x-forwarded-origin is present
2717
if let Some(origin) = headers
2818
.get("x-forwarded-origin")
2919
.and_then(|v| v.to_str().ok())
3020
{
3121
return Ok(origin.to_string());
3222
}
3323

34-
if let Some(h) = headers
35-
.get("x-forwarded-host")
36-
.and_then(|v| v.to_str().ok())
37-
{
38-
host = h.to_string();
39-
}
24+
// infer scheme and host
4025

4126
if let Some(s) = headers
4227
.get("x-forwarded-proto")
@@ -49,6 +34,24 @@ fn infer_origin(headers: &HeaderMap) -> Result<String> {
4934
host = h.to_string();
5035
}
5136

37+
if let Some(h) = headers
38+
.get("x-forwarded-host")
39+
.and_then(|v| v.to_str().ok())
40+
{
41+
host = h.to_string();
42+
}
43+
44+
if let Some(uri_str) = headers.get("x-forwarded-uri").and_then(|v| v.to_str().ok()) {
45+
if let Ok(u) = uri::Uri::try_from(uri_str) {
46+
if let Some(s) = u.scheme_str() {
47+
scheme = s.to_string();
48+
}
49+
if let Some(h) = u.host() {
50+
host = h.to_string();
51+
}
52+
}
53+
}
54+
5255
if !host.is_empty() {
5356
return Ok(format!("{}://{}", scheme, host));
5457
}
@@ -184,7 +187,12 @@ pub async fn proxy_request(
184187
}
185188
Err(_) => {
186189
// If location is a relative path, just prepend the origin
187-
format!("{}{}", origin, loc)
190+
let p = if loc.starts_with('/') {
191+
loc.to_string()
192+
} else {
193+
format!("/{}", loc)
194+
};
195+
format!("{}{}", origin, p)
188196
}
189197
};
190198
if let Ok(new_value) = HeaderValue::from_str(&new_location) {

0 commit comments

Comments
 (0)