hyperlane-plugin-static-files 23.3.0

A high-performance static file server plugin for the Hyperlane framework with canonicalized path-traversal protection, built-in FileExtension MIME detection, 64-way sharded hot-file cache, precompressed .br/.gz sidecars, ETag/304 conditional validation, RFC 9110 Range 206/416 support, and SPA fallback.
Documentation
/// Default URL mount prefix served by [`crate::StaticFilesHook`].
pub const DEFAULT_STATIC_MOUNT_PREFIX: &str = "/";

/// Default directory index filename.
pub const DEFAULT_STATIC_INDEX_FILE: &str = "index.html";

/// Default `Cache-Control` header applied to served static files.
pub const DEFAULT_STATIC_CACHE_CONTROL: &str = "public, max-age=3600";

/// Default maximum total memory budget for the sharded hot-file cache (32 MiB).
pub const DEFAULT_STATIC_MAX_CACHE_BYTES: usize = 32 * 1024 * 1024;

/// Default maximum single file size eligible for in-memory hot caching (1 MiB).
pub const DEFAULT_STATIC_MAX_FILE_CACHE_BYTES: usize = 1024 * 1024;

/// Number of independent hot-file cache shards (`64`, power of two).
pub(crate) const STATIC_CACHE_SHARDS: usize = 64;

/// `Accept-Ranges` value advertising byte-range support.
pub(crate) const ACCEPT_RANGES_BYTES: &str = "bytes";

/// `Range` header unit prefix.
pub(crate) const RANGE_BYTES_PREFIX: &str = "bytes=";

/// `Content-Encoding` token for Brotli precompressed sidecars.
pub(crate) const ENCODING_BR: &str = "br";

/// `Content-Encoding` token for Gzip precompressed sidecars.
pub(crate) const ENCODING_GZIP: &str = "gzip";

/// File extension suffix for Brotli sidecar assets.
pub(crate) const SIDECAR_BR_EXT: &str = ".br";

/// File extension suffix for Gzip sidecar assets.
pub(crate) const SIDECAR_GZ_EXT: &str = ".gz";

/// `Vary` token emitted when serving negotiated precompressed sidecars.
pub(crate) const VARY_ACCEPT_ENCODING: &str = "Accept-Encoding";

/// Lowercase hexadecimal alphabet used for stack-allocated strong ETag formatting.
pub(crate) const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";

/// Bitmask flag indicating client accepts Brotli content encoding.
pub(crate) const ENCODING_MASK_BR: u8 = 0b01;

/// Bitmask flag indicating client accepts Gzip content encoding.
pub(crate) const ENCODING_MASK_GZIP: u8 = 0b10;

/// Prefix for RFC 9110 `Content-Range` satisfied partial responses.
pub(crate) const CONTENT_RANGE_BYTES_PREFIX: &str = "bytes ";

/// Prefix for RFC 9110 `Content-Range` unsatisfiable (`416`) responses.
pub(crate) const CONTENT_RANGE_UNSAT_PREFIX: &str = "bytes */";

/// Stack buffer capacity in bytes for formatting `Content-Range` headers without heap allocation.
pub(crate) const CONTENT_RANGE_STACK_CAP: usize = 64;

/// Stack buffer capacity in bytes for lowercasing file extensions during MIME lookup.
pub(crate) const MIME_EXT_STACK_CAP: usize = 24;

/// Stack buffer capacity in bytes for formatting file ETags (`"<hex>-<hex>"`).
pub(crate) const ETAG_STACK_CAP: usize = 40;

/// Weak ETag prefix stripped during weak comparison.
pub(crate) const WEAK_ETAG_PREFIX: &str = "W/";

/// Wildcard `If-None-Match` token.
pub(crate) const ETAG_WILDCARD: &str = "*";

/// Multiplicative 64-bit mixing constant for zero-allocation [`FxHasher`].
pub(crate) const FX_HASH_K: u64 = 0x517c_c1b7_2722_0a95;

/// 256-entry ASCII hex nibble decode table (`0..=15` valid, `0xFF` invalid).
pub(crate) const HEX_DECODE_LUT: [u8; 256] = {
    let mut table: [u8; 256] = [0xFFu8; 256];
    let mut c: usize = b'0' as usize;
    while c <= b'9' as usize {
        table[c] = (c - b'0' as usize) as u8;
        c += 1;
    }
    let mut c: usize = b'a' as usize;
    while c <= b'f' as usize {
        table[c] = (c - b'a' as usize + 10) as u8;
        c += 1;
    }
    let mut c: usize = b'A' as usize;
    while c <= b'F' as usize {
        table[c] = (c - b'A' as usize + 10) as u8;
        c += 1;
    }
    table
};

