Skip to main content

hyperlane_plugin_static_files/
struct.rs

1use super::*;
2
3/// Configuration for [`StaticFilesRuntime`] and [`StaticFilesPlugin`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct StaticFilesConfig {
6    /// Filesystem root directory from which assets are served.
7    pub(crate) root_dir: PathBuf,
8    /// URL mount prefix (for example `"/static"` or `"/"`).
9    pub(crate) mount_prefix: String,
10    /// Default directory index filename (default `"index.html"`).
11    pub(crate) index_file: String,
12    /// Optional SPA fallback file relative to `root_dir` (for example `"index.html"`).
13    #[get(type(clone))]
14    pub(crate) spa_fallback: Option<String>,
15    /// `Cache-Control` header value applied to served files.
16    pub(crate) cache_control: String,
17    /// Enables serving precompressed `.br` and `.gz` sidecar files when accepted by the client.
18    #[get(type(copy))]
19    pub(crate) serve_precompressed: bool,
20    /// Permits serving dotfiles (`.well-known`, etc.) when `true` (default `false`).
21    #[get(type(copy))]
22    pub(crate) allow_dotfiles: bool,
23    /// Total memory budget in bytes for the 64-way sharded hot-file cache (`0` disables caching).
24    #[get(type(copy))]
25    pub(crate) max_cache_bytes: usize,
26    /// Maximum single file size eligible for in-memory hot caching.
27    #[get(type(copy))]
28    pub(crate) max_file_cache_bytes: usize,
29}
30
31/// Zero-allocation multiplicative 64-bit hasher for hot-file cache lookup.
32#[derive(Clone, Copy, Default)]
33pub(crate) struct FxHasher {
34    /// Running 64-bit hash accumulator.
35    pub(crate) state: u64,
36}
37
38/// Cache-line-aligned hot-file cache shard avoiding cross-thread false sharing.
39#[repr(align(64))]
40pub(crate) struct StaticCacheShard {
41    /// Per-shard read-write lock guarding cached route entries.
42    pub(crate) map: RwLock<StaticCacheShardMap>,
43}
44
45/// Immutable cached static file payload stored inside [`StaticFilesRuntime`].
46#[derive(Debug)]
47pub(crate) struct CachedFile {
48    /// Shared zero-copy file contents bytes.
49    pub(crate) bytes: Arc<[u8]>,
50    /// Precomputed strong `ETag`.
51    pub(crate) etag: String,
52    /// Resolved MIME `Content-Type`.
53    pub(crate) content_type: &'static str,
54    /// Optional precompressed `Content-Encoding` (`"br"` or `"gzip"`).
55    pub(crate) content_encoding: Option<&'static str>,
56    /// Lazily built HTTP/1.1 `200 OK` full wire response (headers + body).
57    pub(crate) wire_ok: OnceLock<Arc<[u8]>>,
58    /// Lazily built HTTP/1.1 `200 OK` `HEAD` wire response (headers only).
59    pub(crate) wire_head: OnceLock<Arc<[u8]>>,
60    /// Lazily built HTTP/1.1 `304 Not Modified` wire response.
61    pub(crate) wire_304: OnceLock<Arc<[u8]>>,
62}
63
64/// Cached asset entry holding the uncompressed representation plus optional `.br` and `.gz` sidecars.
65#[derive(Clone, Debug)]
66pub(crate) struct CachedRouteEntry {
67    /// Uncompressed file representation.
68    pub(crate) raw: Arc<CachedFile>,
69    /// Optional Brotli precompressed sidecar representation.
70    pub(crate) br: Option<Arc<CachedFile>>,
71    /// Optional Gzip precompressed sidecar representation.
72    pub(crate) gz: Option<Arc<CachedFile>>,
73}
74
75/// Shared interior state of [`StaticFilesRuntime`].
76pub(crate) struct StaticFilesRuntimeInner {
77    /// Original static file configuration.
78    pub(crate) config: StaticFilesConfig,
79    /// Canonicalized root directory path for symlink-escape verification.
80    pub(crate) canonical_root: PathBuf,
81    /// Normalized mount prefix (without trailing slash unless `"/"`).
82    pub(crate) normalized_prefix: String,
83    /// Current cached byte usage across all 64 shards.
84    pub(crate) cached_bytes: AtomicUsize,
85    /// 64-way sharded hot-file memory cache.
86    pub(crate) cache_shards: Box<[StaticCacheShard; STATIC_CACHE_SHARDS]>,
87}
88
89/// Thread-safe static asset engine with 64-way sharded hot-file caching and security checks.
90#[derive(Clone)]
91pub struct StaticFilesRuntime {
92    /// Shared runtime state.
93    pub(crate) inner: Arc<StaticFilesRuntimeInner>,
94}
95
96/// Process-wide [`StaticFilesPlugin`] singleton storing [`StaticFilesRuntime`].
97#[derive(Clone)]
98pub struct StaticFilesPlugin {
99    /// Compiled static-file serving engine.
100    pub(crate) runtime: StaticFilesRuntime,
101}
102
103/// Opt-in [`ServerHook`] serving static assets on `GET` / `HEAD` requests under the configured mount prefix.
104#[derive(Clone, Copy, Debug, Default)]
105#[hyperlane::request_middleware(-55)]
106pub struct StaticFilesHook;