Skip to content

Commit e770bb5

Browse files
tisbajeremyevans
authored andcommitted
handles client connection reset for keep-alive connections
1 parent d92e7b9 commit e770bb5

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/webrick/httpserver.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ def run(sock)
7979
timeout -= 0.5
8080
end
8181
raise HTTPStatus::EOFError if timeout <= 0 || @status != :Running
82-
raise HTTPStatus::EOFError if sock.eof?
82+
begin
83+
raise HTTPStatus::EOFError if sock.eof?
84+
rescue Errno::ECONNRESET
85+
raise HTTPStatus::EOFError
86+
end
87+
8388
req.parse(sock)
8489
res.request_method = req.request_method
8590
res.request_uri = req.request_uri

test/webrick/test_httpserver.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,35 @@ def test_accept_put_requests
562562
end
563563
end
564564
end
565+
566+
def test_client_connection_reset
567+
log_tester = lambda {|log, access_log|
568+
assert_empty log
569+
}
570+
571+
TestWEBrick.start_httpserver({}, log_tester) do |server, addr, port, log|
572+
server.mount_proc("/", lambda {|req, res| res.body = "hello!" })
573+
574+
http = Net::HTTP.new(addr, port)
575+
req = Net::HTTP::Get.new("/")
576+
req['Connection'] = 'Keep-Alive'
577+
578+
# Simulate a client, which makes a request, reads the response
579+
# and then resets the connection.
580+
http.request(req) do |res|
581+
res.body # read body to completion
582+
583+
# Fish the socket out of the Net::HTTP object, configure it to
584+
# linger on close, and then close it abruptly. This will reset
585+
# the socket, discard any pending data cause a RST to be sent.
586+
socket = http.instance_variable_get(:@socket).io
587+
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, [1,0].pack('ii'))
588+
socket.close
589+
end
590+
591+
# delay the server shutdown so that WEBrick::HTTPServer#run begins
592+
# a new iteration.
593+
sleep 0.1
594+
end
595+
end
565596
end

0 commit comments

Comments
 (0)