Skip to content

Commit 58284e9

Browse files
jeremyevansst0012
andcommitted
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. Don't delete content-length before yielding inflate body, as that causes a switch to read the entire body instead of reading in chunks. Fixes [Bug #16672] Co-authored-by: st0012 <[email protected]>
1 parent 6233e6b commit 58284e9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/net/http/response.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ def inflater # :nodoc:
431431
ensure
432432
begin
433433
inflate_body_io.finish
434+
if self['content-length']
435+
self['content-length'] = inflate_body_io.bytes_inflated.to_s
436+
end
434437
rescue => err
435438
# Ignore #finish's error if there is an exception from yield
436439
raise err if success
@@ -532,6 +535,14 @@ def finish
532535
@inflate.finish
533536
end
534537

538+
##
539+
# The number of bytes inflated, used to update the Content-Length of
540+
# the response.
541+
542+
def bytes_inflated
543+
@inflate.total_out
544+
end
545+
535546
##
536547
# Returns a Net::ReadAdapter that inflates each read chunk into +dest+.
537548
#

test/net/http/test_httpresponse.rb

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

363363
if Net::HTTP::HAVE_ZLIB
364364
assert_equal nil, res['content-encoding']
365+
assert_equal '5', res['content-length']
365366
assert_equal 'hello', body
366367
else
367368
assert_equal 'deflate', res['content-encoding']
369+
assert_equal '13', res['content-length']
368370
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
369371
end
370372
end
@@ -390,9 +392,11 @@ def test_read_body_content_encoding_deflate_uppercase
390392

391393
if Net::HTTP::HAVE_ZLIB
392394
assert_equal nil, res['content-encoding']
395+
assert_equal '5', res['content-length']
393396
assert_equal 'hello', body
394397
else
395398
assert_equal 'DEFLATE', res['content-encoding']
399+
assert_equal '13', res['content-length']
396400
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
397401
end
398402
end
@@ -423,9 +427,11 @@ def test_read_body_content_encoding_deflate_chunked
423427

424428
if Net::HTTP::HAVE_ZLIB
425429
assert_equal nil, res['content-encoding']
430+
assert_equal nil, res['content-length']
426431
assert_equal 'hello', body
427432
else
428433
assert_equal 'deflate', res['content-encoding']
434+
assert_equal nil, res['content-length']
429435
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
430436
end
431437
end
@@ -450,6 +456,7 @@ def test_read_body_content_encoding_deflate_disabled
450456
end
451457

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

@@ -473,9 +480,11 @@ def test_read_body_content_encoding_deflate_no_length
473480

474481
if Net::HTTP::HAVE_ZLIB
475482
assert_equal nil, res['content-encoding']
483+
assert_equal nil, res['content-length']
476484
assert_equal 'hello', body
477485
else
478486
assert_equal 'deflate', res['content-encoding']
487+
assert_equal nil, res['content-length']
479488
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
480489
end
481490
end
@@ -523,9 +532,11 @@ def test_read_body_content_encoding_deflate_empty_body
523532

524533
if Net::HTTP::HAVE_ZLIB
525534
assert_equal nil, res['content-encoding']
535+
assert_equal '0', res['content-length']
526536
assert_equal '', body
527537
else
528538
assert_equal 'deflate', res['content-encoding']
539+
assert_equal '0', res['content-length']
529540
assert_equal '', body
530541
end
531542
end
@@ -549,9 +560,11 @@ def test_read_body_content_encoding_deflate_empty_body_no_length
549560

550561
if Net::HTTP::HAVE_ZLIB
551562
assert_equal nil, res['content-encoding']
563+
assert_equal nil, res['content-length']
552564
assert_equal '', body
553565
else
554566
assert_equal 'deflate', res['content-encoding']
567+
assert_equal nil, res['content-length']
555568
assert_equal '', body
556569
end
557570
end

0 commit comments

Comments
 (0)