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§
source§impl 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§
source§impl Clone for NormalizePath
impl Clone for NormalizePath
source§fn clone(&self) -> NormalizePath
fn clone(&self) -> NormalizePath
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
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 NormalizePathwhere
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
impl<S, B> Transform<S, ServiceRequest> for NormalizePathwhere
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 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.
source§fn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously