Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware: Send + Sync {
    // Provided methods
    fn before<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _request: &'life1 mut Request,
    ) -> Pin<Box<dyn Future<Output = Result<Outcome, WebError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn after<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        _request: &'life1 Request,
        _response: &'life2 mut ReplyData,
    ) -> Pin<Box<dyn Future<Output = Result<(), WebError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

A trait for implementing middleware.

before runs in registration order; after runs in reverse — so a middleware wraps the ones added after it ([A, B]: A.before, B.before, handler, B.after, A.after).

Provided Methods§

Source

fn before<'life0, 'life1, 'async_trait>( &'life0 self, _request: &'life1 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Outcome, WebError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called before the request is routed. Return Outcome::Continue to proceed, Outcome::Respond to short-circuit with a normal response, or Err(WebError) to short-circuit with an error response. The default implementation continues.

Source

fn after<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _request: &'life1 Request, _response: &'life2 mut ReplyData, ) -> Pin<Box<dyn Future<Output = Result<(), WebError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called after the handler returns (or after a before-hook Outcome::Respond short-circuit), on the way back out. request is the request the reply is for — so the hook can decide what to do based on the request’s headers / method / path. Mutate response in place (including replacing it wholesale, or stamping headers via ReplyData::add_header); Err(WebError) swaps the reply for an error response. The default implementation does nothing.

after is not called on ReplyData::Upgrade replies — a WebSocket handshake’s 101 Switching Protocols has no body to decorate, and the server short-circuits that variant to the upgrade machinery before the after chain runs.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§