@@ -221,7 +221,12 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
221221 {
222222 if ( ! _firstDataOpCode . HasValue )
223223 {
224- throw new InvalidOperationException ( "A continuation can't be the first frame" ) ;
224+ if ( State == WebSocketState . Open )
225+ {
226+ await CloseOutputAsync ( WebSocketCloseStatus . ProtocolError , "Invalid continuation frame" , cancellationToken ) ;
227+ Abort ( ) ;
228+ }
229+ throw new InvalidOperationException ( "A continuation can't be the first frame" ) ; // TODO: WebSocketException
225230 }
226231 opCode = _firstDataOpCode . Value ;
227232 }
@@ -256,8 +261,12 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
256261 if ( messageType == WebSocketMessageType . Text
257262 && ! Utilities . TryValidateUtf8 ( new ArraySegment < byte > ( buffer . Array , buffer . Offset , bytesToCopy ) , _frameInProgress . Fin , _incomingUtf8MessageState ) )
258263 {
259- await CloseOutputAsync ( WebSocketCloseStatus . InvalidPayloadData , string . Empty , cancellationToken ) ;
260- throw new InvalidOperationException ( "An invalid UTF-8 payload was received." ) ;
264+ if ( State == WebSocketState . Open )
265+ {
266+ await CloseOutputAsync ( WebSocketCloseStatus . InvalidPayloadData , "Invalid UTF-8" , cancellationToken ) ;
267+ Abort ( ) ;
268+ }
269+ throw new InvalidOperationException ( "An invalid UTF-8 payload was received." ) ; // TODO: WebSocketException
261270 }
262271
263272 if ( bytesToCopy == _frameBytesRemaining )
@@ -292,9 +301,24 @@ private async Task ReadNextFrameAsync(CancellationToken cancellationToken)
292301 _receiveBufferBytes -= frameHeaderSize ;
293302 _frameBytesRemaining = _frameInProgress . DataLength ;
294303
304+ if ( _frameInProgress . AreReservedSet ( ) )
305+ {
306+ if ( State == WebSocketState . Open )
307+ {
308+ await CloseOutputAsync ( WebSocketCloseStatus . ProtocolError , "Unexpected reserved bits set" , cancellationToken ) ;
309+ Abort ( ) ;
310+ }
311+ throw new InvalidOperationException ( "Unexpected reserved bits are set." ) ; // TODO: WebSocketException
312+ }
313+
295314 if ( _unmaskInput != _frameInProgress . Masked )
296315 {
297- throw new InvalidOperationException ( "Unmasking settings out of sync with data." ) ;
316+ if ( State == WebSocketState . Open )
317+ {
318+ await CloseOutputAsync ( WebSocketCloseStatus . ProtocolError , "Incorrect masking" , cancellationToken ) ;
319+ Abort ( ) ;
320+ }
321+ throw new InvalidOperationException ( "Unmasking settings out of sync with data." ) ; // TODO: WebSocketException
298322 }
299323
300324 if ( _frameInProgress . OpCode == Constants . OpCodes . PingFrame || _frameInProgress . OpCode == Constants . OpCodes . PongFrame )
0 commit comments