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
/// Context attribute key preserving the pre-rewrite request path after an internal rewrite.
pub const ATTR_ORIGINAL_PATH: &str = "hyperlane_plugin_rewrite_original_path";

/// Preformatted internal context map key for zero-allocation original-path attribute updates.
pub(crate) const ATTR_ORIGINAL_PATH_MAP_KEY: &str =
    "External(\"hyperlane_plugin_rewrite_original_path\")";

/// Scheme string denoting HTTPS traffic.
pub(crate) const SCHEME_HTTPS: &str = "https";

/// Scheme string denoting plaintext HTTP traffic.
pub(crate) const SCHEME_HTTP: &str = "http";

/// Scheme-authority separator used when assembling absolute redirect URLs.
pub(crate) const SCHEME_SEPARATOR: &str = "://";

/// RFC 7239 `Forwarded` directive prefix selecting the incoming protocol scheme.
pub(crate) const FORWARDED_PROTO_PREFIX: &str = "proto=";

/// Default HTTP port stripped during canonical host comparison.
pub(crate) const DEFAULT_HTTP_PORT: &str = ":80";

/// Default HTTPS port stripped during canonical host comparison.
pub(crate) const DEFAULT_HTTPS_PORT: &str = ":443";

/// Error message prefix when compiling an invalid regex rewrite pattern.
pub(crate) const ERR_INVALID_REGEX: &str = "invalid rewrite regex pattern";

/// Error message when a rewrite target or host contains forbidden CR/LF characters.
pub(crate) const ERR_UNSAFE_LOCATION: &str = "unsafe CR/LF character in rewrite target";

/// FxHash 64-bit multiplicative mixing constant.
pub(crate) const FX_HASH_K: u64 = 0x517c_c1b7_2722_0a95;

/// SWAR broadcast low-bit mask across 8 bytes (`0x01` repeated).
pub(crate) const SWAR_LO_BITS: u64 = 0x0101_0101_0101_0101;

/// SWAR broadcast high-bit mask across 8 bytes (`0x80` repeated).
pub(crate) const SWAR_HI_BITS: u64 = 0x8080_8080_8080_8080;

/// SWAR broadcast ASCII space mask across 8 bytes (`0x20` repeated).
pub(crate) const SWAR_SPACES: u64 = 0x2020_2020_2020_2020;

/// RFC 9110 valid HTTP header field-value byte lookup table (matching `Response::set_header`).
pub(crate) const HTTP_HEADER_VALUE_VALID_LUT: [bool; 256] = {
    let mut table: [bool; 256] = [false; 256];
    let mut idx: usize = 0;
    while idx < 256 {
        let byte: u8 = idx as u8;
        let is_invalid: bool = (byte < b' ' && byte != b'\t') || byte == 0x7F;
        table[idx] = !is_invalid;
        idx += 1;
    }
    table
};

/// SWAR broadcast mask for ASCII LF (`0x0A`).
pub(crate) const SWAR_LF_MASK: u64 = 0x0a0a_0a0a_0a0a_0a0a;

/// SWAR broadcast mask for ASCII CR (`0x0D`).
pub(crate) const SWAR_CR_MASK: u64 = 0x0d0d_0d0d_0d0d_0d0d;

/// Stack buffer capacity in bytes for zero-allocation HTTP/1.1 redirect wire serialization.
pub(crate) const REDIRECT_WIRE_STACK_CAP: usize = 512;

/// Prebuilt HTTP/1.1 `301 Moved Permanently` redirect wire head up to the `Location` value.
pub(crate) const WIRE_301_PREFIX: &[u8] =
    b"HTTP/1.1 301 Moved Permanently\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";

/// Prebuilt HTTP/1.1 `302 Found` redirect wire head up to the `Location` value.
pub(crate) const WIRE_302_PREFIX: &[u8] =
    b"HTTP/1.1 302 Found\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";

/// Prebuilt HTTP/1.1 `307 Temporary Redirect` redirect wire head up to the `Location` value.
pub(crate) const WIRE_307_PREFIX: &[u8] =
    b"HTTP/1.1 307 Temporary Redirect\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";

/// Prebuilt HTTP/1.1 `308 Permanent Redirect` redirect wire head up to the `Location` value.
pub(crate) const WIRE_308_PREFIX: &[u8] =
    b"HTTP/1.1 308 Permanent Redirect\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";

/// HTTP/1.1 redirect wire terminator ending the `Location` header line and response head.
pub(crate) const WIRE_HEADER_END: &[u8] = b"\r\n\r\n";

/// Capture pattern accepting one or more ASCII decimal digits (`[0-9]+`).
pub(crate) const CAP_PAT_DIGITS_RANGE: &str = "([0-9]+)";

/// Capture pattern accepting one or more non-slash bytes (`[^/]+`).
pub(crate) const CAP_PAT_NON_SLASH: &str = "([^/]+)";

/// Capture pattern accepting one or more bytes (`.+`).
pub(crate) const CAP_PAT_NON_EMPTY: &str = "(.+)";

/// Capture pattern accepting zero or more bytes (`.*`).
pub(crate) const CAP_PAT_ANY: &str = "(.*)";

/// First capture group expansion placeholder `$1`.
pub(crate) const REPL_CAP_1: &str = "$1";

/// Braced first capture group expansion placeholder `${1}`.
pub(crate) const REPL_CAP_1_BRACED: &str = "${1}";

/// ASCII carriage return byte (`\r`).
pub(crate) const BYTE_CR: u8 = b'\r';

/// ASCII line feed byte (`\n`).
pub(crate) const BYTE_LF: u8 = b'\n';

/// ASCII NUL byte (`\0`).
pub(crate) const BYTE_NUL: u8 = b'\0';

/// ASCII forward slash byte (`/`).
pub(crate) const BYTE_SLASH: u8 = b'/';

/// ASCII question mark byte (`?`).
pub(crate) const BYTE_QUESTION: u8 = b'?';

/// ASCII dollar sign byte (`$`).
pub(crate) const BYTE_DOLLAR: u8 = b'$';

/// ASCII underscore byte (`_`).
pub(crate) const BYTE_UNDERSCORE: u8 = b'_';

/// ASCII ampersand byte (`&`).
pub(crate) const BYTE_AMP: u8 = b'&';

/// ASCII equals sign byte (`=`).
pub(crate) const BYTE_EQ: u8 = b'=';

/// 256-entry lookup table marking regular-expression metacharacters.
pub(crate) const REGEX_META_LUT: [bool; 256] = {
    let mut table: [bool; 256] = [false; 256];
    table[b'\\' as usize] = true;
    table[b'.' as usize] = true;
    table[b'+' as usize] = true;
    table[b'*' as usize] = true;
    table[b'?' as usize] = true;
    table[b'(' as usize] = true;
    table[b')' as usize] = true;
    table[b'|' as usize] = true;
    table[b'[' as usize] = true;
    table[b']' as usize] = true;
    table[b'{' as usize] = true;
    table[b'}' as usize] = true;
    table[b'^' as usize] = true;
    table[b'$' as usize] = true;
    table
};