Skip to content

Commit 36f916a

Browse files
committed
Net::HTTPResponse nil checking
Fix nil handling in read_body and stream_check. Fixes: #70
1 parent 196f3d7 commit 36f916a

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
@@ -231,6 +231,7 @@ def read_body(dest = nil, &block)
231231
@body = nil
232232
end
233233
@read = true
234+
return if @body.nil?
234235

235236
case enc = @body_encoding
236237
when Encoding, false, nil
@@ -504,7 +505,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc:
504505
end
505506

506507
def stream_check
507-
raise IOError, 'attempt to read body out of block' if @socket.closed?
508+
raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed?
508509
end
509510

510511
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