diff --git a/src/Http/Streams/HttpWebSocket.cs b/src/Http/Streams/HttpWebSocket.cs index d99b739..27438fa 100644 --- a/src/Http/Streams/HttpWebSocket.cs +++ b/src/Http/Streams/HttpWebSocket.cs @@ -118,6 +118,9 @@ internal HttpWebSocket ( HttpServerEngineWebSocket ctx, HttpRequest req, string? private async ValueTask ReceiveInternalAsync ( CancellationToken cancellation ) { ArraySegment buffer = new ArraySegment ( receiveBuffer ); ValueWebSocketReceiveResult result; + long maxMessageLength = request.baseServer.ServerConfiguration.MaximumContentLength <= 0 + ? Int32.MaxValue + : request.baseServer.ServerConfiguration.MaximumContentLength; if (IsClosed) return null; @@ -137,6 +140,11 @@ internal HttpWebSocket ( HttpServerEngineWebSocket ctx, HttpRequest req, string? do { result = await ctx.ReceiveAsync ( buffer, cancellation ); + if ((ms.Length + result.Count) > maxMessageLength) { + await ctx.CloseOutputAsync ( WebSocketCloseStatus.MessageTooBig, null, cancellation ); + await CloseAsync ( cancellation ); + return null; + } ms.Write ( buffer.Array!, buffer.Offset, result.Count ); } while (!result.EndOfMessage); diff --git a/src/Http/Streams/HttpWebSocketConnectionCollection.cs b/src/Http/Streams/HttpWebSocketConnectionCollection.cs index c168e92..fc3acce 100644 --- a/src/Http/Streams/HttpWebSocketConnectionCollection.cs +++ b/src/Http/Streams/HttpWebSocketConnectionCollection.cs @@ -37,6 +37,7 @@ internal void RegisterWebSocket ( HttpWebSocket src ) { // close another websockets with same identifier HttpWebSocket [] wsId = Find ( s => s == src._identifier ); foreach (HttpWebSocket ws in wsId) { + _ws.Remove ( ws ); ws.Dispose (); } _ws.Add ( src ); @@ -96,7 +97,10 @@ public HttpWebSocket [] All () { /// public void DropAll () { lock (_ws) { - foreach (HttpWebSocket es in _ws) + HttpWebSocket [] allWs = _ws.ToArray (); + _ws.Clear (); + + foreach (HttpWebSocket es in allWs) es.Dispose (); } }