From 941572ce3b0e562996d3b25738f1c42a34b5acfa Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Tue, 13 Jul 2021 09:40:41 +0200 Subject: [PATCH] refactor: reduce allocation on control headers checking --- 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) { +pub fn append_headers(uri: &str, resp: &mut Response) { // 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 { -- libgit2 1.7.2