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

+32 -24
View File
@@ -268,13 +268,12 @@ impl Settings {
if !self.enabled {
return;
}
if let Some(value) = &self.via_header {
headers.append(
http::header::VIA,
value
.parse::<HeaderValue>()
.expect("validated cache Via header"),
);
if let Some(value) = self
.via_header
.as_deref()
.and_then(crate::settings::cached_header_value)
{
headers.append(http::header::VIA, value);
}
if self.expose_only_hits && outcome != "HIT" {
return;
@@ -300,7 +299,7 @@ impl Settings {
headers,
"x-cache-hits",
"x-upstream-cache-hits",
HeaderValue::from_str(&hits.to_string()).expect("integer header"),
HeaderValue::from(hits),
);
}
if self.expose_cache_status {
@@ -311,30 +310,33 @@ impl Settings {
)
.then_some(("stale", Some(StatusCode::NOT_MODIFIED)))
});
let mut value = if let Some((why, status)) = forwarded {
let mut value = format!("RustProxy; fwd={why}");
use std::fmt::Write as _;
let mut value = String::with_capacity(96);
value.push_str("RustProxy; ");
if let Some((why, status)) = forwarded {
let _ = write!(value, "fwd={why}");
if let Some(status) = status {
value.push_str(&format!("; fwd-status={}", status.as_u16()));
let _ = write!(value, "; fwd-status={}", status.as_u16());
}
value
} else {
match outcome {
"HIT" => format!("RustProxy; hit; ttl={ttl}"),
"MISS" => "RustProxy; fwd=miss".to_owned(),
"BYPASS" if reason == Some("request_method") => {
"RustProxy; fwd=method".to_owned()
"HIT" => {
let _ = write!(value, "hit; ttl={ttl}");
}
_ => "RustProxy; fwd=bypass".to_owned(),
"MISS" => value.push_str("fwd=miss"),
"BYPASS" if reason == Some("request_method") => value.push_str("fwd=method"),
_ => value.push_str("fwd=bypass"),
}
};
}
if let Some(reason) = reason {
value.push_str(&format!("; detail=\"{reason}\""));
let _ = write!(value, "; detail=\"{reason}\"");
}
if transformed {
value.push_str("; transformed");
}
headers.append(::http::header::CACHE_STATUS,
HeaderValue::from_str(&value).expect("generated Cache-Status member"),
HeaderValue::from_maybe_shared(bytes::Bytes::from(value))
.expect("generated Cache-Status member"),
);
}
}
@@ -345,7 +347,7 @@ impl Settings {
headers,
"x-cache-resident-age",
"x-upstream-cache-resident-age",
HeaderValue::from_str(&resident_age.to_string()).expect("integer header"),
HeaderValue::from(resident_age),
);
}
}
@@ -357,9 +359,15 @@ fn insert_cache_diagnostic(
upstream_name: &'static str,
value: HeaderValue,
) {
let upstream_values: Vec<HeaderValue> = headers.get_all(name).iter().cloned().collect();
for upstream_value in upstream_values {
headers.append(upstream_name, upstream_value);
let name = http::HeaderName::from_static(name);
// Fast path: the origin did not send this diagnostic, so there is nothing to
// preserve under the X-Upstream-* alias and no temporary vector is needed.
if headers.contains_key(&name) {
let upstream_values: Vec<HeaderValue> = headers.get_all(&name).iter().cloned().collect();
let upstream_name = http::HeaderName::from_static(upstream_name);
for upstream_value in upstream_values {
headers.append(&upstream_name, upstream_value);
}
}
headers.insert(name, value);
}