Struct actix_web_lab::middleware::NormalizePath
source · pub struct NormalizePath { /* private fields */ }
Expand description
Middleware for normalizing a request’s path so that routes can be matched more flexibly.
Normalization Steps
- Merges consecutive slashes into one. (For example,
/path//one
always becomes/path/one
.) - Appends a trailing slash if one is not present, removes one if present, or keeps trailing
slashes as-is, depending on which
TrailingSlash
variant is supplied tonew
.
Default Behavior
The default constructor chooses to strip trailing slashes from the end of paths with them
(TrailingSlash::Trim
). The implication is that route definitions should be defined without
trailing slashes or else they will be inaccessible (or vice versa when using the
TrailingSlash::Always
behavior), as shown in the example tests below.
Examples
use actix_web::{web, middleware, App};
let app = App::new()
.wrap(middleware::NormalizePath::trim())
.route("/test", web::get().to(|| async { "test" }))
.route("/unmatchable/", web::get().to(|| async { "unmatchable" }));
use actix_web::http::StatusCode;
use actix_web::test::{call_service, init_service, TestRequest};
let app = init_service(app).await;
let req = TestRequest::with_uri("/test").to_request();
let res = call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
let req = TestRequest::with_uri("/test/").to_request();
let res = call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
let req = TestRequest::with_uri("/unmatchable").to_request();
let res = call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/unmatchable/").to_request();
let res = call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
Implementations§
source§impl NormalizePath
impl NormalizePath
sourcepub fn new(behavior: TrailingSlash) -> Self
pub fn new(behavior: TrailingSlash) -> Self
Create new NormalizePath
middleware with the specified trailing slash style.
sourcepub fn trim() -> Self
pub fn trim() -> Self
Constructs a new NormalizePath
middleware with trim semantics.
Use this instead of NormalizePath::default()
to avoid deprecation warning.
sourcepub fn use_redirects(self) -> Self
pub fn use_redirects(self) -> Self
Configures middleware to respond to requests with non-normalized paths with a 307 redirect.
If configured
For example, a request with the path /api//v1/foo/
would receive a response with a
Location: /api/v1/foo
header (assuming Trim
trailing slash behavior.)
To customize the status code, use use_redirects_with
.
sourcepub fn use_redirects_with(self, status_code: StatusCode) -> Self
pub fn use_redirects_with(self, status_code: StatusCode) -> Self
Configures middleware to respond to requests with non-normalized paths with a redirect.
For example, a request with the path /api//v1/foo/
would receive a 307 response with a
Location: /api/v1/foo
header (assuming Trim
trailing slash behavior.)
Panics
Panics if status_code
is not a redirect (300-399).
Trait Implementations§
source§impl Clone for NormalizePath
impl Clone for NormalizePath
source§fn clone(&self) -> NormalizePath
fn clone(&self) -> NormalizePath
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more