Skip to main content

compmd_host/
headers.rs

1//! The headers the serving contract answers with ([hosting], decision 8): a served file's, which sandbox it, and a
2//! refusal's.
3//!
4//! [hosting]: https://github.com/comp-markdown/compmd/blob/main/docs/hosting.md
5
6use crate::content_type::content_type;
7
8/// The headers every served file has, beside its type.
9const SERVED: [(&str, &str); 5] = [
10    // Sandboxed even when a frame document is opened outside a frame.
11    ("content-security-policy", "sandbox allow-scripts"),
12    // A frame's module scripts come from an opaque origin, so they are cross-origin requests.
13    ("access-control-allow-origin", "*"),
14    // A page that checks how its frames are served (a host's check) reads these headers.
15    ("access-control-expose-headers", "*"),
16    ("x-content-type-options", "nosniff"),
17    // A frame's path names its build, a pack's its version, so what it serves never changes.
18    ("cache-control", "max-age=31536000, immutable"),
19];
20
21/// The headers of the file at `path`, served: its type, by its extension, and those that sandbox it.
22///
23/// ```
24/// let headers = compmd_host::served_headers("strict.html");
25/// assert!(headers.contains(&("content-type", "text/html; charset=utf-8")));
26/// assert!(headers.contains(&("content-security-policy", "sandbox allow-scripts")));
27/// ```
28#[must_use]
29pub fn served_headers(path: &str) -> Vec<(&'static str, &'static str)> {
30    let mut headers = vec![("content-type", content_type(path))];
31    headers.extend(SERVED);
32    headers
33}
34
35/// The headers of a refusal: plain text, readable from any origin, so a page reads why.
36#[must_use]
37pub fn refusal_headers() -> [(&'static str, &'static str); 2] {
38    [
39        ("content-type", "text/plain; charset=utf-8"),
40        ("access-control-allow-origin", "*"),
41    ]
42}