Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix content-length calculation on range requests #228

Merged
merged 2 commits into from Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
);
assert!(res.headers()["content-range"]
.to_str()
Expand Down