Skip to content

Commit e686ee5

Browse files
authored
Merge pull request #371 from orien/no-retry-consumed-request-body
Don't retry a request whose body comes from an `IO`
2 parents f65a8b4 + 8bd80bf commit e686ee5

3 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎lib/net/http.rb‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,11 @@ def ipaddr=(addr)
13891389
# Timeout::Error.
13901390
# The initial value is 1.
13911391
#
1392+
# A request whose body comes from an IO, set through
1393+
# Net::HTTPGenericRequest#body_stream= or as a value given to
1394+
# Net::HTTPHeader#set_form, is not retried: the first attempt may have read
1395+
# from the IO, so a retry would send only what is left.
1396+
#
13921397
# Argument +retries+ must be a non-negative numeric value:
13931398
#
13941399
# http = Net::HTTP.new(hostname)
@@ -2557,7 +2562,7 @@ def transport_request(req)
25572562
# avoid a dependency on OpenSSL
25582563
defined?(OpenSSL::SSL) ? OpenSSL::SSL::SSLError : IOError,
25592564
Timeout::Error => exception
2560-
if count < max_retries && IDEMPOTENT_METHODS_.include?(req.method)
2565+
if count < max_retries && IDEMPOTENT_METHODS_.include?(req.method) && req.body_replayable?
25612566
count += 1
25622567
@socket.close if @socket
25632568
debug "Conn close because of error #{exception}, and retry"

‎lib/net/http/generic_request.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ def body_stream=(input)
205205
input
206206
end
207207

208+
# False when the body comes from an IO: an earlier attempt may have read from
209+
# it, so sending the request again would send only what is left.
210+
def body_replayable? # :nodoc: internal use only
211+
return false if @body_stream
212+
return true unless @body_data
213+
@body_data.each { |_key, value| return false if value.respond_to?(:read) }
214+
true
215+
end
216+
208217
def set_body_internal(str) #:nodoc: internal use only
209218
raise ArgumentError, "both of body argument and HTTPRequest#body set" if str and (@body or @body_stream)
210219
self.body = str if str

‎test/net/http/test_http.rb‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,89 @@ def test_http_retry_failed_with_block
15021502
@log_tester = nil
15031503
end
15041504

1505+
class RecordingMockSocket < MockSocket
1506+
def written
1507+
@written ||= +""
1508+
end
1509+
def continue_timeout
1510+
end
1511+
def write(*strs)
1512+
strs.each { |str| written << str }
1513+
strs.sum(&:bytesize)
1514+
end
1515+
alias << write
1516+
end
1517+
1518+
def with_mock_socket(success_after: nil)
1519+
start {|http|
1520+
socket = RecordingMockSocket.new(success_after: success_after)
1521+
http.instance_variable_get(:@socket).close
1522+
http.instance_variable_set(:@socket, socket)
1523+
http.max_retries = 10
1524+
yield http, socket
1525+
}
1526+
end
1527+
1528+
def test_http_retry_with_string_body
1529+
with_mock_socket {|http, socket|
1530+
req = Net::HTTP::Put.new("/")
1531+
req.body = "data"
1532+
assert_raise(Errno::ECONNRESET) { http.request(req) }
1533+
assert_equal 11, socket.count
1534+
}
1535+
end
1536+
1537+
def test_http_retry_with_multipart_form
1538+
with_mock_socket {|http, socket|
1539+
req = Net::HTTP::Put.new("/")
1540+
req.set_form([["a", "b"]], "multipart/form-data")
1541+
assert_raise(Errno::ECONNRESET) { http.request(req) }
1542+
assert_equal 11, socket.count
1543+
}
1544+
end
1545+
1546+
def test_http_retry_with_urlencoded_form
1547+
with_mock_socket {|http, socket|
1548+
req = Net::HTTP::Put.new("/")
1549+
req.set_form([["a", "b"]])
1550+
assert_raise(Errno::ECONNRESET) { http.request(req) }
1551+
assert_equal 11, socket.count
1552+
}
1553+
end
1554+
1555+
def test_http_no_retry_with_body_stream
1556+
with_mock_socket {|http, socket|
1557+
req = Net::HTTP::Put.new("/")
1558+
req["Transfer-Encoding"] = "chunked"
1559+
req.body_stream = StringIO.new("data")
1560+
assert_raise(Errno::ECONNRESET) { http.request(req) }
1561+
assert_equal 1, socket.count
1562+
assert_equal 1, socket.written.scan("PUT / HTTP/1.1\r\n").size
1563+
}
1564+
end
1565+
1566+
def test_http_no_retry_with_body_stream_when_retry_would_succeed
1567+
with_mock_socket(success_after: 2) {|http, socket|
1568+
req = Net::HTTP::Put.new("/")
1569+
req["Transfer-Encoding"] = "chunked"
1570+
req.body_stream = StringIO.new("data")
1571+
assert_raise(Errno::ECONNRESET) { http.request(req) }
1572+
assert_equal 1, socket.count
1573+
assert_equal 1, socket.written.scan("PUT / HTTP/1.1\r\n").size
1574+
}
1575+
end
1576+
1577+
def test_http_no_retry_with_multipart_io
1578+
with_mock_socket {|http, socket|
1579+
req = Net::HTTP::Put.new("/")
1580+
req["Transfer-Encoding"] = "chunked"
1581+
req.set_form([["file", StringIO.new("data"), {filename: "f.txt"}]], "multipart/form-data")
1582+
assert_raise(Errno::ECONNRESET) { http.request(req) }
1583+
assert_equal 1, socket.count
1584+
assert_equal 1, socket.written.scan("PUT / HTTP/1.1\r\n").size
1585+
}
1586+
end
1587+
15051588
def test_keep_alive_server_close
15061589
def @server.run(sock)
15071590
sock.close

0 commit comments

Comments
 (0)