Struct actix_web::middleware::NormalizePath[][src]

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 to new.

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

Create new NormalizePath middleware with the specified trailing slash style.

Constructs a new NormalizePath middleware with trim semantics.

Use this instead of NormalizePath::default() to avoid deprecation warning.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Responses produced by the service.

Errors produced by the service.

The TransformService value created by this factory

Errors produced while building a transform service.

The future response value.

Creates and returns a new Transform component, asynchronously

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.