mirror of
https://github.com/redlib-org/redlib.git
synced 2025-04-07 06:57:39 +03:00
Fix #140
This commit is contained in:
parent
10c73fad7f
commit
20f6945160
2 changed files with 12 additions and 5 deletions
13
src/main.rs
13
src/main.rs
|
@ -43,10 +43,17 @@ where
|
|||
#[async_trait]
|
||||
impl<State: Clone + Send + Sync + 'static> Middleware<State> for NormalizePath {
|
||||
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> tide::Result {
|
||||
if !request.url().path().ends_with('/') {
|
||||
Ok(Response::builder(301).header("Location", format!("{}/", request.url().path())).build())
|
||||
} else {
|
||||
let path = request.url().path();
|
||||
let query = request.url().query().unwrap_or_default();
|
||||
if path.ends_with('/') {
|
||||
Ok(next.run(request).await)
|
||||
} else {
|
||||
let normalized = if query != "" {
|
||||
format!("{}/?{}", path.replace("//", "/"), query)
|
||||
} else {
|
||||
format!("{}/", path.replace("//", "/"))
|
||||
};
|
||||
Ok(redirect(normalized))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue