mcpr_core/proxy/pipeline/middleware.rs
1//! Middleware traits for the pipeline.
2//!
3//! See `PIPELINE.md` §Middleware. `RequestMiddleware` inspects and may
4//! transform or short-circuit a `Request`; `ResponseMiddleware` mirrors
5//! the pattern on the way out.
6//!
7//! The driver that runs these traits lives in [`super::driver`].
8
9use async_trait::async_trait;
10
11use super::values::{Context, Request, Response};
12
13/// Decision a request middleware returns.
14#[derive(Debug)]
15pub enum Flow {
16 /// Forward the (possibly transformed) request to the next
17 /// middleware.
18 Continue(Request),
19 /// Produce a response directly. Later request middlewares and the
20 /// router/transport do not run, but the full response chain still
21 /// processes the response.
22 ShortCircuit(Response),
23}
24
25#[async_trait]
26pub trait RequestMiddleware: Send + Sync {
27 /// Stable identifier used for `info!` registration logs and test
28 /// introspection. Return a `&'static str` literal.
29 fn name(&self) -> &'static str;
30
31 /// Inspect or transform the request. Variants the middleware does
32 /// not care about return `Flow::Continue(req)` unchanged.
33 async fn on_request(&self, req: Request, cx: &mut Context) -> Flow;
34}
35
36#[async_trait]
37pub trait ResponseMiddleware: Send + Sync {
38 fn name(&self) -> &'static str;
39
40 /// Inspect or transform the response. Always returns a `Response`
41 /// — replacing one variant with another is how a middleware swaps
42 /// the result.
43 async fn on_response(&self, resp: Response, cx: &mut Context) -> Response;
44}