Skip to main content

next_rust_server/
embed.rs

1//! Files compiled into the production binary.
2//!
3//! Release builds embed `next-rust.toml`, `public/`, `assets/` and `client/`
4//! (see `next-rust-build`), so the server binary is the only file a
5//! deployment needs. Development builds leave this empty and read from disk.
6
7/// One embedded file. `path` is relative to its directory and uses `/`.
8#[derive(Debug, Clone, Copy)]
9pub struct EmbeddedFile {
10    pub path: &'static str,
11    pub bytes: &'static [u8],
12    /// Content hash (`next_rust_assets::content_hash`), used for ETags and
13    /// to validate hashed asset URLs.
14    pub hash: &'static str,
15}
16
17/// Everything a release binary carries with it.
18#[derive(Debug, Clone, Copy, Default)]
19pub struct Embedded {
20    /// Contents of the configuration file and whether it is JSON.
21    pub config: Option<(&'static str, bool)>,
22    /// Build time in seconds since the Unix epoch (`Last-Modified`).
23    pub built_at: u64,
24    /// Sorted by path.
25    pub public: &'static [EmbeddedFile],
26    /// Sorted by path.
27    pub assets: &'static [EmbeddedFile],
28    /// Sorted by path.
29    pub client: &'static [EmbeddedFile],
30}
31
32impl Embedded {
33    pub fn is_empty(&self) -> bool {
34        self.config.is_none() && self.public.is_empty() && self.assets.is_empty() && self.client.is_empty()
35    }
36}
37
38/// Find a file by URL path using the same rules as disk lookups: segments are
39/// percent-decoded and `.`/`..`, hidden files and separators are rejected.
40pub fn find(files: &'static [EmbeddedFile], url_path: &str) -> Option<&'static EmbeddedFile> {
41    let mut rel = String::new();
42    for raw in url_path.split('/').filter(|s| !s.is_empty()) {
43        let seg = next_rust_router::decode_segment(raw)?;
44        if seg.starts_with('.') || seg.contains(['\\', '\0', '/', ':']) {
45            return None;
46        }
47        if !rel.is_empty() {
48            rel.push('/');
49        }
50        rel.push_str(&seg);
51    }
52    if rel.is_empty() {
53        return None;
54    }
55    files.binary_search_by(|f| f.path.cmp(rel.as_str())).ok().map(|i| &files[i])
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    const FILES: &[EmbeddedFile] = &[
63        EmbeddedFile { path: "favicon.svg", bytes: b"<svg/>", hash: "a" },
64        EmbeddedFile { path: "images/logo space.png", bytes: b"png", hash: "b" },
65        EmbeddedFile { path: "robots.txt", bytes: b"ok", hash: "c" },
66    ];
67
68    #[test]
69    fn lookups_follow_disk_rules() {
70        assert_eq!(find(FILES, "/favicon.svg").map(|f| f.hash), Some("a"));
71        assert_eq!(find(FILES, "/images/logo%20space.png").map(|f| f.hash), Some("b"));
72        assert_eq!(find(FILES, "//robots.txt").map(|f| f.hash), Some("c"));
73        for bad in ["/", "/images", "/../robots.txt", "/.env", "/images%2Flogo%20space.png", "/nope.txt"] {
74            assert!(find(FILES, bad).is_none(), "{bad}");
75        }
76    }
77}