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//onealways 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
TrailingSlashvariant 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::{middleware, web, 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,
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 moreSource§impl Debug for NormalizePath
impl Debug for NormalizePath
Source§impl Default for NormalizePath
impl Default for NormalizePath
Source§impl<S, B> Transform<S, ServiceRequest> for NormalizePath
impl<S, B> Transform<S, ServiceRequest> for NormalizePath
Source§type Response = ServiceResponse<EitherBody<B, ()>>
type Response = ServiceResponse<EitherBody<B, ()>>
Source§type Future = Ready<Result<<NormalizePath as Transform<S, ServiceRequest>>::Transform, <NormalizePath as Transform<S, ServiceRequest>>::InitError>>
type Future = Ready<Result<<NormalizePath as Transform<S, ServiceRequest>>::Transform, <NormalizePath as Transform<S, ServiceRequest>>::InitError>>
Source§fn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
impl Copy for NormalizePath
Auto Trait Implementations§
impl Freeze for NormalizePath
impl RefUnwindSafe for NormalizePath
impl Send for NormalizePath
impl Sync for NormalizePath
impl Unpin for NormalizePath
impl UnwindSafe for NormalizePath
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more