[前][次][番号順一覧][スレッド一覧]

ruby-changes:71718

From: Jeremy <ko1@a...>
Date: Thu, 14 Apr 2022 00:11:41 +0900 (JST)
Subject: [ruby-changes:71718] 0579486f15 (master): [ruby/net-http] Update the content-length heading when decoding bodies

https://git.ruby-lang.org/ruby.git/commit/?id=0579486f15

From 0579486f154e80d17521494003dcd2499ef74688 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
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.

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]

https://github.com/ruby/net-http/commit/58284e9710

Co-authored-by: st0012 <stan001212@g...>
---
 lib/net/http/response.rb           | 11 +++++++++++
 test/net/http/test_httpresponse.rb | 13 +++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb
index ecbfd42d2b..9cedbdbbfe 100644
--- a/lib/net/http/response.rb
+++ b/lib/net/http/response.rb
@@ -431,6 +431,9 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/lib/net/http/response.rb#L431
       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
@@ -532,6 +535,14 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/lib/net/http/response.rb#L535
       @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 eb2551df46..394b4c5bfa 100644
--- a/test/net/http/test_httpresponse.rb
+++ b/test/net/http/test_httpresponse.rb
@@ -362,9 +362,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L362
 
     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
@@ -390,9 +392,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L392
 
     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
@@ -423,9 +427,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L427
 
     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
@@ -450,6 +456,7 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L456
     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
 
@@ -473,9 +480,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L480
 
     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
@@ -523,9 +532,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L532
 
     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
@@ -549,9 +560,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L560
 
     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.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]