1
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs.git synced 2025-04-23 00:24:36 +00:00

Disable chunked transfer encoding of responses

This also implicitly adds the content-length header, which improves
compatibility with CDN caches such as Cloudflare when using range requests.
This commit is contained in:
Richard Schwab 2025-03-26 05:08:06 +01:00
parent a71812930e
commit 3063346b9c
No known key found for this signature in database
2 changed files with 28 additions and 2 deletions

View file

@ -1252,6 +1252,10 @@ async fn ranged_file_head_resp<S: Store + 'static>(
if let Some(content_range) = range::to_content_range(range, len) {
let mut builder = HttpResponse::PartialContent();
builder.insert_header(content_range);
builder.insert_header((
actix_web::http::header::CONTENT_LENGTH,
range::to_ranged_content_length(range, len).ok_or(UploadError::Range)?,
));
builder
} else {
HttpResponse::RangeNotSatisfiable()
@ -1261,7 +1265,12 @@ async fn ranged_file_head_resp<S: Store + 'static>(
}
} else {
// no range header
HttpResponse::Ok()
let mut builder = HttpResponse::Ok();
builder.insert_header((
actix_web::http::header::CONTENT_LENGTH,
store.len(&identifier).await?,
));
builder
};
Ok(srv_head(
@ -1288,6 +1297,9 @@ async fn ranged_file_resp<S: Store + 'static>(
if let Some(content_range) = range::to_content_range(range, len) {
let mut builder = HttpResponse::PartialContent();
builder.insert_header(content_range);
builder.no_chunking(
range::to_ranged_content_length(range, len).ok_or(UploadError::Range)?,
);
(
builder,
Either::left(Either::left(
@ -1310,7 +1322,9 @@ async fn ranged_file_resp<S: Store + 'static>(
if not_found {
(HttpResponse::NotFound(), Either::right(stream))
} else {
(HttpResponse::Ok(), Either::right(stream))
let mut builder = HttpResponse::Ok();
builder.no_chunking(store.len(&identifier).await?);
(builder, Either::right(stream))
}
};

View file

@ -51,3 +51,15 @@ pub(crate) fn to_content_range(
})
})
}
pub(crate) fn to_ranged_content_length(
byte_range: &ByteRangeSpec,
total_content_length: u64,
) -> Option<u64> {
byte_range
.to_satisfiable_range(total_content_length)
.map(|(start, end)| {
// END IS INCLUSIVE
end + 1 - start
})
}