Ignore Range header if served file is 0 bytes #2159
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Normally
Rack::Files
truncates a byte range to fit a file's contents--as long as at least some bytes are within range. However, an empty (0 byte) file is a special case since there are no requested bytes that can be within range.Previously, 0 byte files requested with a Range header always resulted in a 416 error. This PR changes 0 byte files to ignore the Range header and return the empty file.
Ignoring the Range header is allowed per RFC 9110 §14.2.
This improves compatibility with clients that speculatively request files with byte ranges. For example, Nginx's slice / slice_range feature causes all requests to utilize Range.
Slightly ironically, this also results in a smaller response. (On this basis, it could even be argued to ignore Range for files below 50-100 bytes.)
Before
After
$ curl -i -H 'Range: bytes=0-1023' http://localhost:3000/favicon.ico HTTP/1.1 200 OK last-modified: Tue, 15 Jan 2019 21:51:01 GMT content-type: image/vnd.microsoft.icon Content-Length: 0