Skip to content

Commit a7cb301

Browse files
committed
Update the content-length heading when decoding bodies
Previously, the content-encoding header was removed and the body was modified, but the content-length header was not modified, resulting in the content-length header not matching the body length. Fixes [Bug #16672]
1 parent bfb5a13 commit a7cb301

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/net/http/response.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ def inflater # :nodoc:
263263
case v&.downcase
264264
when 'deflate', 'gzip', 'x-gzip' then
265265
self.delete 'content-encoding'
266+
had_content_length = self.delete 'content-length'
266267

267268
inflate_body_io = Inflater.new(@socket)
268269

@@ -272,6 +273,9 @@ def inflater # :nodoc:
272273
ensure
273274
begin
274275
inflate_body_io.finish
276+
if had_content_length
277+
self['content-length'] = inflate_body_io.bytes_inflated.to_s
278+
end
275279
rescue => err
276280
# Ignore #finish's error if there is an exception from yield
277281
raise err if success
@@ -373,6 +377,14 @@ def finish
373377
@inflate.finish
374378
end
375379

380+
##
381+
# The number of bytes inflated, used to update the Content-Length of
382+
# the response.
383+
384+
def bytes_inflated
385+
@inflate.total_out
386+
end
387+
376388
##
377389
# Returns a Net::ReadAdapter that inflates each read chunk into +dest+.
378390
#

test/net/http/test_httpresponse.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ def test_read_body_content_encoding_deflate
127127

128128
if Net::HTTP::HAVE_ZLIB
129129
assert_equal nil, res['content-encoding']
130+
assert_equal '5', res['content-length']
130131
assert_equal 'hello', body
131132
else
132133
assert_equal 'deflate', res['content-encoding']
134+
assert_equal '13', res['content-length']
133135
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
134136
end
135137
end
@@ -155,9 +157,11 @@ def test_read_body_content_encoding_deflate_uppercase
155157

156158
if Net::HTTP::HAVE_ZLIB
157159
assert_equal nil, res['content-encoding']
160+
assert_equal '5', res['content-length']
158161
assert_equal 'hello', body
159162
else
160163
assert_equal 'DEFLATE', res['content-encoding']
164+
assert_equal '13', res['content-length']
161165
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
162166
end
163167
end
@@ -188,9 +192,11 @@ def test_read_body_content_encoding_deflate_chunked
188192

189193
if Net::HTTP::HAVE_ZLIB
190194
assert_equal nil, res['content-encoding']
195+
assert_equal nil, res['content-length']
191196
assert_equal 'hello', body
192197
else
193198
assert_equal 'deflate', res['content-encoding']
199+
assert_equal nil, res['content-length']
194200
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
195201
end
196202
end
@@ -215,6 +221,7 @@ def test_read_body_content_encoding_deflate_disabled
215221
end
216222

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

@@ -238,9 +245,11 @@ def test_read_body_content_encoding_deflate_no_length
238245

239246
if Net::HTTP::HAVE_ZLIB
240247
assert_equal nil, res['content-encoding']
248+
assert_equal nil, res['content-length']
241249
assert_equal 'hello', body
242250
else
243251
assert_equal 'deflate', res['content-encoding']
252+
assert_equal nil, res['content-length']
244253
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
245254
end
246255
end
@@ -288,9 +297,11 @@ def test_read_body_content_encoding_deflate_empty_body
288297

289298
if Net::HTTP::HAVE_ZLIB
290299
assert_equal nil, res['content-encoding']
300+
assert_equal '0', res['content-length']
291301
assert_equal '', body
292302
else
293303
assert_equal 'deflate', res['content-encoding']
304+
assert_equal '0', res['content-length']
294305
assert_equal '', body
295306
end
296307
end
@@ -314,9 +325,11 @@ def test_read_body_content_encoding_deflate_empty_body_no_length
314325

315326
if Net::HTTP::HAVE_ZLIB
316327
assert_equal nil, res['content-encoding']
328+
assert_equal nil, res['content-length']
317329
assert_equal '', body
318330
else
319331
assert_equal 'deflate', res['content-encoding']
332+
assert_equal nil, res['content-length']
320333
assert_equal '', body
321334
end
322335
end

0 commit comments

Comments
 (0)