Skip to content

Commit

Permalink
[browser][websocket] Fix for Close Description validation (#45536)
Browse files Browse the repository at this point in the history
* [browser][websocket] Fix for Close Description validation

- Issue #45531
- The description validation exception was not being percolated through to the unit test

* Remove test code

* Address review comments.  Wrap in a try/catch that will catch that exception and store it into a task to be returned.

* Fix merge conflict

* Remove active issue

Co-authored-by: Larry Ewing <lewing@microsoft.com>
  • Loading branch information
kjpou1 and lewing authored Jan 5, 2021
1 parent 65bcfd0 commit 867ba80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,39 @@ public override void Abort()
Dispose();
}

public override async Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
_writeBuffer = null;
ThrowIfNotConnected();
await CloseAsyncCore(closeStatus, statusDescription, cancellationToken).ConfigureAwait(continueOnCapturedContext: true);
}

private async Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent);

WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription);

_tcsClose = new TaskCompletionSource();
_innerWebSocketCloseStatus = closeStatus;
_innerWebSocketCloseStatusDescription = statusDescription;
_innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription);
await _tcsClose.Task.ConfigureAwait(continueOnCapturedContext: true);
try
{
ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent);
}
catch (Exception exc)
{
return Task.FromException(exc);
}

return CloseAsyncCore(closeStatus, statusDescription, cancellationToken);
}

private Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
try
{
_tcsClose = new TaskCompletionSource();
_innerWebSocketCloseStatus = closeStatus;
_innerWebSocketCloseStatusDescription = statusDescription;
_innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription);
return _tcsClose.Task;
}
catch (Exception exc)
{
return Task.FromException(exc);
}
}

public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) => throw new PlatformNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public async Task CloseAsync_CloseDescriptionIsMaxLength_Success(Uri server)

[OuterLoop("Uses external server")]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/45531", TestPlatforms.Browser)]
public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentException(Uri server)
{
string closeDescription = new string('C', CloseDescriptionMaxLength + 1);
Expand Down

0 comments on commit 867ba80

Please sign in to comment.