@@ -221,7 +221,12 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
221
221
{
222
222
if ( ! _firstDataOpCode . HasValue )
223
223
{
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
225
230
}
226
231
opCode = _firstDataOpCode . Value ;
227
232
}
@@ -256,8 +261,12 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
256
261
if ( messageType == WebSocketMessageType . Text
257
262
&& ! Utilities . TryValidateUtf8 ( new ArraySegment < byte > ( buffer . Array , buffer . Offset , bytesToCopy ) , _frameInProgress . Fin , _incomingUtf8MessageState ) )
258
263
{
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
261
270
}
262
271
263
272
if ( bytesToCopy == _frameBytesRemaining )
@@ -292,9 +301,24 @@ private async Task ReadNextFrameAsync(CancellationToken cancellationToken)
292
301
_receiveBufferBytes -= frameHeaderSize ;
293
302
_frameBytesRemaining = _frameInProgress . DataLength ;
294
303
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
+
295
314
if ( _unmaskInput != _frameInProgress . Masked )
296
315
{
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
298
322
}
299
323
300
324
if ( _frameInProgress . OpCode == Constants . OpCodes . PingFrame || _frameInProgress . OpCode == Constants . OpCodes . PongFrame )
0 commit comments