From 7137383864b87d916133d23c61a2d076425304ae Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 16 May 2013 11:23:16 +0200 Subject: [PATCH] * lib/net/http/response.rb: transparently deflates the response body, removes the "content-encoding" response header, but does not adjust the "content-length" header accordingly. So, pass the context to the Inflater so that we count the uncompressed data for every chunk inflated, and then on finish we set the right Content-Length. [Bug #8182] * test/net/http/test_httpresponse.rb: enhance testcase to check the right Content-Length --- ChangeLog | 13 +++++++++++++ lib/net/http/response.rb | 8 ++++++-- test/net/http/test_httpresponse.rb | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index c81db70..ae7044f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Thu May 16 18:09:03 2013 Duncan Mac-Vicar P. + + * lib/net/http/response.rb: transparently deflates the response body, + removes the "content-encoding" response header, but does not adjust + the "content-length" header accordingly. + So, pass the context to the Inflater so that we count the + uncompressed data for every chunk inflated, and then on finish we + set the right Content-Length. + [Bug #8182] + + * test/net/http/test_httpresponse.rb: enhance testcase to check + the right Content-Length + Wed May 15 17:55:49 2013 Nobuyoshi Nakada * configure.in (RUBY_PLATFORM): move to config.h as needed by diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index da3e4b4..f920eaa 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -254,7 +254,7 @@ def inflater # :nodoc: when 'deflate', 'gzip', 'x-gzip' then self.delete 'content-encoding' - inflate_body_io = Inflater.new(@socket) + inflate_body_io = Inflater.new(@socket, self) begin yield inflate_body_io @@ -344,8 +344,10 @@ class Inflater # :nodoc: ## # Creates a new Inflater wrapping +socket+ - def initialize socket + def initialize(socket, response) @socket = socket + @response = response + @deflated_len = 0 # zlib with automatic gzip detection @inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS) end @@ -355,6 +357,7 @@ def initialize socket def finish @inflate.finish + @response['content-length'] = @deflated_len end ## @@ -367,6 +370,7 @@ def inflate_adapter(dest) block = proc do |compressed_chunk| @inflate.inflate(compressed_chunk) do |chunk| dest << chunk + @deflated_len += chunk.bytesize end end diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 974f829..1031fa5 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -96,9 +96,11 @@ def test_read_body_content_encoding_deflate if Net::HTTP::HAVE_ZLIB assert_equal nil, res['content-encoding'] + assert_equal '5', res['content-length'], "Bug #8182" assert_equal 'hello', body else assert_equal 'deflate', res['content-encoding'] + assert_equal '13', res['content-length'], "Bug #8182" assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body end end -- 1.8.1.6