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

Ignore Range header if served file is 0 bytes #2159

Merged
merged 1 commit into from
Feb 26, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file. For info on
- `set_cookie_header` utility now supports the `partitioned` cookie attribute. This is required by Chrome in some embedded contexts. ([#2131](https://github.com/rack/rack/pull/2131), [@flavio-b])
- Remove non-standard status codes 306, 509, & 510 and update descriptions for 413, 422, & 451. ([#2137](https://github.com/rack/rack/pull/2137), [@wtn])
- Add fallback lookup and deprecation warning for obsolete status symbols. ([#2137](https://github.com/rack/rack/pull/2137), [@wtn])
- In `Rack::Files`, ignore the `Range` header if served file is 0 bytes. ([#2159](https://github.com/rack/rack/pull/2159), [@zarqman])

## [3.0.9] - 2024-01-31

Expand Down
2 changes: 2 additions & 0 deletions lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ def byte_ranges(env, size)

def get_byte_ranges(http_range, size)
# See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35>
# Ignore Range when file size is 0 to avoid a 416 error.
return nil if size.zero?
zarqman marked this conversation as resolved.
Show resolved Hide resolved
return nil unless http_range && http_range =~ /bytes=([^;]+)/
ranges = []
$1.split(/,\s*/).each do |range_spec|
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions test/spec_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ def filesize(_); 10000 end
res["content-range"].must_equal "bytes */209"
end

it "ignores range when file is 0 bytes" do
env = Rack::MockRequest.env_for("/cgi/assets/images/favicon.ico")
env["HTTP_RANGE"] = "bytes=0-1234"
res = Rack::MockResponse.new(*files(DOCROOT).call(env))

res.status.must_equal 200
res["content-range"].must_be_nil
end

it "support custom http headers" do
env = Rack::MockRequest.env_for("/cgi/test")
status, heads, _ = files(DOCROOT, 'cache-control' => 'public, max-age=38',
Expand Down
10 changes: 5 additions & 5 deletions test/spec_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,11 @@ def initialize(*)
end

it "handle byte ranges of empty files" do
Rack::Utils.get_byte_ranges("bytes=123-456", 0).must_equal []
Rack::Utils.get_byte_ranges("bytes=0-", 0).must_equal []
Rack::Utils.get_byte_ranges("bytes=-100", 0).must_equal []
Rack::Utils.get_byte_ranges("bytes=0-0", 0).must_equal []
Rack::Utils.get_byte_ranges("bytes=-0", 0).must_equal []
Rack::Utils.get_byte_ranges("bytes=123-456", 0).must_be_nil
Rack::Utils.get_byte_ranges("bytes=0-", 0).must_be_nil
Rack::Utils.get_byte_ranges("bytes=-100", 0).must_be_nil
Rack::Utils.get_byte_ranges("bytes=0-0", 0).must_be_nil
Rack::Utils.get_byte_ranges("bytes=-0", 0).must_be_nil
end
end

Expand Down