Index: lib/net/http/header.rb =================================================================== --- lib/net/http/header.rb (revision 41241) +++ lib/net/http/header.rb (working copy) @@ -258,6 +258,11 @@ module Net::HTTPHeader # Returns an Integer object which represents the HTTP Content-Length: # header field, or +nil+ if that field was not provided. + # + # For a response that has a gzip or deflate Content-Encoding the + # Content-Length will be updated with the decoded length after the body has + # been read. + # def content_length return nil unless key?('Content-Length') len = self['Content-Length'].slice(/\d+/) or Index: lib/net/http/response.rb =================================================================== --- lib/net/http/response.rb (revision 41241) +++ lib/net/http/response.rb (working copy) @@ -190,6 +190,9 @@ class Net::HTTPResponse # end # } # + # If the response has a gzip or deflate Content-Encoding the Content-Length + # will be updated with the decoded length after the body has been read. + # def read_body(dest = nil, &block) if @read raise IOError, "#{self.class}\#read_body called twice" if dest or block @@ -222,6 +225,9 @@ class Net::HTTPResponse # p res.body.object_id # 538149362 # } # + # If the response has a gzip or deflate Content-Encoding the Content-Length + # will be updated with the decoded length after the body has been read. + # def body read_body() end @@ -260,6 +266,7 @@ class Net::HTTPResponse yield inflate_body_io ensure inflate_body_io.finish + self['content-length'] = inflate_body_io.decoded_length end when 'none', 'identity' then self.delete 'content-encoding' @@ -342,10 +349,16 @@ class Net::HTTPResponse class Inflater # :nodoc: ## + # Length of the decoded response body in bytes + + attr_reader :decoded_length + + ## # Creates a new Inflater wrapping +socket+ def initialize socket @socket = socket + @decoded_length = 0 # zlib with automatic gzip detection @inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS) end @@ -367,6 +380,7 @@ class Net::HTTPResponse block = proc do |compressed_chunk| @inflate.inflate(compressed_chunk) do |chunk| dest << chunk + @decoded_length += chunk.bytesize end end Index: test/net/http/test_httpresponse.rb =================================================================== --- test/net/http/test_httpresponse.rb (revision 41241) +++ test/net/http/test_httpresponse.rb (working copy) @@ -96,9 +96,11 @@ EOS 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