@@ -37,6 +37,7 @@ with Ada.Strings.Fixed;
3737with Ada.Strings.Maps ;
3838with Ada.Strings.Unbounded ;
3939with Ada.Text_IO ;
40+ with Ada.Unchecked_Deallocation ;
4041
4142with GNAT.MD5 ;
4243with GNAT.OS_Lib ;
@@ -1667,33 +1668,50 @@ package body AWS.Server.HTTP_Utils is
16671668 -- if the WebSocket is not to be accepted. In this case
16681669 -- a forbidden message is sent back.
16691670
1670- WS : constant Net.WebSocket.Object'Class :=
1671- Net.WebSocket.Registry.Constructor
1672- (Status.URI (C_Stat))
1673- (Socket => Status.Socket (C_Stat),
1674- Request => C_Stat);
1671+ procedure Unchecked_Free is
1672+ new Ada.Unchecked_Deallocation
1673+ (Net.WebSocket.Object'Class,
1674+ Net.WebSocket.Object_Class);
1675+
1676+ use type Net.WebSocket.Object_Class;
1677+ WS : Net.WebSocket.Object_Class;
1678+ Registered : Boolean := False;
16751679 begin
1680+ WS := Net.WebSocket.Registry.Constructor
1681+ (Status.URI (C_Stat)) (C_Stat);
1682+
1683+ if WS /= null then
1684+ Net.WebSocket.Setup_Socket
1685+ (WS, Status.Socket (C_Stat), C_Stat);
1686+ end if ;
1687+
16761688 -- Register this new WebSocket
16771689
1678- if WS in Net.WebSocket.Handshake_Error.Object'Class then
1690+ if WS = null then
1691+ Send_WebSocket_Handshake_Error
1692+ (Messages.S412, " no route defined" );
1693+
1694+ elsif WS.all
1695+ in Net.WebSocket.Handshake_Error.Object'Class
1696+ then
16791697 declare
16801698 E : constant Net.WebSocket.Handshake_Error.Object :=
1681- Net.WebSocket.Handshake_Error.Object (WS);
1699+ Net.WebSocket.Handshake_Error.Object (WS. all );
16821700 begin
16831701 Send_WebSocket_Handshake_Error
16841702 (E.Status_Code, E.Reason_Phrase);
1703+ WS.Free;
1704+ Unchecked_Free (WS);
16851705 end ;
16861706
16871707 else
16881708 -- First try to register the WebSocket object
16891709
1690- declare
1691- use type Net.WebSocket.Object_Class;
1692- W : Net.WebSocket.Object_Class;
16931710 begin
1694- W := Net.WebSocket.Registry.Utils.Register (WS);
1711+ Net.WebSocket.Registry.Utils.Register (WS);
1712+ Registered := True;
16951713
1696- if W = null then
1714+ if WS = null then
16971715 Send_WebSocket_Handshake_Error
16981716 (Messages.S412,
16991717 " too many WebSocket registered" );
@@ -1705,7 +1723,7 @@ package body AWS.Server.HTTP_Utils is
17051723 Socket_Taken := True;
17061724 Will_Close := False;
17071725
1708- Net.WebSocket.Registry.Utils.Watch (W );
1726+ Net.WebSocket.Registry.Utils.Watch (WS );
17091727 end if ;
17101728 end ;
17111729 end if ;
@@ -1715,7 +1733,16 @@ package body AWS.Server.HTTP_Utils is
17151733 Send_WebSocket_Handshake_Error
17161734 (Messages.S403,
17171735 Exception_Message (E));
1718- WS.Shutdown;
1736+
1737+ if Registered then
1738+ -- Close will automatically free the memory for WS
1739+ -- itself, by looking up the pointer in the
1740+ -- registry.
1741+ Net.WebSocket.Registry.Close
1742+ (WS.all , " closed on error" );
1743+ else
1744+ Unchecked_Free (WS);
1745+ end if ;
17191746 end ;
17201747
17211748 exception
0 commit comments