/// Two-digit ASCII decimal lookup pairs for `00..=99`.
pub(crate) const DEC_DIGITS_LUT: &[u8; 200] = b"00010203040506070809\
10111213141516171819\
20212223242526272829\
30313233343536373839\
40414243444546474849\
50515253545556575859\
60616263646566676869\
70717273747576777879\
80818283848586878889\
90919293949596979899";

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

/// ASCII backslash byte.
pub(crate) const BYTE_BACKSLASH: u8 = b'\\';

/// ASCII percent byte.
pub(crate) const BYTE_PERCENT: u8 = b'%';

/// ASCII dot byte.
pub(crate) const BYTE_DOT: u8 = b'.';

/// ASCII double quote byte.
pub(crate) const BYTE_QUOTE: u8 = b'"';

/// ASCII hyphen byte.
pub(crate) const BYTE_DASH: u8 = b'-';

/// ASCII comma byte.
pub(crate) const BYTE_COMMA: u8 = b',';

/// ASCII semicolon byte.
pub(crate) const BYTE_SEMICOLON: u8 = b';';

/// ASCII zero digit byte.
pub(crate) const BYTE_ZERO: u8 = b'0';

/// Lowercase fast-path extension byte array for `js`.
pub(crate) const EXT_JS: [u8; 2] = *b"js";

/// Lowercase fast-path extension byte array for `css`.
pub(crate) const EXT_CSS: [u8; 3] = *b"css";

/// Lowercase fast-path extension byte array for `png`.
pub(crate) const EXT_PNG: [u8; 3] = *b"png";

/// Lowercase fast-path extension byte array for `jpg`.
pub(crate) const EXT_JPG: [u8; 3] = *b"jpg";

/// Lowercase fast-path extension byte array for `svg`.
pub(crate) const EXT_SVG: [u8; 3] = *b"svg";

/// Lowercase fast-path extension byte array for `gif`.
pub(crate) const EXT_GIF: [u8; 3] = *b"gif";

/// Lowercase fast-path extension byte array for `ico`.
pub(crate) const EXT_ICO: [u8; 3] = *b"ico";

/// Lowercase fast-path extension byte array for `htm`.
pub(crate) const EXT_HTM: [u8; 3] = *b"htm";

/// Lowercase fast-path extension byte array for `txt`.
pub(crate) const EXT_TXT: [u8; 3] = *b"txt";

/// Lowercase fast-path extension byte array for `xml`.
pub(crate) const EXT_XML: [u8; 3] = *b"xml";

/// Lowercase fast-path extension byte array for `pdf`.
pub(crate) const EXT_PDF: [u8; 3] = *b"pdf";

/// Lowercase fast-path extension byte array for `ttf`.
pub(crate) const EXT_TTF: [u8; 3] = *b"ttf";

/// Lowercase fast-path extension byte array for `otf`.
pub(crate) const EXT_OTF: [u8; 3] = *b"otf";

/// Lowercase fast-path extension byte array for `mp4`.
pub(crate) const EXT_MP4: [u8; 3] = *b"mp4";

/// Lowercase fast-path extension byte array for `mp3`.
pub(crate) const EXT_MP3: [u8; 3] = *b"mp3";

/// Lowercase fast-path extension byte array for `html`.
pub(crate) const EXT_HTML: [u8; 4] = *b"html";

/// Lowercase fast-path extension byte array for `json`.
pub(crate) const EXT_JSON: [u8; 4] = *b"json";

/// Lowercase fast-path extension byte array for `wasm`.
pub(crate) const EXT_WASM: [u8; 4] = *b"wasm";

/// Lowercase fast-path extension byte array for `webp`.
pub(crate) const EXT_WEBP: [u8; 4] = *b"webp";

/// Lowercase fast-path extension byte array for `jpeg`.
pub(crate) const EXT_JPEG: [u8; 4] = *b"jpeg";

/// Lowercase fast-path extension byte array for `woff`.
pub(crate) const EXT_WOFF: [u8; 4] = *b"woff";

/// Lowercase fast-path extension byte array for `webm`.
pub(crate) const EXT_WEBM: [u8; 4] = *b"webm";

/// Lowercase fast-path extension byte array for `woff2`.
pub(crate) const EXT_WOFF2: [u8; 5] = *b"woff2";

/// Quality-factor parameter prefix lowercase (`"q="`).
pub(crate) const QUALITY_PREFIX_LOWER: &str = "q=";

/// Quality-factor parameter prefix uppercase (`"Q="`).
pub(crate) const QUALITY_PREFIX_UPPER: &str = "Q=";