Project

General

Profile

Bug #8182 » net.http.bug8182.patch

drbrain (Eric Hodel), 06/12/2013 08:36 AM

View differences:

lib/net/http/header.rb (working copy)
# 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
lib/net/http/response.rb (working copy)
# 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
......
# 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
......
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'
......
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
......
block = proc do |compressed_chunk|
@inflate.inflate(compressed_chunk) do |chunk|
dest << chunk
@decoded_length += chunk.bytesize
end
end
test/net/http/test_httpresponse.rb (working copy)
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
(2-2/3)