From 012b626ebf0b90259791e5d2a190d57c1e81b055 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sat, 17 Jul 2021 23:48:26 +0200 Subject: [PATCH] feat: cache control headers optional via `--cache-control-headers` --- src/config.rs | 10 ++++++++++ src/handler.rs | 5 ++++- src/server.rs | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 3b6de0a..0f18c08 100644 --- a/src/config.rs +++ b/src/config.rs @@ -131,4 +131,14 @@ pub struct Config { /// Headers included: "Strict-Transport-Security: max-age=63072000; includeSubDomains; preload" (2 years max-age), /// "X-Frame-Options: DENY", "X-XSS-Protection: 1; mode=block" and "Content-Security-Policy: frame-ancestors 'self'". pub security_headers: bool, + + #[structopt( + long, + short = "e", + parse(try_from_str), + default_value = "true", + env = "SERVER_CACHE_CONTROL_HEADERS" + )] + /// Enable cache control headers for incoming requests based on a set of file types. The file type list can be found on `src/control_headers.rs` file. + pub cache_control_headers: bool, } diff --git a/src/handler.rs b/src/handler.rs index 645b719..ee03ef7 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -12,6 +12,7 @@ pub struct RequestHandlerOpts { pub dir_listing: bool, pub cors: Option>, pub security_headers: bool, + pub cache_control_headers: bool, pub page404: Arc, pub page50x: Arc, } @@ -74,7 +75,9 @@ impl RequestHandler { } // Append `Cache-Control` headers for web assets - control_headers::append_headers(&uri_path, &mut resp); + if self.opts.cache_control_headers { + control_headers::append_headers(&uri_path, &mut resp); + } // Append security headers if self.opts.security_headers { diff --git a/src/server.rs b/src/server.rs index ddf7682..c3247df 100644 --- a/src/server.rs +++ b/src/server.rs @@ -102,6 +102,10 @@ impl Server { let dir_listing = opts.directory_listing; tracing::info!("directory listing: enabled={}", dir_listing); + // Cache control headers option + let cache_control_headers = opts.cache_control_headers; + tracing::info!("cache control headers: enabled={}", cache_control_headers); + // CORS option let cors = cors::new(opts.cors_allow_origins.trim().to_owned()); @@ -113,6 +117,7 @@ impl Server { dir_listing, cors, security_headers, + cache_control_headers, page404, page50x, }, -- libgit2 1.7.2