This commit is contained in:
Mateusz Gruszczyński committed 2026-10-05 09:30:21 +02:00
1 parent 1dea7f55c8
commit c819f0a335
151 files changed
+7610 -4264

No files matched your search

+25 -38
View File
@@ -68,7 +68,9 @@ fn content_type_kind(headers: &HeaderMap) -> Option<Kind> {
let value = headers.get("content-type")?.to_str().ok()?;
let mime = value.split(';').next()?.trim().to_ascii_lowercase();
match mime.as_str() {
"application/javascript" | "text/javascript" | "application/ecmascript"
"application/javascript"
| "text/javascript"
| "application/ecmascript"
| "text/ecmascript" => Some(Kind::Javascript),
"text/css" => Some(Kind::Css),
"text/html" | "application/xhtml+xml" => Some(Kind::Html),
@@ -166,26 +168,19 @@ fn decode<'a>(body: &'a [u8], encoding: Option<&str>, limit: usize) -> Result<Co
// Borrow them instead of creating a second full-size allocation.
Ok(Cow::Borrowed(body))
}
Some("gzip") => read_bounded(flate2::read::GzDecoder::new(body), limit, "gzip")
.map(Cow::Owned),
Some("gzip") => {
read_bounded(flate2::read::GzDecoder::new(body), limit, "gzip").map(Cow::Owned)
}
Some("deflate") => {
let zlib = flate2::read::ZlibDecoder::new(body);
match read_bounded(zlib, limit, "deflate") {
Ok(value) => Ok(Cow::Owned(value)),
Err(_) => read_bounded(
flate2::read::DeflateDecoder::new(body),
limit,
"deflate",
)
.map(Cow::Owned),
Err(_) => read_bounded(flate2::read::DeflateDecoder::new(body), limit, "deflate")
.map(Cow::Owned),
}
}
Some("br") => read_bounded(
brotli::Decompressor::new(body, 16 * 1024),
limit,
"brotli",
)
.map(Cow::Owned),
Some("br") => read_bounded(brotli::Decompressor::new(body, 16 * 1024), limit, "brotli")
.map(Cow::Owned),
Some("zstd") => {
let decoder = zstd::stream::read::Decoder::new(body).context("open zstd decoder")?;
read_bounded(decoder, limit, "zstd").map(Cow::Owned)
@@ -204,7 +199,11 @@ fn drop_vary_accept_encoding(headers: &mut HeaderMap) {
let Ok(value) = value.to_str() else {
return;
};
for token in value.split(',').map(str::trim).filter(|token| !token.is_empty()) {
for token in value
.split(',')
.map(str::trim)
.filter(|token| !token.is_empty())
{
if token == "*" {
// `Vary: *` has special semantics and is rejected by the shared
// cache. Do not rewrite it into a different policy.
@@ -213,7 +212,10 @@ fn drop_vary_accept_encoding(headers: &mut HeaderMap) {
if token.eq_ignore_ascii_case("accept-encoding") {
continue;
}
if !retained.iter().any(|existing| existing.eq_ignore_ascii_case(token)) {
if !retained
.iter()
.any(|existing| existing.eq_ignore_ascii_case(token))
{
retained.push(token.to_owned());
}
}
@@ -235,8 +237,7 @@ fn transformed_etag(body: &[u8]) -> HeaderValue {
let _ = write!(&mut value, "{byte:02x}");
}
value.push('"');
HeaderValue::from_str(&value)
.expect("sha256 validator is always a valid header")
HeaderValue::from_str(&value).expect("sha256 validator is always a valid header")
}
fn transform_memory_required_bytes(
@@ -249,8 +250,8 @@ fn transform_memory_required_bytes(
} else {
content_length.checked_add(max_body_bytes.checked_mul(2)?)?
};
let units = estimated.checked_add(TRANSFORM_MEMORY_UNIT_BYTES - 1)?
/ TRANSFORM_MEMORY_UNIT_BYTES;
let units =
estimated.checked_add(TRANSFORM_MEMORY_UNIT_BYTES - 1)? / TRANSFORM_MEMORY_UNIT_BYTES;
units.max(1).checked_mul(TRANSFORM_MEMORY_UNIT_BYTES)
}
@@ -279,12 +280,7 @@ fn try_reserve_memory_from(
if next > budget_bytes {
return None;
}
match counter.compare_exchange_weak(
current,
next,
Ordering::AcqRel,
Ordering::Acquire,
) {
match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
Ok(_) => {
return Some(TransformMemoryReservation {
counter,
@@ -300,11 +296,7 @@ fn try_reserve_transform_memory(
required_bytes: usize,
budget_bytes: usize,
) -> Option<TransformMemoryReservation<'static>> {
try_reserve_memory_from(
&TRANSFORM_MEMORY_IN_USE_BYTES,
required_bytes,
budget_bytes,
)
try_reserve_memory_from(&TRANSFORM_MEMORY_IN_USE_BYTES, required_bytes, budget_bytes)
}
struct TransformInflightReservation<'a> {
@@ -327,12 +319,7 @@ fn try_reserve_slot_from(
return None;
}
let next = current.checked_add(1)?;
match counter.compare_exchange_weak(
current,
next,
Ordering::AcqRel,
Ordering::Acquire,
) {
match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
Ok(_) => return Some(TransformInflightReservation { counter }),
Err(actual) => current = actual,
}