Skip to content
Open
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
7 changes: 6 additions & 1 deletion lib/net/http/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,16 @@ def chunked?
# res['Content-Range'] # => "bytes 0-499/1000"
# res.content_range # => 0..499
#
# Returns +nil+ for an unsatisfied range such as <tt>'bytes */1000'</tt>,
# which a <tt>416 Range Not Satisfiable</tt> response may carry;
# see {RFC 9110 Section 14.4}[https://www.rfc-editor.org/rfc/rfc9110.html#section-14.4].
#
def content_range
return nil unless @header['content-range']
m = %r<\A\s*(\w+)\s+(\d+)-(\d+)/(\d+|\*)>.match(self['Content-Range']) or
m = %r<\A\s*(\w+)\s+(?:(\d+)-(\d+)/(?:\d+|\*)|\*/\d+)>.match(self['Content-Range']) or
raise Net::HTTPHeaderSyntaxError, 'wrong Content-Range format'
return unless m[1] == 'bytes'
return unless m[2]
m[2].to_i .. m[3].to_i
end

Expand Down
6 changes: 6 additions & 0 deletions test/net/http/test_httpheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,14 @@ def test_content_range
assert_equal 1..1, @c.content_range
@c['Content-Range'] = "tokens 1-1/1000"
assert_equal nil, @c.content_range
@c['Content-Range'] = "bytes */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"
try_invalid_content_range "bytes */abc"
try_invalid_content_range "bytes */*"
end

def test_range_length
Expand All @@ -366,6 +370,8 @@ def test_range_length
assert_equal 1, @c.range_length
@c['Content-Range'] = "tokens 1-1/1000"
assert_equal nil, @c.range_length
@c['Content-Range'] = "bytes */1000"
assert_equal nil, @c.range_length

try_invalid_content_range "bytes 1-1/abc"
end
Expand Down
21 changes: 21 additions & 0 deletions test/net/http/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,27 @@ def test_read_body_content_encoding_deflate_content_range
assert_equal "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x03", body
end

def test_read_body_unsatisfied_content_range
io = dummy_io(<<EOS)
HTTP/1.1 416 Range Not Satisfiable
Connection: close
Content-Range: bytes */1000

not satisfiable
EOS

res = Net::HTTPResponse.read_new(io)
assert_kind_of Net::HTTPRangeNotSatisfiable, res

body = nil

res.reading_body io, true do
body = res.read_body
end

assert_equal "not satisfiable\r\n", body
end

def test_read_body_content_encoding_deflate_empty_body
io = dummy_io(<<EOS)
HTTP/1.1 200 OK
Expand Down
Loading