Skip to content

Commit

Permalink
fix content-length calculation on range requests (#228)
Browse files Browse the repository at this point in the history
* fix content-length calculation on range requests

* changelog

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
  • Loading branch information
moumar and davidpdrsn committed Mar 9, 2022
1 parent 420ee3a commit 053f5a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tower-http/CHANGELOG.md
Expand Up @@ -21,7 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Fixed

- None.
- Fix content-length calculation on range requests ([#228])

[#228]: https://github.com/tower-rs/tower-http/pull/228

# 0.2.4 (March 5, 2022)

Expand Down
10 changes: 6 additions & 4 deletions tower-http/src/services/fs/serve_dir.rs
Expand Up @@ -512,8 +512,7 @@ impl Future for ResponseFuture {
};
let mut builder = Response::builder()
.header(header::CONTENT_TYPE, file_request.mime_header_value)
.header(header::ACCEPT_RANGES, "bytes")
.header(header::CONTENT_LENGTH, size.to_string());
.header(header::ACCEPT_RANGES, "bytes");
if let Some(encoding) = file_request.maybe_encoding {
builder = builder
.header(header::CONTENT_ENCODING, encoding.into_header_value());
Expand Down Expand Up @@ -597,6 +596,7 @@ fn handle_file_request(
header::CONTENT_RANGE,
format!("bytes {}-{}/{}", range.start(), range.end(), size),
)
.header(header::CONTENT_LENGTH, range.end() - range.start() + 1)
.status(StatusCode::PARTIAL_CONTENT)
.body(body)
}
Expand All @@ -621,7 +621,9 @@ fn handle_file_request(
} else {
empty_body()
};
builder.body(body)
builder
.header(header::CONTENT_LENGTH, size.to_string())
.body(body)
}
}
}
Expand Down Expand Up @@ -1083,7 +1085,7 @@ mod tests {
assert_eq!(res.status(), StatusCode::PARTIAL_CONTENT);
assert_eq!(
res.headers()["content-length"],
file_contents.len().to_string()
(bytes_end_incl - bytes_start_incl + 1).to_string()
);
assert!(res.headers()["content-range"]
.to_str()
Expand Down

0 comments on commit 053f5a5

Please sign in to comment.