index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-07-13 7:40:41.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-07-13 7:40:41.0 +00:00:00
commit
941572ce3b0e562996d3b25738f1c42a34b5acfa [patch]
tree
21e1d12df9fae243eea5682b74ae56789479109f
parent
157ade1c85d746584baf9ca7c0ac184b6a8600dd
download
941572ce3b0e562996d3b25738f1c42a34b5acfa.tar.gz

refactor: reduce allocation on control headers checking



Diff

 src/control_headers.rs | 6 +++---
 src/handler.rs         | 3 +--
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/control_headers.rs b/src/control_headers.rs
index 19122a9..558a6bf 100644
--- a/src/control_headers.rs
+++ b/src/control_headers.rs
@@ -13,18 +13,18 @@ const CACHE_EXT_ONE_YEAR: [&str; 30] = [
];

/// It appends a `Cache-Control` header to a response if that one is part of a set of file types.
pub fn append_headers(ext: &str, resp: &mut Response<Body>) {
pub fn append_headers(uri: &str, resp: &mut Response<Body>) {
    // Default max-age value in seconds (one day)
    let mut max_age = 60 * 60 * 24_u64;

    if CACHE_EXT_ONE_HOUR
        .iter()
        .any(|x| ext.ends_with(&[".", *x].concat()))
        .any(|x| uri.ends_with(&[".", *x].concat()))
    {
        max_age = 60 * 60;
    } else if CACHE_EXT_ONE_YEAR
        .iter()
        .any(|x| ext.ends_with(&[".", *x].concat()))
        .any(|x| uri.ends_with(&[".", *x].concat()))
    {
        max_age = 60 * 60 * 24 * 365;
    }
diff --git a/src/handler.rs b/src/handler.rs
index c0282bb..645b719 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -74,8 +74,7 @@ impl RequestHandler {
                    }

                    // Append `Cache-Control` headers for web assets
                    let ext = uri_path.to_lowercase();
                    control_headers::append_headers(&ext, &mut resp);
                    control_headers::append_headers(&uri_path, &mut resp);

                    // Append security headers
                    if self.opts.security_headers {