From 16daa80d5d06c7c9fa691b1281c0e4eb7fea3377 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Sat, 3 Aug 2019 09:50:03 +0200
Subject: [PATCH] Websocketd: correctly removes the client.
---
src/websocketd.cr | 51 ++++++++++++++++++++++-------------------------
1 file changed, 24 insertions(+), 27 deletions(-)
diff --git a/src/websocketd.cr b/src/websocketd.cr
index 74ff4f4..ef85584 100644
--- a/src/websocketd.cr
+++ b/src/websocketd.cr
@@ -72,39 +72,33 @@ class InstanceStorage
def remove_fd (fdclient : Int32)
# 1. closing both the client and the service
- fdservice = @switchtable[fdclient]
- # puts "1. fds to close: #{fdclient} and #{fdservice}"
+ fdservice = @switchtable[fdclient]?
tcpfdc = @socklist[fdclient]
- service = @connectionlist[fdservice]
# 2. closing the TCP connections
- # puts "2.1 closing fd #{fdclient}"
tcpfdc.close unless tcpfdc.closed?
- # puts "2.2 closing fd #{fdservice}"
- service.close
# 3. removing the client and the service fds from the loop check
- # puts "3.1. removing #{fdclient} from @service"
@service.remove_fd (fdclient)
- # puts "3.2. removing #{fdservice} from @service"
- @service.remove_fd (fdservice)
-
- # 4. removing the service IPC::Connection from connectionlist
- # puts "4. removing fdservice from @connectionlist"
- @connectionlist.select do |k, v|
- k != fdservice
- end
# 5. removing both the client and the service from the switchtable
- # puts "5. removing both fd from @switchable"
- @switchtable.select do |fdc, fds|
+ @switchtable = @switchtable.select do |fdc, fds|
fdc != fdclient && fds != fdclient
end
# 6. removing the client and the service from fdlist
- # puts "6. removing both fd from fdlist"
- @fdlist.select do |fd,v| fd != fdclient end
- @fdlist.select do |fd,v| fd != fdservice end
+ @fdlist = @fdlist.select do |fd,v| fd != fdclient end
+
+ unless fdservice.nil?
+ service = @connectionlist[fdservice]
+ service.close
+ @service.remove_fd (fdservice)
+ @connectionlist = @connectionlist.select do |k, v|
+ k != fdservice
+ end
+
+ @fdlist = @fdlist.select do |fd,v| fd != fdservice end
+ end
end
end
@@ -120,13 +114,11 @@ def websocket_client_connection(client, context : InstanceStorage)
# pp! request
if request.nil?
- puts "#{CRED}eEQUEST IS NIL#{CRESET}"
- return
+ raise "#REQUEST IS NIL"
end
if request.is_a? HTTP::Request::BadRequest
- puts "#{CRED}BAD REQUEST DAZE~#{CRESET}"
- return
+ raise "BAD REQUEST DAZE~"
end
# FIXME: check they actually wanted to upgrade to websocket
@@ -280,8 +272,13 @@ service.loop do |event|
# 1. accept new websocket clients
if server.fd == event.connection.fd
client = server.accept
- websocket_client_connection client, context
- puts "#{CBLUE}new client:#{CRESET} #{client.fd}"
+ begin
+ websocket_client_connection client, context
+ puts "#{CBLUE}new client:#{CRESET} #{client.fd}"
+ rescue e
+ puts "Exception: #{CRED}#{e}#{CRESET}"
+ client.close
+ end
next
end
@@ -296,7 +293,7 @@ service.loop do |event|
message = wsclient.read
if message.nil?
- puts "#{CBLUE}disconnection of client #{event.connection.fd}#{CRESET}"
+ puts "#{CBLUE}disconnection of client#{CRESET} #{event.connection.fd}"
closing_client activefd, context
next
end