ruby-changes:72192
From: Shishir <ko1@a...>
Date: Thu, 16 Jun 2022 23:35:38 +0900 (JST)
Subject: [ruby-changes:72192] c310691dd8 (master): [ruby/net-http] Make `Net::HTTPHeader#content_range` return nil on non-byte units
https://git.ruby-lang.org/ruby.git/commit/?id=c310691dd8 From c310691dd8087eac5153b3c778ea898dcf387b57 Mon Sep 17 00:00:00 2001 From: Shishir Joshi <shishir127@u...> Date: Thu, 16 Jun 2022 16:46:47 +0530 Subject: [ruby/net-http] Make `Net::HTTPHeader#content_range` return nil on non-byte units * Returning nil from the `content_range` method instead of raising an error when the unit in the content-range header is not "bytes". Fix https://bugs.ruby-lang.org/issues/11450 https://github.com/ruby/net-http/commit/0b5030dd86 Co-Authored-By: Nobuyoshi Nakada <nobu@r...> --- lib/net/http/header.rb | 5 +++-- test/net/http/test_httpheader.rb | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index a8901e79cb..b0ec4b0625 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -338,9 +338,10 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L338 # 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. diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb index cfbe36bcfd..b1ca9e8225 100644 --- a/test/net/http/test_httpheader.rb +++ b/test/net/http/test_httpheader.rb @@ -308,6 +308,18 @@ class HTTPHeaderTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpheader.rb#L308 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 @@ -317,6 +329,15 @@ class HTTPHeaderTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpheader.rb#L329 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? -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/