-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Background and Motivation
The method AcceptWebSocketAsync on the HttpListenerContext class currently takes a string subProtocol. When using #nullable enable, the compiler warns about passing null to this parameter, but doing so is totally valid behaviour and indicates no sub-protocol should be used.
Note that passing an empty string (string.Empty) is not valid and causes an exception to be thrown during validation.
The code eventually gets in to HttpWebSocket::ValidateOptions() which has an explicit check for subProtocol begin null, but the parameter is also not marked as nullable there.
Note that currently to ignore the compiler warning you can call the method as follwos:
await ctx.AcceptWebSocketAsync(null!);Proposed API
UPDATE by @antonfirsov: included all 4 overloads.
public class HttpListenerContext
{
- public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol);
+ public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string? subProtocol);
- public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval);
+ public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string? subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval);
- public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment<byte> internalBuffer);
+ public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string? subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment<byte> internalBuffer);
- public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol, TimeSpan keepAliveInterval);
+ public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string? subProtocol, TimeSpan keepAliveInterval);
}Note: There are several overloads of this method, and it ends up calling these overloads and other methods to validate options - these will also need alterations.
Usage Examples
WebSocketContext webSocketCtx = await httpContext.AcceptWebSocketAsync(null);Alternative Designs
N/A
Risks
N/A