diff options
| author | nagachika <nagachika@ruby-lang.org> | 2023-12-16 10:40:29 +0900 |
|---|---|---|
| committer | nagachika <nagachika@ruby-lang.org> | 2023-12-16 10:48:39 +0900 |
| commit | 06051311d828bead3922ac5766822000c011e5db (patch) | |
| tree | f8e673b2225a6bdcabcfa95722acabea58b4b96d | |
| parent | 359653658e604c075e3eca73e20b02d0d46bd0ef (diff) | |
merge revision(s) 9d58f9382893a71d8badad605879c0120915fbee:
[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(-)
| -rw-r--r-- | lib/net/http/response.rb | 3 | ||||
| -rw-r--r-- | test/net/http/test_httpresponse.rb | 35 | ||||
| -rw-r--r-- | version.h | 2 |
3 files changed, 38 insertions, 2 deletions
diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 192fa2c749..40de963868 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -366,6 +366,7 @@ class Net::HTTPResponse @body = nil end @read = true + return if @body.nil? case enc = @body_encoding when Encoding, false, nil @@ -639,7 +640,7 @@ class Net::HTTPResponse 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 394b4c5bfa..5c48a48e0c 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -589,6 +589,41 @@ EOS assert_equal 'hello', body end + def test_read_body_receiving_no_body + io = dummy_io(<<EOS) +HTTP/1.1 204 OK +Connection: close + +EOS + + res = Net::HTTPResponse.read_new(io) + res.body_encoding = 'utf-8' + + body = 'something to override' + + res.reading_body io, true do + body = res.read_body + end + + assert_equal nil, body + assert_equal nil, res.body + end + + def test_read_body_outside_of_reading_body + io = dummy_io(<<EOS) +HTTP/1.1 200 OK +Connection: close +Content-Length: 0 + +EOS + + res = Net::HTTPResponse.read_new(io) + + assert_raise IOError do + res.read_body + end + end + def test_uri_equals uri = URI 'http://example' @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 2 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 145 +#define RUBY_PATCHLEVEL 146 #include "ruby/version.h" #include "ruby/internal/abi.h" |
