* lib/net/http.rb (connect): detect closed connection and reconnect
If the server closes a keep-alive http connection, the client socket reaches EOF. To avoid an EOFError, detect the closed connection and reconnect. Added test to ensure HTTP#post succeeds even if the keep-alive-connection has been closed by the server. by Kristian Hanekamp <kris.hanekamp@gmail.com> https://github.com/ruby/ruby/pull/1089 fix GH-1089 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52764 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6c0cf1848e
commit
d249be6233
11
ChangeLog
11
ChangeLog
@ -1,3 +1,14 @@
|
|||||||
|
Fri Nov 27 19:19:44 2015 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/net/http.rb (connect): detect closed connection and reconnect
|
||||||
|
If the server closes a keep-alive http connection, the client socket
|
||||||
|
reaches EOF. To avoid an EOFError, detect the closed connection and
|
||||||
|
reconnect.
|
||||||
|
Added test to ensure HTTP#post succeeds even if the
|
||||||
|
keep-alive-connection has been closed by the server.
|
||||||
|
by Kristian Hanekamp <kris.hanekamp@gmail.com>
|
||||||
|
https://github.com/ruby/ruby/pull/1089 fix GH-1089
|
||||||
|
|
||||||
Thu Nov 26 21:36:40 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Thu Nov 26 21:36:40 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* compile.c (iseq_peephole_optimize): enable tail call
|
* compile.c (iseq_peephole_optimize): enable tail call
|
||||||
|
@ -1473,10 +1473,16 @@ module Net #:nodoc:
|
|||||||
def begin_transport(req)
|
def begin_transport(req)
|
||||||
if @socket.closed?
|
if @socket.closed?
|
||||||
connect
|
connect
|
||||||
elsif @last_communicated && @last_communicated + @keep_alive_timeout < Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
elsif @last_communicated
|
||||||
|
if @last_communicated + @keep_alive_timeout < Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
D 'Conn close because of keep_alive_timeout'
|
D 'Conn close because of keep_alive_timeout'
|
||||||
@socket.close
|
@socket.close
|
||||||
connect
|
connect
|
||||||
|
elsif @socket.io.wait_readable(0) && @socket.eof?
|
||||||
|
D "Conn close because of EOF"
|
||||||
|
@socket.close
|
||||||
|
connect
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not req.response_body_permitted? and @close_on_empty_response
|
if not req.response_body_permitted? and @close_on_empty_response
|
||||||
|
@ -896,6 +896,23 @@ class TestNetHTTPKeepAlive < Test::Unit::TestCase
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_server_closed_connection_auto_reconnect
|
||||||
|
start {|http|
|
||||||
|
res = http.get('/')
|
||||||
|
http.keep_alive_timeout = 5
|
||||||
|
assert_kind_of Net::HTTPResponse, res
|
||||||
|
assert_kind_of String, res.body
|
||||||
|
sleep 1.5
|
||||||
|
assert_nothing_raised {
|
||||||
|
# Net::HTTP should detect the closed connection before attempting the
|
||||||
|
# request, since post requests cannot be retried.
|
||||||
|
res = http.post('/', 'query=foo', 'content-type' => 'application/x-www-form-urlencoded')
|
||||||
|
}
|
||||||
|
assert_kind_of Net::HTTPResponse, res
|
||||||
|
assert_kind_of String, res.body
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_keep_alive_get_auto_retry
|
def test_keep_alive_get_auto_retry
|
||||||
start {|http|
|
start {|http|
|
||||||
res = http.get('/')
|
res = http.get('/')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user