Skip to content
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

Fix: Handle client connection reset for keep-alive connections #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion lib/webrick/httpserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def run(sock)
timeout -= 0.5
end
raise HTTPStatus::EOFError if timeout <= 0 || @status != :Running
raise HTTPStatus::EOFError if sock.eof?
begin
raise HTTPStatus::EOFError if sock.eof?
rescue Errno::ECONNRESET
raise HTTPStatus::EOFError
end

req.parse(sock)
res.request_method = req.request_method
res.request_uri = req.request_uri
Expand Down
31 changes: 31 additions & 0 deletions test/webrick/test_httpserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,35 @@ def test_accept_put_requests
end
end
end

def test_client_connection_reset
log_tester = lambda {|log, access_log|
assert_empty log
}

TestWEBrick.start_httpserver({}, log_tester) do |server, addr, port, log|
server.mount_proc("/", lambda {|req, res| res.body = "hello!" })

http = Net::HTTP.new(addr, port)
req = Net::HTTP::Get.new("/")
req['Connection'] = 'Keep-Alive'

# Simulate a client, which makes a request, reads the response
# and then resets the connection.
http.request(req) do |res|
res.body # read body to completion

# Fish the socket out of the Net::HTTP object, configure it to
# linger on close, and then close it abruptly. This will reset
# the socket, discard any pending data cause a RST to be sent.
socket = http.instance_variable_get(:@socket).io
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, [1,0].pack('ii'))
socket.close
end

# delay the server shutdown so that WEBrick::HTTPServer#run begins
# a new iteration.
sleep 0.1
end
end
end