Skip to main content

hyperlane_plugin_cors/
struct.rs

1use super::*;
2
3/// User-facing configuration for [`CorsPlugin`].
4#[derive(Clone, Data, Debug)]
5pub struct CorsConfig {
6    /// Origin matching rule controlling `Access-Control-Allow-Origin`.
7    pub(crate) allow_origin: AllowOrigin,
8    /// Allowed HTTP methods emitted in preflight `Access-Control-Allow-Methods`.
9    pub(crate) allow_methods: Vec<String>,
10    /// Allowed request headers emitted in preflight `Access-Control-Allow-Headers` (`*` or empty reflects requested headers).
11    pub(crate) allow_headers: Vec<String>,
12    /// Response headers exposed to browser scripts via `Access-Control-Expose-Headers`.
13    pub(crate) expose_headers: Vec<String>,
14    /// Whether `Access-Control-Allow-Credentials: true` is emitted.
15    #[get(type(copy))]
16    pub(crate) allow_credentials: bool,
17    /// Optional preflight cache lifetime in seconds (`Access-Control-Max-Age`).
18    #[get(type(copy))]
19    pub(crate) max_age_secs: Option<u64>,
20    /// Whether to answer `Access-Control-Request-Private-Network: true` with `Access-Control-Allow-Private-Network: true`.
21    #[get(type(copy))]
22    pub(crate) allow_private_network: bool,
23}
24
25/// Precompiled zero-allocation CORS policy stored inside [`CorsPlugin`].
26#[derive(Clone, Debug)]
27pub struct CompiledCors {
28    /// Original configuration retained for inspection and origin matching.
29    pub(crate) config: CorsConfig,
30    /// Small flat slice of lowercased exact origins scanned linearly when `AllowOrigin::Exact` has `<= 8` entries.
31    pub(crate) exact_small: Option<Box<[String]>>,
32    /// `true` when `AllowOrigin::Any` and `!allow_credentials` (always emits `"*"`).
33    pub(crate) is_any_wildcard: bool,
34    /// Pre-joined `Access-Control-Allow-Methods` value (pre-validated for HTTP field-value safety).
35    pub(crate) methods_header: String,
36    /// `true` when preflight `Access-Control-Allow-Headers` mirrors incoming `Access-Control-Request-Headers`.
37    pub(crate) mirror_headers: bool,
38    /// Pre-joined `Access-Control-Allow-Headers` value when configured and valid (`None` otherwise).
39    pub(crate) headers_header: Option<String>,
40    /// Pre-joined `Access-Control-Expose-Headers` value (pre-validated).
41    pub(crate) expose_header: Option<String>,
42    /// Pre-formatted `Access-Control-Max-Age` value.
43    pub(crate) max_age_header: Option<String>,
44    /// Pre-formatted static preflight HTTP/1.1 header lines (credentials, methods, fixed headers, max-age, vary).
45    pub(crate) preflight_static_lines: Box<[u8]>,
46    /// `true` when `Access-Control-Allow-Origin` varies by request `Origin`.
47    pub(crate) vary_origin: bool,
48}
49
50/// Process-wide [`CorsPlugin`] singleton holding a precompiled [`CompiledCors`] policy.
51#[derive(Clone, Debug)]
52pub struct CorsPlugin {
53    /// Precompiled CORS header policy.
54    pub(crate) compiled: CompiledCors,
55}
56
57/// Opt-in request middleware applying CORS headers and answering `OPTIONS` preflight requests.
58#[derive(Clone, Copy, Debug, Default)]
59#[hyperlane::request_middleware(-100)]
60pub struct CorsRequestHook;
61
62/// Opt-in response middleware guaranteeing CORS headers persist even if a route replaced headers.
63#[derive(Clone, Copy, Debug, Default)]
64#[hyperlane::response_middleware(-80)]
65pub struct CorsResponseHook;