Skip to content

Commit 9d58f93

Browse files
BrianHawleymatzbot
authored andcommitted
[ruby/net-http] Net::HTTPResponse nil checking
Fix nil handling in read_body and stream_check. Fixes: #70 ruby/net-http@36f916ac18
1 parent d088b9f commit 9d58f93

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/net/http/response.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def read_body(dest = nil, &block)
366366
@body = nil
367367
end
368368
@read = true
369+
return if @body.nil?
369370

370371
case enc = @body_encoding
371372
when Encoding, false, nil
@@ -639,7 +640,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc:
639640
end
640641

641642
def stream_check
642-
raise IOError, 'attempt to read body out of block' if @socket.closed?
643+
raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed?
643644
end
644645

645646
def procdest(dest, block)

test/net/http/test_httpresponse.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,41 @@ def test_read_body_string
589589
assert_equal 'hello', body
590590
end
591591

592+
def test_read_body_receiving_no_body
593+
io = dummy_io(<<EOS)
594+
HTTP/1.1 204 OK
595+
Connection: close
596+
597+
EOS
598+
599+
res = Net::HTTPResponse.read_new(io)
600+
res.body_encoding = 'utf-8'
601+
602+
body = 'something to override'
603+
604+
res.reading_body io, true do
605+
body = res.read_body
606+
end
607+
608+
assert_equal nil, body
609+
assert_equal nil, res.body
610+
end
611+
612+
def test_read_body_outside_of_reading_body
613+
io = dummy_io(<<EOS)
614+
HTTP/1.1 200 OK
615+
Connection: close
616+
Content-Length: 0
617+
618+
EOS
619+
620+
res = Net::HTTPResponse.read_new(io)
621+
622+
assert_raise IOError do
623+
res.read_body
624+
end
625+
end
626+
592627
def test_uri_equals
593628
uri = URI 'http://example'
594629

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy