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