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::{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
§type Response = ServiceResponse<EitherBody<B, ()>>
type Response = ServiceResponse<EitherBody<B, ()>>
§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: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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