Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the content-length heading when decoding bodies #16

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def inflater # :nodoc:
case v&.downcase
when 'deflate', 'gzip', 'x-gzip' then
self.delete 'content-encoding'
had_content_length = self.delete 'content-length'

inflate_body_io = Inflater.new(@socket)

Expand All @@ -272,6 +273,9 @@ def inflater # :nodoc:
ensure
begin
inflate_body_io.finish
if had_content_length
self['content-length'] = inflate_body_io.bytes_inflated.to_s
end
rescue => err
# Ignore #finish's error if there is an exception from yield
raise err if success
Expand Down Expand Up @@ -373,6 +377,14 @@ def finish
@inflate.finish
end

##
# The number of bytes inflated, used to update the Content-Length of
# the response.

def bytes_inflated
@inflate.total_out
end

##
# Returns a Net::ReadAdapter that inflates each read chunk into +dest+.
#
Expand Down
13 changes: 13 additions & 0 deletions test/net/http/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,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']
assert_equal 'hello', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal '13', res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
end
end
Expand All @@ -155,9 +157,11 @@ def test_read_body_content_encoding_deflate_uppercase

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal '5', res['content-length']
assert_equal 'hello', body
else
assert_equal 'DEFLATE', res['content-encoding']
assert_equal '13', res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
end
end
Expand Down Expand Up @@ -188,9 +192,11 @@ def test_read_body_content_encoding_deflate_chunked

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal nil, res['content-length']
assert_equal 'hello', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal nil, res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
end
end
Expand All @@ -215,6 +221,7 @@ def test_read_body_content_encoding_deflate_disabled
end

assert_equal 'deflate', res['content-encoding'], 'Bug #7831'
assert_equal '13', res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body, 'Bug #7381'
end

Expand All @@ -238,9 +245,11 @@ def test_read_body_content_encoding_deflate_no_length

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal nil, res['content-length']
assert_equal 'hello', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal nil, res['content-length']
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
end
end
Expand Down Expand Up @@ -288,9 +297,11 @@ def test_read_body_content_encoding_deflate_empty_body

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal '0', res['content-length']
assert_equal '', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal '0', res['content-length']
assert_equal '', body
end
end
Expand All @@ -314,9 +325,11 @@ def test_read_body_content_encoding_deflate_empty_body_no_length

if Net::HTTP::HAVE_ZLIB
assert_equal nil, res['content-encoding']
assert_equal nil, res['content-length']
assert_equal '', body
else
assert_equal 'deflate', res['content-encoding']
assert_equal nil, res['content-length']
assert_equal '', body
end
end
Expand Down