Redirect trailing slashes
This commit is contained in:
@@ -130,6 +130,38 @@ impl PageServer {
|
||||
.and_then(|x| x.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
// Normalize url with redirect
|
||||
if route.ends_with('/') || route.contains("//") || route.starts_with('/') {
|
||||
let mut new_route = route.clone();
|
||||
while new_route.contains("//") {
|
||||
new_route = new_route.replace("//", "/");
|
||||
}
|
||||
let new_route = new_route.trim_matches('/');
|
||||
|
||||
trace!(
|
||||
message = "Redirecting route",
|
||||
route,
|
||||
new_route,
|
||||
addr = ?addr.addr,
|
||||
user_agent = ua,
|
||||
device_type = ?client_info.device_type
|
||||
);
|
||||
|
||||
let mut headers = HeaderMap::with_capacity(2);
|
||||
|
||||
let new_route = match HeaderValue::from_str(&format!("/{new_route}")) {
|
||||
Ok(x) => x,
|
||||
Err(_) => {
|
||||
// Be extra careful, this is user-provided data
|
||||
return StatusCode::BAD_REQUEST.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
headers.append(header::LOCATION, new_route);
|
||||
headers.append("Accept-CH", HeaderValue::from_static("Sec-CH-UA-Mobile"));
|
||||
return (StatusCode::PERMANENT_REDIRECT, headers).into_response();
|
||||
}
|
||||
|
||||
trace!(
|
||||
message = "Serving route",
|
||||
route,
|
||||
|
||||
Reference in New Issue
Block a user