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§
fn handle( &self, request: &Request, connection: &ConnectionInfo, next: &dyn Application, ) -> Result<Response, String>
Provided Methods§
Sourcefn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
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.
Sourcefn is_passthrough(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
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.
Sourcefn accepts_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
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.
Sourcefn answers_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
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.
Sourcefn layer_name(&self) -> &'static str
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§
impl Middleware for AuthServer
sso-server and sso only.impl Middleware for AuthzLayer
impl Middleware for BlocklistLayer
impl Middleware for CacheLayer
impl Middleware for CsrfLayer
csrf only.impl Middleware for EarlyHintsLayer
impl Middleware for ForwardAuthLayer
auth only.impl Middleware for IpFilter
impl Middleware for JwtLayer
auth only.impl Middleware for LogLayer
impl Middleware for MaintenanceLayer
impl Middleware for MetricsLayer
impl Middleware for OidcAuth
sso only.impl Middleware for OtelLayer
impl Middleware for RateLimitLayer
impl Middleware for RequestIdLayer
impl Middleware for RetryLayer
impl Middleware for RewriteLayer
impl Middleware for SamlSp
sso-saml and sso only.impl<F: Fn(&str, &str) -> bool + Send + Sync + 'static> Middleware for BasicAuthLayer<F>
auth only.