Skip to content

Add ignore_eof access to HTTP and HTTPResponse #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ def initialize(address, port = nil)
@max_retries = 1
@debug_output = nil
@response_body_encoding = false
@ignore_eof = true

@proxy_from_env = false
@proxy_uri = nil
Expand Down Expand Up @@ -839,6 +840,10 @@ def continue_timeout=(sec)
# The default value is 2 seconds.
attr_accessor :keep_alive_timeout

# Whether to ignore EOF when reading response bodies with defined
# Content-Length headers. For backwards compatibility, the default is true.
attr_accessor :ignore_eof

# Returns true if the HTTP session has been started.
def started?
@started
Expand Down Expand Up @@ -1606,6 +1611,7 @@ def transport_request(req)
res = HTTPResponse.read_new(@socket)
res.decode_content = req.decode_content
res.body_encoding = @response_body_encoding
res.ignore_eof = @ignore_eof
end while res.kind_of?(HTTPInformation)

res.uri = req.uri
Expand Down
7 changes: 6 additions & 1 deletion lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def initialize(httpv, code, msg) #:nodoc: internal use only
@uri = nil
@decode_content = false
@body_encoding = false
@ignore_eof = true
end

# The HTTP version supported by the server.
Expand Down Expand Up @@ -119,6 +120,10 @@ def body_encoding=(value)
@body_encoding = value
end

# Whether to ignore EOF when reading bodies with a specified Content-Length
# header.
attr_accessor :ignore_eof

def inspect
"#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end
Expand Down Expand Up @@ -459,7 +464,7 @@ def read_body_0(dest)

clen = content_length()
if clen
@socket.read clen, dest, true # ignore EOF
@socket.read clen, dest, @ignore_eof
return
end
clen = range_length()
Expand Down
30 changes: 30 additions & 0 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1348,3 +1348,33 @@ def test_response_body_encoding_encoding_without_content_type
assert_equal(Encoding::UTF_8, res.body.encoding)
end
end

class TestNetHTTPPartialResponse < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'proxy_host' => nil,
'proxy_port' => nil,
}

include TestNetHTTPUtils

def test_partial_response
str = "0123456789"
@server.mount_proc('/') do |req, res|
res.status = 200
res['Content-Type'] = 'text/plain'

res.body = str
res['Content-Length'] = str.length + 1
end
@server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip }

http = Net::HTTP.new(config('host'), config('port'))
res = http.get('/')
assert_equal(str, res.body)

http = Net::HTTP.new(config('host'), config('port'))
http.ignore_eof = false
assert_raise(EOFError) {http.get('/')}
end
end