1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Pre-route middleware registry.
//!
//! Pre-route middleware runs before route matching, allowing path rewrites that
//! affect which route handler is selected. Standard global middleware runs after
//! route matching (inside the handler chain) and cannot influence routing.
//!
//! # Example
//!
//! ```rust,ignore
//! use ferro::{pre_route_middleware, PreRouteMiddleware, Request, HttpResponse};
//! use async_trait::async_trait;
//!
//! pub struct HostMiddleware;
//!
//! #[async_trait]
//! impl PreRouteMiddleware for HostMiddleware {
//! async fn rewrite(&self, request: Request) -> Result<Request, HttpResponse> {
//! // Inspect Host header, call request.set_path() if needed, then:
//! Ok(request)
//! // Or to short-circuit:
//! // Err(HttpResponse::new().status(404).set_body("Not found"))
//! }
//! }
//!
//! // In bootstrap.rs:
//! pre_route_middleware!(HostMiddleware::new());
//! ```
use crate;
use async_trait;
use RwLock;
use ;
/// Trait for middleware that runs before route matching.
///
/// Implement `rewrite` to inspect and/or rewrite the request path. The method
/// receives ownership of the request and must return either the (possibly modified)
/// request to continue routing, or an `HttpResponse` to short-circuit immediately.
///
/// Short-circuiting is appropriate for cases like unknown custom domains (return 404)
/// where the request should never reach route matching.
/// Type alias for a boxed pre-route middleware.
pub type BoxedPreRouteMiddleware = ;
/// Global pre-route middleware registry.
static PRE_ROUTE_MIDDLEWARE: = new;
/// Register a pre-route middleware that runs before route matching on every request.
///
/// Called by the `pre_route_middleware!` macro. Middleware runs in registration order.
/// Get all registered pre-route middleware.
///
/// Used internally by `server.rs` before route matching.