Struct actix_web::middleware::NormalizePath
source · [−]pub struct NormalizePath(_);
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
sourceimpl NormalizePath
impl NormalizePath
sourcepub fn new(trailing_slash_style: TrailingSlash) -> Self
pub fn new(trailing_slash_style: TrailingSlash) -> Self
Create new NormalizePath
middleware with the specified trailing slash style.
Trait Implementations
sourceimpl Clone for NormalizePath
impl Clone for NormalizePath
sourcefn clone(&self) -> NormalizePath
fn clone(&self) -> NormalizePath
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for NormalizePath
impl Debug for NormalizePath
sourceimpl Default for NormalizePath
impl Default for NormalizePath
sourceimpl<S, B> Transform<S, ServiceRequest> for NormalizePath where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
impl<S, B> Transform<S, ServiceRequest> for NormalizePath where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
type Response = ServiceResponse<B>
type Response = ServiceResponse<B>
Responses produced by the service.
type Transform = NormalizePathNormalization<S>
type Transform = NormalizePathNormalization<S>
The TransformService
value created by this factory
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>>
The future response value.
sourcefn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously
impl Copy for NormalizePath
Auto Trait Implementations
impl RefUnwindSafe for NormalizePath
impl Send for NormalizePath
impl Sync for NormalizePath
impl Unpin for NormalizePath
impl UnwindSafe for NormalizePath
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more