hyperlane-plugin-rewrite 23.1.0

A high-performance HTTP URL rewrite and redirect plugin for the Hyperlane framework featuring canonical host/HTTPS enforcement (X-Forwarded-Proto aware), trailing-slash and duplicate-slash normalization, O(1) XxHash3_64 static tables, segment-bounded prefix rules, and compiled regex rules.
Documentation
use super::*;

/// Fast zero-allocation 64-bit multiplicative hasher for internal exact-path lookup tables.
#[derive(Clone, Copy, Default)]
pub(crate) struct FxHasher {
    /// Running 64-bit hash accumulator.
    pub(crate) state: u64,
}

/// Static exact-path redirect or internal rewrite entry.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct ExactRewriteRule {
    /// Exact incoming path matched in `O(1)` via `XxHash3_64`.
    pub(crate) from: String,
    /// Replacement path or absolute redirect URL.
    pub(crate) to: String,
    /// Internal path rewrite or HTTP redirect status.
    #[get(type(copy))]
    pub(crate) action: RewriteAction,
    /// Whether to append the original query string when `to` contains no `'?'`.
    #[get(type(copy))]
    pub(crate) preserve_query: bool,
}

/// Segment-bounded path-prefix strip or rewrite entry.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct PrefixRewriteRule {
    /// Incoming path prefix matched on segment boundaries (for example `"/v1"`).
    pub(crate) from: String,
    /// Replacement prefix (empty string strips `from`, mapping `"/v1/users"` -> `"/users"`).
    pub(crate) to: String,
    /// Internal path rewrite or HTTP redirect status.
    #[get(type(copy))]
    pub(crate) action: RewriteAction,
    /// Whether to preserve the incoming query string on redirects.
    #[get(type(copy))]
    pub(crate) preserve_query: bool,
}

/// Regular-expression rewrite or redirect definition.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct RegexRewriteRule {
    /// Regular expression source pattern compiled once during [`CompiledRewrite::compile`].
    pub(crate) pattern: String,
    /// Replacement template supporting `$1`, `$2`, and named capture groups.
    pub(crate) replacement: String,
    /// Internal path rewrite or HTTP redirect status.
    #[get(type(copy))]
    pub(crate) action: RewriteAction,
    /// Whether to preserve the incoming query string on redirects.
    #[get(type(copy))]
    pub(crate) preserve_query: bool,
}

/// User-facing configuration for [`RewritePlugin`] and [`CompiledRewrite`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct RewriteConfig {
    /// Redirect non-HTTPS requests to `https://` (`X-Forwarded-Proto` / `Forwarded` aware).
    #[get(type(copy))]
    pub(crate) force_https: bool,
    /// Optional canonical host (for example `"example.com"` or `"api.example.com:8443"`).
    #[get(type(clone))]
    pub(crate) canonical_host: Option<String>,
    /// HTTP status code emitted for canonical host and HTTPS redirects.
    #[get(type(copy))]
    pub(crate) canonical_redirect_status: RedirectStatus,
    /// Whether to trust `X-Forwarded-Proto` and `Forwarded: proto=...` when detecting HTTPS.
    #[get(type(copy))]
    pub(crate) trust_forwarded_proto: bool,
    /// Collapse consecutive slashes (`"//api///v1"` -> `"/api/v1"`).
    #[get(type(copy))]
    pub(crate) collapse_duplicate_slashes: bool,
    /// Trailing-slash normalization mode (`Keep`, `Strip`, or `Enforce`).
    #[get(type(copy))]
    pub(crate) trailing_slash: TrailingSlashMode,
    /// Action taken when path normalization (duplicate or trailing slash) modifies the path.
    #[get(type(copy))]
    pub(crate) normalization_action: RewriteAction,
    /// Exact path rules indexed in an `O(1)` `XxHash3_64` lookup table.
    pub(crate) exact_rules: Vec<ExactRewriteRule>,
    /// Segment-bounded prefix rules evaluated longest-prefix first.
    pub(crate) prefix_rules: Vec<PrefixRewriteRule>,
    /// Regex rewrite rules evaluated in registration order after exact and prefix rules.
    pub(crate) regex_rules: Vec<RegexRewriteRule>,
}

/// Compiled exact-path target stored in the `O(1)` lookup table.
#[derive(Clone, Debug)]
pub(crate) struct CompiledExactTarget {
    /// Replacement path or URL.
    pub(crate) to: String,
    /// Internal rewrite or redirect action.
    pub(crate) action: RewriteAction,
    /// Whether to append the original query string on redirects.
    pub(crate) preserve_query: bool,
    /// Precomputed flag indicating whether `to` already contains `'?'`.
    pub(crate) has_query: bool,
}

/// Compiled segment-bounded prefix rule sorted longest-prefix first.
#[derive(Clone, Debug)]
pub(crate) struct CompiledPrefixRule {
    /// Normalized prefix (without trailing slash except `"/"`).
    pub(crate) from: String,
    /// Normalized replacement prefix (empty for pure prefix strip).
    pub(crate) to: String,
    /// Internal rewrite or redirect action.
    pub(crate) action: RewriteAction,
    /// Whether to append the original query string on redirects.
    pub(crate) preserve_query: bool,
    /// Precomputed flag indicating whether `to` already contains `'?'`.
    pub(crate) to_has_query: bool,
}

/// Precompiled regex rule ready for hot-path evaluation.
#[derive(Clone, Debug)]
pub(crate) struct CompiledRegexRule {
    /// Optimized matcher (literal, single-capture, or general compiled regex).
    pub(crate) matcher: CompiledRegexMatcher,
    /// Internal rewrite or redirect action.
    pub(crate) action: RewriteAction,
    /// Whether to append the original query string on redirects.
    pub(crate) preserve_query: bool,
}

/// Precompiled zero-allocation-fast-path URL rewrite & redirect engine backing [`RewritePlugin`].
#[derive(Clone, Debug)]
pub struct CompiledRewrite {
    /// Source configuration retained for inspection.
    pub(crate) config: RewriteConfig,
    /// Lowercased canonical host without redundant default port, if configured.
    pub(crate) canonical_host_lower: Option<String>,
    /// Prebuilt `"https://<canonical_host>"` origin string when canonical host is set.
    pub(crate) canonical_https_origin: Option<String>,
    /// Prebuilt `"http://<canonical_host>"` origin string when canonical host is set.
    pub(crate) canonical_http_origin: Option<String>,
    /// `O(1)` zero-allocation exact-path lookup table.
    pub(crate) exact_map: FxHashMap<String, CompiledExactTarget>,
    /// Longest-first segment-bounded prefix rewrite rules.
    pub(crate) prefix_rules: Vec<CompiledPrefixRule>,
    /// Precompiled regex rewrite rules (with regex-free fast matchers when applicable).
    pub(crate) regex_rules: Vec<CompiledRegexRule>,
    /// Optional combined `RegexSet` prefilter when `>= 2` general regex rules exist.
    pub(crate) general_regex_set: Option<RegexSet>,
}

/// Process-wide [`RewritePlugin`] singleton storing a [`CompiledRewrite`].
#[derive(Clone, Debug)]
pub struct RewritePlugin {
    /// Precompiled rewrite and redirect engine.
    pub(crate) compiled: CompiledRewrite,
}

/// Opt-in request middleware performing canonical redirects, path normalization, and URL rewrites.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-102)]
pub struct RewriteHook;