Skip to content

Return 0 content length for range requests for empty files #556

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

Conversation

Dav1dde
Copy link
Contributor

@Dav1dde Dav1dde commented Apr 9, 2025

Motivation

Also came up here: seanmonstar/reqwest#1559 (comment)

Currently tower-http's ServeDir handles range requests for empty files slightly incorrectly, it returns a correctly empty Content-Range but the Content-Length is set to 1. Which causes troubles in reqwest (it tries to read 1 byte which never comes).

For example:

└─ %  curl http://localhost:49488/files/index.android.bundle -i -H "Range: bytes=0-"
HTTP/1.1 206 Partial Content
content-type: application/octet-stream
accept-ranges: bytes
last-modified: Thu, 09 Jan 2025 08:11:11 GMT
content-range: bytes 0-0/0
content-length: 1
date: Wed, 09 Apr 2025 08:36:05 GMT

curl: (18) transfer closed with 1 bytes remaining to read

┌─── ~ 
└─ %  curl http://localhost:49488/files/index.android.bundle -i                     
HTTP/1.1 200 OK
content-type: application/octet-stream
accept-ranges: bytes
last-modified: Thu, 09 Jan 2025 08:11:11 GMT
content-length: 0
date: Wed, 09 Apr 2025 08:36:11 GMT

Solution

For empty files, return a content length of 1, instead of calculating it from the inclusive range.

@seanmonstar seanmonstar merged commit 6c20928 into tower-rs:main Apr 9, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants