hyperlane_plugin_rewrite/struct.rs
1use super::*;
2
3/// Static exact-path redirect or internal rewrite entry.
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct ExactRewriteRule {
6 /// Exact incoming path matched in `O(1)` via `XxHash3_64`.
7 pub(crate) from: String,
8 /// Replacement path or absolute redirect URL.
9 pub(crate) to: String,
10 /// Internal path rewrite or HTTP redirect status.
11 #[get(type(copy))]
12 pub(crate) action: RewriteAction,
13 /// Whether to append the original query string when `to` contains no `'?'`.
14 #[get(type(copy))]
15 pub(crate) preserve_query: bool,
16}
17
18/// Segment-bounded path-prefix strip or rewrite entry.
19#[derive(Clone, Data, Debug, Eq, PartialEq)]
20pub struct PrefixRewriteRule {
21 /// Incoming path prefix matched on segment boundaries (for example `"/v1"`).
22 pub(crate) from: String,
23 /// Replacement prefix (empty string strips `from`, mapping `"/v1/users"` -> `"/users"`).
24 pub(crate) to: String,
25 /// Internal path rewrite or HTTP redirect status.
26 #[get(type(copy))]
27 pub(crate) action: RewriteAction,
28 /// Whether to preserve the incoming query string on redirects.
29 #[get(type(copy))]
30 pub(crate) preserve_query: bool,
31}
32
33/// Regular-expression rewrite or redirect definition.
34#[derive(Clone, Data, Debug, Eq, PartialEq)]
35pub struct RegexRewriteRule {
36 /// Regular expression source pattern compiled once during [`CompiledRewrite::compile`].
37 pub(crate) pattern: String,
38 /// Replacement template supporting `$1`, `$2`, and named capture groups.
39 pub(crate) replacement: String,
40 /// Internal path rewrite or HTTP redirect status.
41 #[get(type(copy))]
42 pub(crate) action: RewriteAction,
43 /// Whether to preserve the incoming query string on redirects.
44 #[get(type(copy))]
45 pub(crate) preserve_query: bool,
46}
47
48/// User-facing configuration for [`RewritePlugin`] and [`CompiledRewrite`].
49#[derive(Clone, Data, Debug, Eq, PartialEq)]
50pub struct RewriteConfig {
51 /// Redirect non-HTTPS requests to `https://` (`X-Forwarded-Proto` / `Forwarded` aware).
52 #[get(type(copy))]
53 pub(crate) force_https: bool,
54 /// Optional canonical host (for example `"example.com"` or `"api.example.com:8443"`).
55 #[get(type(clone))]
56 pub(crate) canonical_host: Option<String>,
57 /// HTTP status code emitted for canonical host and HTTPS redirects.
58 #[get(type(copy))]
59 pub(crate) canonical_redirect_status: RedirectStatus,
60 /// Whether to trust `X-Forwarded-Proto` and `Forwarded: proto=...` when detecting HTTPS.
61 #[get(type(copy))]
62 pub(crate) trust_forwarded_proto: bool,
63 /// Collapse consecutive slashes (`"//api///v1"` -> `"/api/v1"`).
64 #[get(type(copy))]
65 pub(crate) collapse_duplicate_slashes: bool,
66 /// Trailing-slash normalization mode (`Keep`, `Strip`, or `Enforce`).
67 #[get(type(copy))]
68 pub(crate) trailing_slash: TrailingSlashMode,
69 /// Action taken when path normalization (duplicate or trailing slash) modifies the path.
70 #[get(type(copy))]
71 pub(crate) normalization_action: RewriteAction,
72 /// Exact path rules indexed in an `O(1)` `XxHash3_64` lookup table.
73 pub(crate) exact_rules: Vec<ExactRewriteRule>,
74 /// Segment-bounded prefix rules evaluated longest-prefix first.
75 pub(crate) prefix_rules: Vec<PrefixRewriteRule>,
76 /// Regex rewrite rules evaluated in registration order after exact and prefix rules.
77 pub(crate) regex_rules: Vec<RegexRewriteRule>,
78}
79
80/// Compiled exact-path target stored in the `O(1)` `XxHash3_64` map.
81#[derive(Clone, Debug)]
82pub(crate) struct CompiledExactTarget {
83 /// Replacement path or URL.
84 pub(crate) to: String,
85 /// Internal rewrite or redirect action.
86 pub(crate) action: RewriteAction,
87 /// Whether to append the original query string on redirects.
88 pub(crate) preserve_query: bool,
89}
90
91/// Compiled segment-bounded prefix rule sorted longest-prefix first.
92#[derive(Clone, Debug)]
93pub(crate) struct CompiledPrefixRule {
94 /// Normalized prefix (without trailing slash except `"/"`).
95 pub(crate) from: String,
96 /// Normalized replacement prefix (empty for pure prefix strip).
97 pub(crate) to: String,
98 /// Internal rewrite or redirect action.
99 pub(crate) action: RewriteAction,
100 /// Whether to append the original query string on redirects.
101 pub(crate) preserve_query: bool,
102}
103
104/// Precompiled regex rule ready for hot-path evaluation.
105#[derive(Clone, Debug)]
106pub(crate) struct CompiledRegexRule {
107 /// Precompiled regular expression engine.
108 pub(crate) regex: Regex,
109 /// Replacement template supporting capture groups.
110 pub(crate) replacement: String,
111 /// Internal rewrite or redirect action.
112 pub(crate) action: RewriteAction,
113 /// Whether to append the original query string on redirects.
114 pub(crate) preserve_query: bool,
115}
116
117/// Precompiled zero-allocation-fast-path URL rewrite & redirect engine backing [`RewritePlugin`].
118#[derive(Clone, Debug)]
119pub struct CompiledRewrite {
120 /// Source configuration retained for inspection.
121 pub(crate) config: RewriteConfig,
122 /// Lowercased canonical host without redundant default port, if configured.
123 pub(crate) canonical_host_lower: Option<String>,
124 /// `O(1)` `XxHash3_64` exact-path lookup table.
125 pub(crate) exact_map: HashMapXxHash3_64<String, CompiledExactTarget>,
126 /// Longest-first segment-bounded prefix rewrite rules.
127 pub(crate) prefix_rules: Vec<CompiledPrefixRule>,
128 /// Precompiled regex rewrite rules.
129 pub(crate) regex_rules: Vec<CompiledRegexRule>,
130}
131
132/// Process-wide [`RewritePlugin`] singleton storing a [`CompiledRewrite`].
133#[derive(Clone, Debug)]
134pub struct RewritePlugin {
135 /// Precompiled rewrite and redirect engine.
136 pub(crate) compiled: CompiledRewrite,
137}
138
139/// Opt-in request middleware performing canonical redirects, path normalization, and URL rewrites.
140#[derive(Clone, Copy, Debug, Default)]
141#[hyperlane::request_middleware(-102)]
142pub struct RewriteHook;