Skip to content

Commit 8976e42

Browse files
committed
Fix(http): Normalize header case for caching
Headers from upstream servers can have inconsistent casing (e.g., "ETag" vs "etag"). This was causing issues with cache lookups, as the comparison was case-sensitive. This commit fixes the issue by: - Converting all header keys to lowercase before storing them in the cache metadata file. - Using lowercase keys when retrieving headers for cache validation (ETag, Last-Modified). This ensures that cache lookups are case-insensitive and therefore more robust.
1 parent 3edd5e2 commit 8976e42

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/azure.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ fn save_headers_to_file(filename: String, headers: HeaderMap) {
166166
match f {
167167
Ok(mut f) => {
168168
for (key, value) in headers.iter() {
169-
let line = format!("{}:{}\n", key, value.to_str().unwrap());
169+
let key_lower = key.as_str().to_lowercase();
170+
let line = format!("{}:{}\n", key_lower, value.to_str().unwrap());
170171
// TBD: Filter out some names?
171172
f.write_all(line.as_bytes()).unwrap();
172173
}

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ struct Args {
6464
generate_jwt_token: String,
6565
}
6666

67+
// const names for last-modified and etag in lowercase
68+
const LAST_MODIFIED: &str = "last-modified";
69+
const ETAG: &str = "etag";
6770

6871
type FileSemaphores = Arc<RwLock<HashMap<String, Arc<Semaphore>>>>;
6972

@@ -539,6 +542,7 @@ async fn ax_get_file(
539542
}
540543
};
541544

545+
// IMPORTANT! Headers in cache must be stored in lowercase
542546
let received_file = driver_get_file(filepath.clone());
543547
if !received_file.valid {
544548
println!(
@@ -576,7 +580,7 @@ async fn ax_get_file(
576580
headers.insert(header::ETAG, etag.clone());
577581
}
578582
// add last-modified header
579-
if let Some(last_modified) = upstream_headers.get("Last-Modified") {
583+
if let Some(last_modified) = upstream_headers.get(LAST_MODIFIED) {
580584
headers.insert(header::LAST_MODIFIED, last_modified.clone());
581585
}
582586

@@ -587,7 +591,7 @@ async fn ax_get_file(
587591
if method != axum::http::Method::GET && method != axum::http::Method::HEAD {
588592
return (StatusCode::PRECONDITION_FAILED, "Method Not Allowed").into_response();
589593
}
590-
if let Some(etag) = upstream_headers.get("ETag") {
594+
if let Some(etag) = upstream_headers.get(ETAG) {
591595
if if_none_match == etag {
592596
println!(
593597
"{:?} 304 0 {} {} {} {}",
@@ -598,7 +602,7 @@ async fn ax_get_file(
598602
}
599603
// Does request have If-Modified-Since header?
600604
} else if let Some(if_modified_since) = rxheaders.get("If-Modified-Since") {
601-
if let Some(last_modified) = upstream_headers.get("Last-Modified") {
605+
if let Some(last_modified) = upstream_headers.get(LAST_MODIFIED) {
602606
// TODO: Validate properly last_modified
603607
if if_modified_since == last_modified {
604608
println!(

0 commit comments

Comments
 (0)