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

ruby-changes:59276

From: Yusuke <ko1@a...>
Date: Mon, 16 Dec 2019 23:29:09 +0900 (JST)
Subject: [ruby-changes:59276] 5105240b1e (master): lib/net/http/response.rb: support raw deflate correctly

https://git.ruby-lang.org/ruby.git/commit/?id=5105240b1e

From 5105240b1e851410020b3b3f1a2bead7ffdd4291 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Mon, 16 Dec 2019 23:20:42 +0900
Subject: lib/net/http/response.rb: support raw deflate correctly

Net::HTTP had used `Zlib::Inflate.new(32 + Zlib::MAX_WBITS)` for all
content encoding (deflate, zlib, and gzip).
But the argument `32 + Zlib::MAX_WBITS` means zlib and gzip decoding
with automatic header detection, so (raw) deflate compression had not
been supported.

This change makes it support raw deflate correctly by passing an
argument `-Zlib::MAX_WBITS` (which means raw deflate) to
`Zlib::Inflate.new`.  All deflate-mode tests are fixed too.

[Bug #11268]

diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb
index 5a94f95..7c89876 100644
--- a/lib/net/http/response.rb
+++ b/lib/net/http/response.rb
@@ -264,7 +264,7 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/lib/net/http/response.rb#L264
     when 'deflate', 'gzip', 'x-gzip' then
       self.delete 'content-encoding'
 
-      inflate_body_io = Inflater.new(@socket)
+      inflate_body_io = Inflater.new(@socket, v.downcase == "deflate")
 
       begin
         yield inflate_body_io
@@ -358,10 +358,10 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/lib/net/http/response.rb#L358
     ##
     # Creates a new Inflater wrapping +socket+
 
-    def initialize socket
+    def initialize(socket, raw_deflate)
       @socket = socket
       # zlib with automatic gzip detection
-      @inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
+      @inflate = Zlib::Inflate.new(raw_deflate ? -Zlib::MAX_WBITS : 32 + Zlib::MAX_WBITS)
     end
 
     ##
diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb
index a03bb2e..14a804d 100644
--- a/test/net/http/test_httpresponse.rb
+++ b/test/net/http/test_httpresponse.rb
@@ -107,9 +107,9 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L107
 HTTP/1.1 200 OK
 Connection: close
 Content-Encoding: deflate
-Content-Length: 13
+Content-Length: 7
 
-x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+\xCBH\xCD\xC9\xC9\a\x00
 EOS
 
     res = Net::HTTPResponse.read_new(io)
@@ -126,7 +126,7 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L126
       assert_equal 'hello', body
     else
       assert_equal 'deflate', res['content-encoding']
-      assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
+      assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body
     end
   end
 
@@ -135,9 +135,9 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L135
 HTTP/1.1 200 OK
 Connection: close
 Content-Encoding: DEFLATE
-Content-Length: 13
+Content-Length: 7
 
-x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+\xCBH\xCD\xC9\xC9\a\x00
 EOS
 
     res = Net::HTTPResponse.read_new(io)
@@ -154,7 +154,7 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L154
       assert_equal 'hello', body
     else
       assert_equal 'DEFLATE', res['content-encoding']
-      assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
+      assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body
     end
   end
 
@@ -165,10 +165,10 @@ Connection: close https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L165
 Content-Encoding: deflate
 Transfer-Encoding: chunked
 
-6
-x\x9C\xCBH\xCD\xC9
-7
-\xC9\a\x00\x06,\x02\x15
+4
+\xCBH\xCD\xC9
+3
+\xC9\a\x00
 0
 
 EOS
@@ -187,7 +187,7 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L187
       assert_equal 'hello', body
     else
       assert_equal 'deflate', res['content-encoding']
-      assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
+      assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body
     end
   end
 
@@ -196,9 +196,9 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L196
 HTTP/1.1 200 OK
 Connection: close
 Content-Encoding: deflate
-Content-Length: 13
+Content-Length: 7
 
-x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+\xCBH\xCD\xC9\xC9\a\x00
 EOS
 
     res = Net::HTTPResponse.read_new(io)
@@ -211,7 +211,7 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L211
     end
 
     assert_equal 'deflate', res['content-encoding'], 'Bug #7831'
-    assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body, 'Bug #7381'
+    assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body, 'Bug #7381'
   end
 
   def test_read_body_content_encoding_deflate_no_length
@@ -220,7 +220,7 @@ HTTP/1.1 200 OK https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L220
 Connection: close
 Content-Encoding: deflate
 
-x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+\xCBH\xCD\xC9\xC9\a\x00
 EOS
 
     res = Net::HTTPResponse.read_new(io)
@@ -237,7 +237,7 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L237
       assert_equal 'hello', body
     else
       assert_equal 'deflate', res['content-encoding']
-      assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
+      assert_equal "\xCBH\xCD\xC9\xC9\a\x00\r\n", body
     end
   end
 
-- 
cgit v0.10.2


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

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