Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware: Send + Sync {
    // Required method
    fn handle(
        &self,
        request: &Request,
        connection: &ConnectionInfo,
        next: &dyn Application,
    ) -> Result<Response, String>;

    // Provided methods
    fn runs_inline(
        &self,
        _request: &Request,
        _connection: &ConnectionInfo,
    ) -> bool { ... }
    fn is_passthrough(
        &self,
        _request: &Request,
        _connection: &ConnectionInfo,
    ) -> bool { ... }
    fn accepts_streamed_body(
        &self,
        _request: &Request,
        _connection: &ConnectionInfo,
    ) -> bool { ... }
    fn answers_streamed_body(
        &self,
        _request: &Request,
        _connection: &ConnectionInfo,
    ) -> bool { ... }
    fn layer_name(&self) -> &'static str { ... }
}
Expand description

A single middleware layer.

Receive the request, call next.execute(request, connection) to pass control to the next layer (or the inner application), and optionally inspect or transform the resulting response.

Short-circuit by returning a Response (or Err) without calling next.

Required Methods§

Source

fn handle( &self, request: &Request, connection: &ConnectionInfo, next: &dyn Application, ) -> Result<Response, String>

Provided Methods§

Source

fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool

true when this layer’s own work for request never blocks (in-memory state, atomics, short mutexes) — see Application::runs_inline. A WithMiddleware stack runs inline only if every layer and the inner application agree. Default false.

Source

fn is_passthrough( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool

true when handle would do nothing for request but call next (e.g. a rate limit that isn’t configured). A WithMiddleware stack offers the inner application’s execute_async only if every layer passes the request through. Default false.

Source

fn accepts_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool

true when this layer never reads request.body, so the inner application may receive it as a stream (Application::streams_body). A layer that inspects the body (CSRF tokens in a form, request signatures, rewriting) must keep the default false: it would see an empty body.

Source

fn answers_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool

true when this layer answers request itself — it doesn’t call next — and reads its body as a stream (a proxy forwarding it as it arrives). The body is then streamed whatever the layers after it and the application would say. Requires accepts_streamed_body. Since 18.3.0.

Source

fn layer_name(&self) -> &'static str

A short name for this layer, as tools that describe an app list it (Application::middleware_names): by default the type’s name without its module path or generic parameters (CacheLayer). Since 18.28.0.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Middleware for AuthServer

Source§

impl Middleware for AuthzLayer

Source§

impl Middleware for BlocklistLayer

Source§

impl Middleware for CacheLayer

Source§

impl Middleware for CanaryLayer

Source§

impl Middleware for CsrfLayer

Source§

impl Middleware for EarlyHintsLayer

Source§

impl Middleware for ForwardAuthLayer

Source§

impl Middleware for GrpcProxy

Available on crate feature http2 only.
Source§

impl Middleware for GrpcWebLayer

Source§

impl Middleware for H2ReverseProxy

Available on crate feature http2 only.
Source§

impl Middleware for H3ReverseProxy

Available on crate feature http3 only.
Source§

impl Middleware for IpFilter

Source§

impl Middleware for JwtLayer

Source§

impl Middleware for LogLayer

Source§

impl Middleware for MaintenanceLayer

Source§

impl Middleware for McpAuditLayer

Source§

impl Middleware for MetricsLayer

Source§

impl Middleware for MirrorLayer

Source§

impl Middleware for OidcAuth

Source§

impl Middleware for OtelLayer

Source§

impl Middleware for RateLimitLayer

Source§

impl Middleware for RequestIdLayer

Source§

impl Middleware for RetryLayer

Source§

impl Middleware for ReverseProxy

Source§

impl Middleware for RewriteLayer

Source§

impl Middleware for SamlSp

Source§

impl Middleware for WafLayer

Source§

impl Middleware for WasmLayer

Source§

impl<F> Middleware for BasicAuthLayer<F>
where F: Fn(&str, &str) -> bool + Send + Sync + 'static,

Source§

impl<L, ResBody> Middleware for TowerLayer<L>
where L: for<'a> Layer<Next<'a>> + Send + Sync, <L as Layer<Next<'a>>>::Service: for<'a> Service<Request<Body>, Response = Response<ResBody>>, <<L as Layer<Next<'a>>>::Service as Service<Request<Body>>>::Error: for<'a> Into<Box<dyn Error + Send + Sync>>, ResBody: Body<Data = Bytes> + Send + 'static, <ResBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>,