Skip to main content

hyperlane_plugin_rewrite/
const.rs

1/// Context attribute key preserving the pre-rewrite request path after an internal rewrite.
2pub const ATTR_ORIGINAL_PATH: &str = "hyperlane_plugin_rewrite_original_path";
3
4/// Preformatted internal context map key for zero-allocation original-path attribute updates.
5pub(crate) const ATTR_ORIGINAL_PATH_MAP_KEY: &str =
6    "External(\"hyperlane_plugin_rewrite_original_path\")";
7
8/// Scheme string denoting HTTPS traffic.
9pub(crate) const SCHEME_HTTPS: &str = "https";
10
11/// Scheme string denoting plaintext HTTP traffic.
12pub(crate) const SCHEME_HTTP: &str = "http";
13
14/// Scheme-authority separator used when assembling absolute redirect URLs.
15pub(crate) const SCHEME_SEPARATOR: &str = "://";
16
17/// RFC 7239 `Forwarded` directive prefix selecting the incoming protocol scheme.
18pub(crate) const FORWARDED_PROTO_PREFIX: &str = "proto=";
19
20/// Default HTTP port stripped during canonical host comparison.
21pub(crate) const DEFAULT_HTTP_PORT: &str = ":80";
22
23/// Default HTTPS port stripped during canonical host comparison.
24pub(crate) const DEFAULT_HTTPS_PORT: &str = ":443";
25
26/// Error message prefix when compiling an invalid regex rewrite pattern.
27pub(crate) const ERR_INVALID_REGEX: &str = "invalid rewrite regex pattern";
28
29/// Error message when a rewrite target or host contains forbidden CR/LF characters.
30pub(crate) const ERR_UNSAFE_LOCATION: &str = "unsafe CR/LF character in rewrite target";
31
32/// FxHash 64-bit multiplicative mixing constant.
33pub(crate) const FX_HASH_K: u64 = 0x517c_c1b7_2722_0a95;
34
35/// SWAR broadcast low-bit mask across 8 bytes (`0x01` repeated).
36pub(crate) const SWAR_LO_BITS: u64 = 0x0101_0101_0101_0101;
37
38/// SWAR broadcast high-bit mask across 8 bytes (`0x80` repeated).
39pub(crate) const SWAR_HI_BITS: u64 = 0x8080_8080_8080_8080;
40
41/// SWAR broadcast ASCII space mask across 8 bytes (`0x20` repeated).
42pub(crate) const SWAR_SPACES: u64 = 0x2020_2020_2020_2020;
43
44/// RFC 9110 valid HTTP header field-value byte lookup table (matching `Response::set_header`).
45pub(crate) const HTTP_HEADER_VALUE_VALID_LUT: [bool; 256] = {
46    let mut table: [bool; 256] = [false; 256];
47    let mut idx: usize = 0;
48    while idx < 256 {
49        let byte: u8 = idx as u8;
50        let is_invalid: bool = (byte < b' ' && byte != b'\t') || byte == 0x7F;
51        table[idx] = !is_invalid;
52        idx += 1;
53    }
54    table
55};
56
57/// SWAR broadcast mask for ASCII LF (`0x0A`).
58pub(crate) const SWAR_LF_MASK: u64 = 0x0a0a_0a0a_0a0a_0a0a;
59
60/// SWAR broadcast mask for ASCII CR (`0x0D`).
61pub(crate) const SWAR_CR_MASK: u64 = 0x0d0d_0d0d_0d0d_0d0d;
62
63/// Stack buffer capacity in bytes for zero-allocation HTTP/1.1 redirect wire serialization.
64pub(crate) const REDIRECT_WIRE_STACK_CAP: usize = 512;
65
66/// Prebuilt HTTP/1.1 `301 Moved Permanently` redirect wire head up to the `Location` value.
67pub(crate) const WIRE_301_PREFIX: &[u8] =
68    b"HTTP/1.1 301 Moved Permanently\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";
69
70/// Prebuilt HTTP/1.1 `302 Found` redirect wire head up to the `Location` value.
71pub(crate) const WIRE_302_PREFIX: &[u8] =
72    b"HTTP/1.1 302 Found\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";
73
74/// Prebuilt HTTP/1.1 `307 Temporary Redirect` redirect wire head up to the `Location` value.
75pub(crate) const WIRE_307_PREFIX: &[u8] =
76    b"HTTP/1.1 307 Temporary Redirect\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";
77
78/// Prebuilt HTTP/1.1 `308 Permanent Redirect` redirect wire head up to the `Location` value.
79pub(crate) const WIRE_308_PREFIX: &[u8] =
80    b"HTTP/1.1 308 Permanent Redirect\r\nconnection:keep-alive\r\ncontent-length:0\r\nlocation:";
81
82/// HTTP/1.1 redirect wire terminator ending the `Location` header line and response head.
83pub(crate) const WIRE_HEADER_END: &[u8] = b"\r\n\r\n";
84
85/// Capture pattern accepting one or more ASCII decimal digits (`[0-9]+`).
86pub(crate) const CAP_PAT_DIGITS_RANGE: &str = "([0-9]+)";
87
88/// Capture pattern accepting one or more non-slash bytes (`[^/]+`).
89pub(crate) const CAP_PAT_NON_SLASH: &str = "([^/]+)";
90
91/// Capture pattern accepting one or more bytes (`.+`).
92pub(crate) const CAP_PAT_NON_EMPTY: &str = "(.+)";
93
94/// Capture pattern accepting zero or more bytes (`.*`).
95pub(crate) const CAP_PAT_ANY: &str = "(.*)";
96
97/// First capture group expansion placeholder `$1`.
98pub(crate) const REPL_CAP_1: &str = "$1";
99
100/// Braced first capture group expansion placeholder `${1}`.
101pub(crate) const REPL_CAP_1_BRACED: &str = "${1}";
102
103/// ASCII carriage return byte (`\r`).
104pub(crate) const BYTE_CR: u8 = b'\r';
105
106/// ASCII line feed byte (`\n`).
107pub(crate) const BYTE_LF: u8 = b'\n';
108
109/// ASCII NUL byte (`\0`).
110pub(crate) const BYTE_NUL: u8 = b'\0';
111
112/// ASCII forward slash byte (`/`).
113pub(crate) const BYTE_SLASH: u8 = b'/';
114
115/// ASCII question mark byte (`?`).
116pub(crate) const BYTE_QUESTION: u8 = b'?';
117
118/// ASCII dollar sign byte (`$`).
119pub(crate) const BYTE_DOLLAR: u8 = b'$';
120
121/// ASCII underscore byte (`_`).
122pub(crate) const BYTE_UNDERSCORE: u8 = b'_';
123
124/// ASCII ampersand byte (`&`).
125pub(crate) const BYTE_AMP: u8 = b'&';
126
127/// ASCII equals sign byte (`=`).
128pub(crate) const BYTE_EQ: u8 = b'=';
129
130/// 256-entry lookup table marking regular-expression metacharacters.
131pub(crate) const REGEX_META_LUT: [bool; 256] = {
132    let mut table: [bool; 256] = [false; 256];
133    table[b'\\' as usize] = true;
134    table[b'.' as usize] = true;
135    table[b'+' as usize] = true;
136    table[b'*' as usize] = true;
137    table[b'?' as usize] = true;
138    table[b'(' as usize] = true;
139    table[b')' as usize] = true;
140    table[b'|' as usize] = true;
141    table[b'[' as usize] = true;
142    table[b']' as usize] = true;
143    table[b'{' as usize] = true;
144    table[b'}' as usize] = true;
145    table[b'^' as usize] = true;
146    table[b'$' as usize] = true;
147    table
148};