From f58d39807541d8f50ba682183ab9097ddcb52698 Mon Sep 17 00:00:00 2001 From: drbrain Date: Thu, 14 Feb 2013 01:08:19 +0000 Subject: [PATCH] * lib/net/http: Do not handle Content-Encoding when the user sets Accept-Encoding. This allows users to handle Content-Encoding for themselves. This restores backwards-compatibility with Ruby 1.x. * lib/net/http/generic_request.rb: ditto. * lib/net/http/response.rb: ditto * test/net/http/test_http.rb: Test for the above. * test/net/http/test_http_request.rb: ditto. * test/net/http/test_httpresponse.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ lib/net/http.rb | 1 + lib/net/http/generic_request.rb | 19 +++++++++++++++++++ lib/net/http/response.rb | 6 ++++++ test/net/http/test_http.rb | 19 +++++++++++++++++++ test/net/http/test_http_request.rb | 22 ++++++++++++++++++++++ test/net/http/test_httpresponse.rb | 26 ++++++++++++++++++++++++++ 7 files changed, 104 insertions(+) diff --git a/ChangeLog b/ChangeLog index aec5fe6474..927586d150 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Thu Feb 14 10:05:57 2013 Eric Hodel + + * lib/net/http: Do not handle Content-Encoding when the user sets + Accept-Encoding. This allows users to handle Content-Encoding for + themselves. This restores backwards-compatibility with Ruby 1.x. + * lib/net/http/generic_request.rb: ditto. + * lib/net/http/response.rb: ditto + * test/net/http/test_http.rb: Test for the above. + * test/net/http/test_http_request.rb: ditto. + * test/net/http/test_httpresponse.rb: ditto. + Thu Feb 14 08:18:47 2013 Tanaka Akira * ext/socket/extconf.rb: don't define HAVE_SA_LEN and HAVE_SA_LEN. diff --git a/lib/net/http.rb b/lib/net/http.rb index d460aeaa0b..4b1a65767f 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -1410,6 +1410,7 @@ module Net #:nodoc: req.exec @socket, @curr_http_version, edit_path(req.path) begin res = HTTPResponse.read_new(@socket) + res.decode_content = req.decode_content end while res.kind_of?(HTTPContinue) res.uri = req.uri diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index e822ac9181..6e79bb9c2e 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -27,11 +27,14 @@ class Net::HTTPGenericRequest raise ArgumentError, "HTTP request path is empty" if path.empty? @path = path + @decode_content = false + if @response_has_body and Net::HTTP::HAVE_ZLIB then if !initheader || !initheader.keys.any? { |k| %w[accept-encoding range].include? k.downcase } then + @decode_content = true initheader = initheader ? initheader.dup : {} initheader["accept-encoding"] = "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" @@ -51,10 +54,25 @@ class Net::HTTPGenericRequest attr_reader :path attr_reader :uri + # Automatically set to false if the user sets the Accept-Encoding header. + # This indicates they wish to handle Content-encoding in responses + # themselves. + attr_reader :decode_content + def inspect "\#<#{self.class} #{@method}>" end + ## + # Don't automatically decode response content-encoding if the user indicates + # they want to handle it. + + def []=(key, val) # :nodoc: + @decode_content = false if key.downcase == 'accept-encoding' + + super key, val + end + def request_body_permitted? @request_has_body end @@ -291,6 +309,7 @@ class Net::HTTPGenericRequest if IO.select([sock.io], nil, nil, sock.continue_timeout) res = Net::HTTPResponse.read_new(sock) unless res.kind_of?(Net::HTTPContinue) + res.decode_content = @decode_content throw :response, res end end diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index bde3b5b440..da3e4b4c8c 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -80,6 +80,7 @@ class Net::HTTPResponse @body = nil @read = false @uri = nil + @decode_content = false end # The HTTP version supported by the server. @@ -98,6 +99,10 @@ class Net::HTTPResponse # if a URI was used to create the request. attr_reader :uri + # Set to true automatically when the request did not contain an + # Accept-Encoding header from the user. + attr_accessor :decode_content + def inspect "#<#{self.class} #{@code} #{@message} readbody=#{@read}>" end @@ -242,6 +247,7 @@ class Net::HTTPResponse def inflater # :nodoc: return yield @socket unless Net::HTTP::HAVE_ZLIB + return yield @socket unless @decode_content return yield @socket if self['content-range'] case self['content-encoding'] diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 280d08c321..32c8744413 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -419,6 +419,7 @@ module TestNetHTTP_version_1_2_methods def test_request start {|http| _test_request__GET http + _test_request__accept_encoding http _test_request__file http # _test_request__range http # WEBrick does not support Range: header. _test_request__HEAD http @@ -440,6 +441,24 @@ module TestNetHTTP_version_1_2_methods end assert_equal $test_net_http_data.size, res.body.size assert_equal $test_net_http_data, res.body + + assert res.decode_content, 'Bug #7831' + } + end + + def _test_request__accept_encoding(http) + req = Net::HTTP::Get.new('/', 'accept-encoding' => 'deflate') + http.request(req) {|res| + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + + refute res.decode_content, 'Bug #7831' } end diff --git a/test/net/http/test_http_request.rb b/test/net/http/test_http_request.rb index c01e52c0b4..4ce93acde3 100644 --- a/test/net/http/test_http_request.rb +++ b/test/net/http/test_http_request.rb @@ -53,5 +53,27 @@ class HTTPRequestTest < Test::Unit::TestCase assert_equal expected, req.to_hash end + def test_initialize_accept_encoding + req1 = Net::HTTP::Get.new '/' + + assert req1.decode_content, 'Bug #7831 - automatically decode content' + + req2 = Net::HTTP::Get.new '/', 'accept-encoding' => 'identity' + + refute req2.decode_content, + 'Bug #7381 - do not decode content if the user overrides' + end + + def test_header_set + req = Net::HTTP::Get.new '/' + + assert req.decode_content, 'Bug #7831 - automatically decode content' + + req['accept-encoding'] = 'identity' + + refute req.decode_content, + 'Bug #7831 - do not decode content if the user overrides' + end + end diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 121b81fe96..974f8296cc 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -86,6 +86,7 @@ x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15 EOS res = Net::HTTPResponse.read_new(io) + res.decode_content = true body = nil @@ -118,6 +119,7 @@ x\x9C\xCBH\xCD\xC9 EOS res = Net::HTTPResponse.read_new(io) + res.decode_content = true body = nil @@ -134,6 +136,29 @@ EOS end end + def test_read_body_content_encoding_deflate_disabled + io = dummy_io(<