mirror of
https://github.com/tower-rs/tower-http.git
synced 2026-03-06 13:19:32 +00:00
fix(fs): Return 0 content length for range requests for empty files (#556)
This commit is contained in:
parent
33645694bd
commit
6c20928e50
0
test-files/empty.txt
Normal file
0
test-files/empty.txt
Normal file
@ -269,12 +269,18 @@ fn build_response(output: FileOpened) -> Response<ResponseBody> {
|
||||
empty_body()
|
||||
};
|
||||
|
||||
let content_length = if size == 0 {
|
||||
0
|
||||
} else {
|
||||
range.end() - range.start() + 1
|
||||
};
|
||||
|
||||
builder
|
||||
.header(
|
||||
header::CONTENT_RANGE,
|
||||
format!("bytes {}-{}/{}", range.start(), range.end(), size),
|
||||
)
|
||||
.header(header::CONTENT_LENGTH, range.end() - range.start() + 1)
|
||||
.header(header::CONTENT_LENGTH, content_length)
|
||||
.status(StatusCode::PARTIAL_CONTENT)
|
||||
.body(body)
|
||||
.unwrap()
|
||||
|
||||
@ -506,6 +506,25 @@ async fn access_space_percent_encoded_uri_path() {
|
||||
assert_eq!(res.headers()["content-type"], "text/plain");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_partial_empty() {
|
||||
let svc = ServeDir::new("../test-files");
|
||||
|
||||
let req = Request::builder()
|
||||
.uri("/empty.txt")
|
||||
.header("Range", "bytes=0-")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
let res = svc.oneshot(req).await.unwrap();
|
||||
|
||||
assert_eq!(res.status(), StatusCode::PARTIAL_CONTENT);
|
||||
assert_eq!(res.headers()["content-length"], "0");
|
||||
assert_eq!(res.headers()["content-range"], "bytes 0-0/0");
|
||||
|
||||
let body = to_bytes(res.into_body()).await.ok().unwrap();
|
||||
assert!(body.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_partial_in_bounds() {
|
||||
let svc = ServeDir::new("..");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user