Skip to content
Open
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
73 changes: 61 additions & 12 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2513,31 +2513,78 @@ def send_entity(path, data, initheader, dest, type, &block)

IDEMPOTENT_METHODS_ = %w/GET HEAD PUT DELETE OPTIONS TRACE QUERY/.freeze # :nodoc:

class RequestIO
attr_reader :write_error

def initialize(socket)
@socket = socket
@write_error = nil
end

def write(*strs)
@socket.write(*strs)
rescue Errno::ECONNRESET => error
@write_error = error
raise
end

alias << write

def io
@socket.io
end

def continue_timeout
@socket.continue_timeout
end

def readuntil(*args, **kwargs)
@socket.readuntil(*args, **kwargs)
end

def close_on_error
@socket.close if @write_error && !@socket.closed?
end
end
private_constant :RequestIO

def transport_request(req)
count = 0
begin
begin_transport req
write_error = nil
request_io = RequestIO.new(@socket)
res = catch(:response) {
begin
req.exec @socket, @curr_http_version, edit_path(req.path)
req.exec request_io, @curr_http_version, edit_path(req.path)
rescue Errno::EPIPE
# Failure when writing full request, but we can probably
# still read the received response.
rescue Errno::ECONNRESET => error
raise unless request_io.write_error.equal?(error)
write_error = error
end

informational_count = 0
begin
res = HTTPResponse.read_new(@socket)
res.decode_content = req.decode_content
res.body_encoding = @response_body_encoding
res.ignore_eof = @ignore_eof
if res.kind_of?(HTTPInformation)
informational_count += 1
raise HTTPBadResponse, 'too many informational responses' if
informational_count > HTTPResponse::MAX_INFORMATIONAL_RESPONSES
end
end while res.kind_of?(HTTPInformation)
informational_count = 0
begin
res = HTTPResponse.read_new(@socket, ignore_eof: !write_error)
if res.kind_of?(HTTPInformation)
informational_count += 1
raise HTTPBadResponse, 'too many informational responses' if
informational_count > HTTPResponse::MAX_INFORMATIONAL_RESPONSES
end
end while res.kind_of?(HTTPInformation)
rescue Net::ReadTimeout, IOError, EOFError, SystemCallError,
HTTPBadResponse, ArgumentError,
defined?(OpenSSL::SSL) ? OpenSSL::SSL::SSLError : IOError
raise write_error if write_error
raise
end

res.decode_content = req.decode_content
res.body_encoding = @response_body_encoding
res.ignore_eof = @ignore_eof
res.uri = req.uri

res
Expand All @@ -2564,6 +2611,8 @@ def transport_request(req)
debug "Conn close because of error #{exception}"
@socket.close if @socket
raise
ensure
request_io.close_on_error if request_io
end

end_transport req, res
Expand Down
8 changes: 4 additions & 4 deletions lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def exception_type # :nodoc: internal use only
self::EXCEPTION_TYPE
end

def read_new(sock) #:nodoc: internal use only
def read_new(sock, ignore_eof: true) #:nodoc: internal use only
httpv, code, msg = read_status_line(sock)
res = response_class(code).new(httpv, code, msg)
each_response_header(sock) do |k,v|
each_response_header(sock, ignore_eof: ignore_eof) do |k,v|
res.add_field k, v
end
res
Expand Down Expand Up @@ -181,11 +181,11 @@ def response_class(code)
Net::HTTPUnknownResponse
end

def each_response_header(sock)
def each_response_header(sock, ignore_eof: true)
key = value = nil
remaining = MAX_RESPONSE_HEADER_LENGTH
while true
line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH, true)
line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH, ignore_eof)
remaining -= line.bytesize
raise Net::HTTPBadResponse, 'response header too large' if remaining < 0
line = line.chomp
Expand Down
Loading