Skip to content

Commit

Permalink
[ruby/net-http] Update the content-length heading when decoding bodies
Browse files Browse the repository at this point in the history
Previously, the content-encoding header was removed and the body
was modified, but the content-length header was not modified,
resulting in the content-length header not matching the body
length.

Don't delete content-length before yielding inflate body, as that
causes a switch to read the entire body instead of reading in
chunks.

Fixes [Bug #16672]

ruby/net-http@58284e9710

Co-authored-by: st0012 <stan001212@gmail.com>
  • Loading branch information
2 people authored and matzbot committed Apr 13, 2022
1 parent 54b53e2 commit 0579486
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/net/http/response.rb
Expand Up @@ -431,6 +431,9 @@ def inflater # :nodoc:
ensure
begin
inflate_body_io.finish
if self['content-length']
self['content-length'] = inflate_body_io.bytes_inflated.to_s
end
rescue => err
# Ignore #finish's error if there is an exception from yield
raise err if success
Expand Down Expand Up @@ -532,6 +535,14 @@ def finish
@inflate.finish
end

##
# The number of bytes inflated, used to update the Content-Length of
# the response.

def bytes_inflated
@inflate.total_out
end

##
# Returns a Net::ReadAdapter that inflates each read chunk into +dest+.
#
Expand Down
13 changes: 13 additions & 0 deletions test/net/http/test_httpresponse.rb
Expand Up @@ -362,9 +362,11 @@ def test_read_body_content_encoding_deflate

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal '5', res['content-length']
assert_equal 'hello', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal '13', res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
end
end
Expand All @@ -390,9 +392,11 @@ def test_read_body_content_encoding_deflate_uppercase

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal '5', res['content-length']
assert_equal 'hello', body
else
assert_equal 'DEFLATE', res['content-encoding']
assert_equal '13', res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
end
end
Expand Down Expand Up @@ -423,9 +427,11 @@ def test_read_body_content_encoding_deflate_chunked

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal nil, res['content-length']
assert_equal 'hello', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal nil, res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
end
end
Expand All @@ -450,6 +456,7 @@ def test_read_body_content_encoding_deflate_disabled
end

assert_equal 'deflate', res['content-encoding'], 'Bug #7831'
assert_equal '13', res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body, 'Bug #7381'
end

Expand All @@ -473,9 +480,11 @@ def test_read_body_content_encoding_deflate_no_length

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal nil, res['content-length']
assert_equal 'hello', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal nil, res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
end
end
Expand Down Expand Up @@ -523,9 +532,11 @@ def test_read_body_content_encoding_deflate_empty_body

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal '0', res['content-length']
assert_equal '', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal '0', res['content-length']
assert_equal '', body
end
end
Expand All @@ -549,9 +560,11 @@ def test_read_body_content_encoding_deflate_empty_body_no_length

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal nil, res['content-length']
assert_equal '', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal nil, res['content-length']
assert_equal '', body
end
end
Expand Down

0 comments on commit 0579486

Please sign in to comment.