tiles_proxy/
http_headers.rs

1//! Additional HTTP headers for browser cache management.
2
3use rocket::fairing::{Fairing, Info, Kind};
4use rocket::http::Header;
5use rocket::{Request, Response};
6
7/// The `Cache-Control` HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).
8///
9/// See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control>
10pub struct CacheControl;
11
12#[rocket::async_trait]
13impl Fairing for CacheControl {
14    fn info(&self) -> Info {
15        Info {
16            name: "Add Cache-Control header to responses",
17            kind: Kind::Response,
18        }
19    }
20
21    async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
22        // 2592000 = 30 * 24 * 60 * 60
23        response.set_header(Header::new(
24            "Cache-Control",
25            "public, max-age=2592000, immutable",
26        ));
27    }
28}
29
30/// Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.
31///
32/// See <https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS>
33pub struct Cors;
34
35#[rocket::async_trait]
36impl Fairing for Cors {
37    fn info(&self) -> Info {
38        Info {
39            name: "Add CORS headers to responses",
40            kind: Kind::Response,
41        }
42    }
43
44    async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
45        response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
46        response.set_header(Header::new(
47            "Access-Control-Allow-Methods",
48            "POST, GET, PATCH, OPTIONS",
49        ));
50        response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
51        response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
52    }
53}