Skip to content

Make Net::HTTPHeader#content_range return nil on non-byte units #63

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

Merged
merged 1 commit into from
Jun 16, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/net/http/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ def chunked?
# fits inside the full entity body, as range of byte offsets.
def content_range
return nil unless @header['content-range']
m = %r<bytes\s+(\d+)-(\d+)/(\d+|\*)>i.match(self['Content-Range']) or
m = %r<\A\s*(\w+)\s+(\d+)-(\d+)/(\d+|\*)>.match(self['Content-Range']) or
raise Net::HTTPHeaderSyntaxError, 'wrong Content-Range format'
m[1].to_i .. m[2].to_i
return unless m[1] == 'bytes'
m[2].to_i .. m[3].to_i
end

# The length of the range represented in Content-Range: header.
Expand Down
21 changes: 21 additions & 0 deletions test/net/http/test_httpheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ def test_range=
end

def test_content_range
@c['Content-Range'] = "bytes 0-499/1000"
assert_equal 0..499, @c.content_range
@c['Content-Range'] = "bytes 1-500/1000"
assert_equal 1..500, @c.content_range
@c['Content-Range'] = "bytes 1-1/1000"
assert_equal 1..1, @c.content_range
@c['Content-Range'] = "tokens 1-1/1000"
assert_equal nil, @c.content_range

try_invalid_content_range "invalid"
try_invalid_content_range "bytes 123-abc"
try_invalid_content_range "bytes abc-123"
end

def test_range_length
Expand All @@ -317,6 +329,15 @@ def test_range_length
assert_equal 500, @c.range_length
@c['Content-Range'] = "bytes 1-1/1000"
assert_equal 1, @c.range_length
@c['Content-Range'] = "tokens 1-1/1000"
assert_equal nil, @c.range_length

try_invalid_content_range "bytes 1-1/abc"
end

def try_invalid_content_range(s)
@c['Content-Range'] = "#{s}"
assert_raise(Net::HTTPHeaderSyntaxError, s){ @c.content_range }
end

def test_chunked?
Expand Down