Skip to content

Comments

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

Merged
seanmonstar merged 1 commit intotower-rs:mainfrom
Dav1dde:dav1d/empty-range-serve-dir-content-length
Apr 9, 2025
Merged

Return 0 content length for range requests for empty files#556
seanmonstar merged 1 commit intotower-rs:mainfrom
Dav1dde:dav1d/empty-range-serve-dir-content-length

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