index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-07-17 21:48:26.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-07-17 21:48:26.0 +00:00:00
commit
012b626ebf0b90259791e5d2a190d57c1e81b055 [patch]
tree
e7336c9d3aa5215cacb34291bc59db6462f05009
parent
5aa587f9e9d5f699c5a5fcffa8d32d4974d02221
download
012b626ebf0b90259791e5d2a190d57c1e81b055.tar.gz

feat: cache control headers optional via `--cache-control-headers`



Diff

 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<Arc<cors::Configured>>,
    pub security_headers: bool,
    pub cache_control_headers: bool,
    pub page404: Arc<str>,
    pub page50x: Arc<str>,
}
@@ -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,
            },