From f64d03493e846372fb0bd141bb50a8ac61d38d47 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sat, 10 Jul 2021 19:04:27 -0400 Subject: [PATCH] use static default headers --- src/client.rs | 59 ++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/src/client.rs b/src/client.rs index 9e35d7d..db3e3b9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -32,6 +32,25 @@ pub static HTTP_CLIENT: Lazy = Lazy::new(|| CachingClient { locks: RwLock::new(HashMap::new()), }); +static DEFAULT_HEADERS: Lazy = Lazy::new(|| { + let mut headers = HeaderMap::with_capacity(8); + headers.insert(X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff")); + headers.insert( + ACCESS_CONTROL_ALLOW_ORIGIN, + HeaderValue::from_static("https://mangadex.org"), + ); + headers.insert(ACCESS_CONTROL_EXPOSE_HEADERS, HeaderValue::from_static("*")); + headers.insert( + CACHE_CONTROL, + HeaderValue::from_static("public, max-age=1209600"), + ); + headers.insert( + HeaderName::from_static("timing-allow-origin"), + HeaderValue::from_static("https://mangadex.org"), + ); + headers +}); + pub struct CachingClient { inner: Client, locks: RwLock>>, @@ -88,28 +107,12 @@ impl CachingClient { if resp.status() != StatusCode::OK || !is_image { warn!("Got non-OK or non-image response code from upstream, proxying and not caching result."); - let mut headers = HeaderMap::new(); + let mut headers = DEFAULT_HEADERS.clone(); if let Some(content_type) = content_type { headers.insert(CONTENT_TYPE, content_type.clone()); } - headers.insert(X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff")); - headers.insert( - ACCESS_CONTROL_ALLOW_ORIGIN, - HeaderValue::from_static("https://mangadex.org"), - ); - headers - .insert(ACCESS_CONTROL_EXPOSE_HEADERS, HeaderValue::from_static("*")); - headers.insert( - CACHE_CONTROL, - HeaderValue::from_static("public, max-age=1209600"), - ); - headers.insert( - HeaderName::from_static("timing-allow-origin"), - HeaderValue::from_static("https://mangadex.org"), - ); - FetchResult::Data( resp.status(), headers, @@ -140,7 +143,7 @@ impl CachingClient { Ok(()) => { debug!("Done putting into cache"); - let mut headers = HeaderMap::new(); + let mut headers = DEFAULT_HEADERS.clone(); if let Some(content_type) = content_type { headers.insert(CONTENT_TYPE, content_type); } @@ -153,26 +156,6 @@ impl CachingClient { headers.insert(LAST_MODIFIED, last_modified); } - headers.insert( - X_CONTENT_TYPE_OPTIONS, - HeaderValue::from_static("nosniff"), - ); - headers.insert( - ACCESS_CONTROL_ALLOW_ORIGIN, - HeaderValue::from_static("https://mangadex.org"), - ); - headers.insert( - ACCESS_CONTROL_EXPOSE_HEADERS, - HeaderValue::from_static("*"), - ); - headers.insert( - CACHE_CONTROL, - HeaderValue::from_static("public, max-age=1209600"), - ); - headers.insert( - HeaderName::from_static("timing-allow-origin"), - HeaderValue::from_static("https://mangadex.org"), - ); FetchResult::Data(StatusCode::OK, headers, body) } Err(e) => {