This commit is contained in:
Mateusz Gruszczyński committed 2026-11-07 08:16:11 +01:00
1 parent c038ad44b3
commit cd3c28d390
33 files changed
+807 -221

No files matched your search

+23 -1
View File
@@ -454,13 +454,35 @@ impl ServerHeader {
headers.remove(::http::header::SERVER);
}
ServerMode::Custom => {
if let Ok(value) = self.value.parse() {
if let Some(value) = cached_header_value(&self.value) {
headers.insert(::http::header::SERVER, value);
}
}
}
}
}
/// Returns a reusable `HeaderValue` for a configured, already validated header
/// string. Values are parsed once per worker thread and then cloned (a reference
/// count bump) instead of being re-validated and re-allocated on every response.
pub(crate) fn cached_header_value(value: &str) -> Option<HeaderValue> {
const CAPACITY: usize = 16;
thread_local! {
static CACHE: std::cell::RefCell<Vec<(Box<str>, HeaderValue)>> =
const { std::cell::RefCell::new(Vec::new()) };
}
CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
if let Some((_, header)) = cache.iter().find(|(key, _)| key.as_ref() == value) {
return Some(header.clone());
}
let header = HeaderValue::from_maybe_shared(bytes::Bytes::copy_from_slice(value.as_bytes())).ok()?;
if cache.len() >= CAPACITY {
cache.remove(0);
}
cache.push((value.into(), header.clone()));
Some(header)
})
}
include!("settings/public_origin.rs");
#[cfg(test)]
include!("settings/tests.rs");