use static default headers

This commit is contained in:
Edward Shen 2021-07-10 19:04:27 -04:00
parent 93249397f1
commit f64d03493e
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -32,6 +32,25 @@ pub static HTTP_CLIENT: Lazy<CachingClient> = Lazy::new(|| CachingClient {
locks: RwLock::new(HashMap::new()), locks: RwLock::new(HashMap::new()),
}); });
static DEFAULT_HEADERS: Lazy<HeaderMap> = 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 { pub struct CachingClient {
inner: Client, inner: Client,
locks: RwLock<HashMap<String, Receiver<FetchResult>>>, locks: RwLock<HashMap<String, Receiver<FetchResult>>>,
@ -88,28 +107,12 @@ impl CachingClient {
if resp.status() != StatusCode::OK || !is_image { if resp.status() != StatusCode::OK || !is_image {
warn!("Got non-OK or non-image response code from upstream, proxying and not caching result."); 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 { if let Some(content_type) = content_type {
headers.insert(CONTENT_TYPE, content_type.clone()); 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( FetchResult::Data(
resp.status(), resp.status(),
headers, headers,
@ -140,7 +143,7 @@ impl CachingClient {
Ok(()) => { Ok(()) => {
debug!("Done putting into cache"); debug!("Done putting into cache");
let mut headers = HeaderMap::new(); let mut headers = DEFAULT_HEADERS.clone();
if let Some(content_type) = content_type { if let Some(content_type) = content_type {
headers.insert(CONTENT_TYPE, content_type); headers.insert(CONTENT_TYPE, content_type);
} }
@ -153,26 +156,6 @@ impl CachingClient {
headers.insert(LAST_MODIFIED, last_modified); 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) FetchResult::Data(StatusCode::OK, headers, body)
} }
Err(e) => { Err(e) => {