From 9d58f9382893a71d8badad605879c0120915fbee Mon Sep 17 00:00:00 2001 From: Brian Hawley Date: Thu, 22 Sep 2022 16:08:59 -0700 Subject: [PATCH] [ruby/net-http] Net::HTTPResponse nil checking Fix nil handling in read_body and stream_check. Fixes: #70 https://github.com/ruby/net-http/commit/36f916ac18 --- lib/net/http/response.rb | 3 ++- test/net/http/test_httpresponse.rb | 35 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 192fa2c749c860..40de96386804f3 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -366,6 +366,7 @@ def read_body(dest = nil, &block) @body = nil end @read = true + return if @body.nil? case enc = @body_encoding when Encoding, false, nil @@ -639,7 +640,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc: end def stream_check - raise IOError, 'attempt to read body out of block' if @socket.closed? + raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed? end def procdest(dest, block) diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 99a946748c3a3e..01281063cdade0 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -589,6 +589,41 @@ def test_read_body_string assert_equal 'hello', body end + def test_read_body_receiving_no_body + io = dummy_io(<