Skip to content

Support endless ranges in HTTPHeader#set_range - #390

Open
mokshupadhyay wants to merge 1 commit into
ruby:masterfrom
mokshupadhyay:fix/set-range-endless
Open

mokshupadhyay wants to merge 1 commit into
ruby:masterfrom
mokshupadhyay:fix/set-range-endless

Conversation

@mokshupadhyay

Copy link
Copy Markdown

Net::HTTPHeader#set_range (also range=) builds a Range request header from a Ruby Range. It has two bugs:

  1. An endless range such as (100..) crashes, because Range#end is nil and the code then evaluates nil < ... / last -= 1.
  2. 0..-1 ("the whole content") is sent as bytes=-0, a suffix range of length zero, which can never be satisfied. The check is first > 0 ? "#{first}-" : "-#{-first}", which treats 0 like a negative number.

Real header produced for req.range = r on master and with this change:

range before after
5.. NoMethodError: undefined method '<' for nil bytes=5-
10.. NoMethodError bytes=10-
0..-1 bytes=-0 bytes=0-
5..-1 bytes=5- bytes=5-
0..9, 1...5, -3..-1 bytes=0-9, bytes=1-4, bytes=-3 same

The fix treats an endless range like first..-1 (runs to the end of the content) and uses first >= 0 for the open-ended form. The Net::HTTPHeader#range getter already reads bytes=1- back as 1..-1, so this matches the existing behaviour in the other direction.

Test: test_range_endless in test/net/http/test_httpheader.rb (100.., 100..., 0.., -5.. and 0..-1); it errors on master and passes now. rake test passes (224 tests, 994 assertions). No rubocop config in the repo. Not covered: beginless ranges, which I did not change. I did not check which Ruby release bundles the current version of this gem.

req.range = (100..) raised NoMethodError because Range#end is nil. Treat
it like 100..-1 and send "bytes=100-". Also send "bytes=0-" for 0..-1;
it was sent as "bytes=-0", a zero-length suffix that no server can satisfy.

This branch has not been deployed

No deployments
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.

1 participant