pub trait Middleware: Send + Sync {
// Required method
fn handle_request<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Result<Outcome, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn handle_response<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
response: Response,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn middleware(self) -> MiddlewareHandler
where Self: Sized + 'static { ... }
fn middleware_name(&self) -> &'static str { ... }
}Expand description
HTTP middleware, code which runs before a request is sent to a controller and after the controller has provided a response, allowing it to modify or interecept requests, and modify responses.
§Example
struct RequireHeader;
#[async_trait]
impl Middleware for RequireHeader {
// Process request and determine if it should be
// allowed to proceed.
async fn handle_request(
&self,
request: Request
) -> Result<Outcome, Error> {
if request.header("X-Custom-Header").is_some() {
Ok(Outcome::Forward(request))
} else {
Ok(Outcome::Stop(request, Response::forbidden()))
}
}
// Optionally, modify response returned by controller.
async fn handle_response(
&self,
request: &Request,
response: Response
) -> Result<Response, Error> {
let response = response.header("X-Middleware", "1");
Ok(response)
}
}Required Methods§
Sourcefn handle_request<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Result<Outcome, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle_request<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Result<Outcome, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Process the request before it reaches the controller. You can modify it, forward it without modification, or block the request entirely and return a response.
Provided Methods§
Sourcefn handle_response<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
response: Response,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn handle_response<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
response: Response,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Process the response returned by a controller. You can modify it or forward it without modification.
Sourcefn middleware(self) -> MiddlewareHandlerwhere
Self: Sized + 'static,
fn middleware(self) -> MiddlewareHandlerwhere
Self: Sized + 'static,
Get the middleware handler. This method
is used when adding middleware to a MiddlewareSet.
Sourcefn middleware_name(&self) -> &'static str
fn middleware_name(&self) -> &'static str
Name of this middleware. It’s globally unique so it should not be overriden.