From 58adb1636be32fb95173f01e448673dbae4511b0 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 1 Mar 2021 13:48:06 -0800 Subject: [ruby/net-http] Update the content-length heading when decoding bodies 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. Fixes [Bug #16672] https://github.com/ruby/net-http/commit/a7cb30124c --- lib/net/http/response.rb | 12 ++++++++++++ test/net/http/test_httpresponse.rb | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 08eaeb2cac..cbe16ca205 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -263,6 +263,7 @@ class Net::HTTPResponse case v&.downcase when 'deflate', 'gzip', 'x-gzip' then self.delete 'content-encoding' + had_content_length = self.delete 'content-length' inflate_body_io = Inflater.new(@socket) @@ -272,6 +273,9 @@ class Net::HTTPResponse ensure begin inflate_body_io.finish + if had_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 @@ -373,6 +377,14 @@ class Net::HTTPResponse @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+. # diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 86a467ac19..555c1cd6bd 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -127,9 +127,11 @@ EOS 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 @@ -155,9 +157,11 @@ EOS 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 @@ -188,9 +192,11 @@ EOS 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 @@ -215,6 +221,7 @@ EOS 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 @@ -238,9 +245,11 @@ EOS 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 @@ -288,9 +297,11 @@ EOS 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 @@ -314,9 +325,11 @@ EOS 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 -- cgit v1.2.3