mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-23 16:21:06 +01:00
fix trimming to inaccessible root path (#1678)
This commit is contained in:
parent
4b4c9d1b93
commit
7f8073233a
@ -1,6 +1,10 @@
|
||||
# Changes
|
||||
|
||||
## Unreleased - 2020-xx-xx
|
||||
### Fixed
|
||||
* `NormalizePath` when used with `TrailingSlash::Trim` no longer trims the root path "/". [#1678]
|
||||
|
||||
[#1678]: https://github.com/actix/actix-web/pull/1678
|
||||
|
||||
|
||||
## 3.0.1 - 2020-09-13
|
||||
|
@ -79,6 +79,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct NormalizePathNormalization<S> {
|
||||
service: S,
|
||||
merge_slash: Regex,
|
||||
@ -113,6 +114,10 @@ where
|
||||
// normalize multiple /'s to one /
|
||||
let path = self.merge_slash.replace_all(&path, "/");
|
||||
|
||||
// Ensure root paths are still resolvable. If resulting path is blank after previous step
|
||||
// it means the path was one or more slashes. Reduce to single slash.
|
||||
let path = if path.is_empty() { "/" } else { path.as_ref() };
|
||||
|
||||
// Check whether the path has been changed
|
||||
//
|
||||
// This check was previously implemented as string length comparison
|
||||
@ -158,10 +163,23 @@ mod tests {
|
||||
let mut app = init_service(
|
||||
App::new()
|
||||
.wrap(NormalizePath::default())
|
||||
.service(web::resource("/").to(HttpResponse::Ok))
|
||||
.service(web::resource("/v1/something/").to(HttpResponse::Ok)),
|
||||
)
|
||||
.await;
|
||||
|
||||
let req = TestRequest::with_uri("/").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let req = TestRequest::with_uri("/?query=test").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let req = TestRequest::with_uri("///").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let req = TestRequest::with_uri("/v1//something////").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
@ -184,10 +202,24 @@ mod tests {
|
||||
let mut app = init_service(
|
||||
App::new()
|
||||
.wrap(NormalizePath(TrailingSlash::Trim))
|
||||
.service(web::resource("/").to(HttpResponse::Ok))
|
||||
.service(web::resource("/v1/something").to(HttpResponse::Ok)),
|
||||
)
|
||||
.await;
|
||||
|
||||
// root paths should still work
|
||||
let req = TestRequest::with_uri("/").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let req = TestRequest::with_uri("/?query=test").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let req = TestRequest::with_uri("///").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let req = TestRequest::with_uri("/v1/something////").to_request();
|
||||
let res = call_service(&mut app, req).await;
|
||||
assert!(res.status().is_success());
|
||||
|
Loading…
Reference in New Issue
Block a user