ruby-changes:27192
From: naruse <ko1@a...>
Date: Fri, 15 Feb 2013 00:51:57 +0900 (JST)
Subject: [ruby-changes:27192] naruse:r39244 (ruby_2_0_0): merge revision(s) 39232,39233,39238: [Backport #7831][Backport #7852]
naruse 2013-02-15 00:51:45 +0900 (Fri, 15 Feb 2013) New Revision: 39244 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39244 Log: merge revision(s) 39232,39233,39238: [Backport #7831][Backport #7852] * 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. [ruby-trunk - Bug #7831] * lib/net/http.rb: Removed OpenSSL dependency from Net::HTTP. * test/net/http/test_http.rb: Remove Zlib dependency from tests. * test/net/http/test_http_request.rb: ditto. Modified directories: branches/ruby_2_0_0/ Modified files: branches/ruby_2_0_0/ChangeLog branches/ruby_2_0_0/lib/net/http/generic_request.rb branches/ruby_2_0_0/lib/net/http/response.rb branches/ruby_2_0_0/lib/net/http.rb branches/ruby_2_0_0/test/net/http/test_http.rb branches/ruby_2_0_0/test/net/http/test_http_request.rb branches/ruby_2_0_0/test/net/http/test_httpresponse.rb branches/ruby_2_0_0/version.h Index: ruby_2_0_0/ChangeLog =================================================================== --- ruby_2_0_0/ChangeLog (revision 39243) +++ ruby_2_0_0/ChangeLog (revision 39244) @@ -1,3 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1 +Fri Feb 15 00:49:32 2013 Eric Hodel <drbrain@s...> + + * lib/net/http.rb: Removed OpenSSL dependency from Net::HTTP. + + * test/net/http/test_http.rb: Remove Zlib dependency from tests. + * test/net/http/test_http_request.rb: ditto. + +Fri Feb 15 00:49:32 2013 Eric Hodel <drbrain@s...> + + * 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. + [ruby-trunk - Bug #7831] + * 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 13:17:10 2013 Zachary Scott <zachary@z...> * Backport r39168 Warning about TracePoint events to 2.0.0 Index: ruby_2_0_0/lib/net/http/response.rb =================================================================== --- ruby_2_0_0/lib/net/http/response.rb (revision 39243) +++ ruby_2_0_0/lib/net/http/response.rb (revision 39244) @@ -80,6 +80,7 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http/response.rb#L80 @body = nil @read = false @uri = nil + @decode_content = false end # The HTTP version supported by the server. @@ -98,6 +99,10 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http/response.rb#L99 # 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 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http/response.rb#L247 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'] Index: ruby_2_0_0/lib/net/http/generic_request.rb =================================================================== --- ruby_2_0_0/lib/net/http/generic_request.rb (revision 39243) +++ ruby_2_0_0/lib/net/http/generic_request.rb (revision 39244) @@ -27,11 +27,14 @@ class Net::HTTPGenericRequest https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http/generic_request.rb#L27 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 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http/generic_request.rb#L54 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 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http/generic_request.rb#L309 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 Index: ruby_2_0_0/lib/net/http.rb =================================================================== --- ruby_2_0_0/lib/net/http.rb (revision 39243) +++ ruby_2_0_0/lib/net/http.rb (revision 39244) @@ -1410,6 +1410,7 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http.rb#L1410 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 @@ -1423,7 +1424,9 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/lib/net/http.rb#L1424 raise rescue Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, - OpenSSL::SSL::SSLError, Timeout::Error => exception + # avoid a dependency on OpenSSL + defined?(OpenSSL::SSL) ? OpenSSL::SSL::SSLError : IOError, + Timeout::Error => exception if count == 0 && IDEMPOTENT_METHODS_.include?(req.method) count += 1 @socket.close if @socket and not @socket.closed? Index: ruby_2_0_0/version.h =================================================================== --- ruby_2_0_0/version.h (revision 39243) +++ ruby_2_0_0/version.h (revision 39244) @@ -1,11 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1 #define RUBY_VERSION "2.0.0" -#define RUBY_RELEASE_DATE "2013-02-14" +#define RUBY_RELEASE_DATE "2013-02-15" #define RUBY_PATCHLEVEL -1 #define RUBY_BRANCH_NAME "trunk" #define RUBY_RELEASE_YEAR 2013 #define RUBY_RELEASE_MONTH 2 -#define RUBY_RELEASE_DAY 14 +#define RUBY_RELEASE_DAY 15 #include "ruby/version.h" Index: ruby_2_0_0/test/net/http/test_httpresponse.rb =================================================================== --- ruby_2_0_0/test/net/http/test_httpresponse.rb (revision 39243) +++ ruby_2_0_0/test/net/http/test_httpresponse.rb (revision 39244) @@ -86,6 +86,7 @@ x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x1 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_httpresponse.rb#L86 EOS res = Net::HTTPResponse.read_new(io) + res.decode_content = true body = nil @@ -118,6 +119,7 @@ x\x9C\xCBH\xCD\xC9 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_httpresponse.rb#L119 EOS res = Net::HTTPResponse.read_new(io) + res.decode_content = true body = nil @@ -134,6 +136,29 @@ EOS https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_httpresponse.rb#L136 end end + def test_read_body_content_encoding_deflate_disabled + io = dummy_io(<<EOS) +HTTP/1.1 200 OK +Connection: close +Content-Encoding: deflate +Content-Length: 13 + +x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15 +EOS + + res = Net::HTTPResponse.read_new(io) + res.decode_content = false # user set accept-encoding in request + + body = nil + + res.reading_body io, true do + body = res.read_body + 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' + end + def test_read_body_content_encoding_deflate_no_length io = dummy_io(<<EOS) HTTP/1.1 200 OK @@ -144,6 +169,7 @@ x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x1 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_httpresponse.rb#L169 EOS res = Net::HTTPResponse.read_new(io) + res.decode_content = true body = nil Index: ruby_2_0_0/test/net/http/test_http.rb =================================================================== --- ruby_2_0_0/test/net/http/test_http.rb (revision 39243) +++ ruby_2_0_0/test/net/http/test_http.rb (revision 39244) @@ -419,6 +419,7 @@ module TestNetHTTP_version_1_2_methods https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_http.rb#L419 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 https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_http.rb#L441 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' if Net::HTTP::HAVE_ZLIB + } + 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 Index: ruby_2_0_0/test/net/http/test_http_request.rb =================================================================== --- ruby_2_0_0/test/net/http/test_http_request.rb (revision 39243) +++ ruby_2_0_0/test/net/http/test_http_request.rb (revision 39244) @@ -53,5 +53,27 @@ class HTTPRequestTest < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/net/http/test_http_request.rb#L53 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 if Net::HTTP::HAVE_ZLIB + + 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 if Net::HTTP::HAVE_ZLIB + end Property changes on: ruby_2_0_0 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r39232-39233,39238 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/