@@ -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 ;
@@ -1668,33 +1669,50 @@ package body AWS.Server.HTTP_Utils is
16681669 -- if the WebSocket is not to be accepted. In this case
16691670 -- a forbidden message is sent back.
16701671
1671- WS : constant Net.WebSocket.Object'Class :=
1672- Net.WebSocket.Registry.Constructor
1673- (Status.URI (C_Stat))
1674- (Socket => Status.Socket (C_Stat),
1675- Request => C_Stat);
1672+ procedure Unchecked_Free is
1673+ new Ada.Unchecked_Deallocation
1674+ (Net.WebSocket.Object'Class,
1675+ Net.WebSocket.Object_Class);
1676+
1677+ use type Net.WebSocket.Object_Class;
1678+ WS : Net.WebSocket.Object_Class;
1679+ Registered : Boolean := False;
16761680 begin
1681+ WS := Net.WebSocket.Registry.Constructor
1682+ (Status.URI (C_Stat)) (C_Stat);
1683+
1684+ if WS /= null then
1685+ Net.WebSocket.Setup_Socket
1686+ (WS, Status.Socket (C_Stat), C_Stat);
1687+ end if ;
1688+
16771689 -- Register this new WebSocket
16781690
1679- if WS in Net.WebSocket.Handshake_Error.Object'Class then
1691+ if WS = null then
1692+ Send_WebSocket_Handshake_Error
1693+ (Messages.S412, " no route defined" );
1694+
1695+ elsif WS.all
1696+ in Net.WebSocket.Handshake_Error.Object'Class
1697+ then
16801698 declare
16811699 E : constant Net.WebSocket.Handshake_Error.Object :=
1682- Net.WebSocket.Handshake_Error.Object (WS);
1700+ Net.WebSocket.Handshake_Error.Object (WS. all );
16831701 begin
16841702 Send_WebSocket_Handshake_Error
16851703 (E.Status_Code, E.Reason_Phrase);
1704+ WS.Free;
1705+ Unchecked_Free (WS);
16861706 end ;
16871707
16881708 else
16891709 -- First try to register the WebSocket object
16901710
1691- declare
1692- use type Net.WebSocket.Object_Class;
1693- W : Net.WebSocket.Object_Class;
16941711 begin
1695- W := Net.WebSocket.Registry.Utils.Register (WS);
1712+ Net.WebSocket.Registry.Utils.Register (WS);
1713+ Registered := True;
16961714
1697- if W = null then
1715+ if WS = null then
16981716 Send_WebSocket_Handshake_Error
16991717 (Messages.S412,
17001718 " too many WebSocket registered" );
@@ -1706,7 +1724,7 @@ package body AWS.Server.HTTP_Utils is
17061724 Socket_Taken := True;
17071725 Will_Close := False;
17081726
1709- Net.WebSocket.Registry.Utils.Watch (W );
1727+ Net.WebSocket.Registry.Utils.Watch (WS );
17101728 end if ;
17111729 end ;
17121730 end if ;
@@ -1716,7 +1734,16 @@ package body AWS.Server.HTTP_Utils is
17161734 Send_WebSocket_Handshake_Error
17171735 (Messages.S403,
17181736 Exception_Message (E));
1719- WS.Shutdown;
1737+
1738+ if Registered then
1739+ -- Close will automatically free the memory for WS
1740+ -- itself, by looking up the pointer in the
1741+ -- registry.
1742+ Net.WebSocket.Registry.Close
1743+ (WS.all , " closed on error" );
1744+ else
1745+ Unchecked_Free (WS);
1746+ end if ;
17201747 end ;
17211748
17221749 exception
0 commit comments