Skip to content

Expose ClientWebSocket Upgrade Response Headers #28331

Description

@Yucked

Summary:

The API proposal comes after I was required to read response headers after making a WS handshake. Currently there is no way to see what response headers were returned after connecting to a WS server.

Rational & Usage:

For my use case, the current usage would be to figure out if a session was resumed by reading the response headers after re-establishing a WS connection.

Here is where the use case is properly described: https://github.com/Frederikam/Lavalink/blob/master/IMPLEMENTATION.md#resuming-lavalink-sessions

Further more, it can allow people to read any custom headers sent in response.

Proposed API:

Expose a property called Headers in ClientWebSocket/WebSocket class.

        public HttpResponseHeaders Headers
        {
            get
            {
                if (_headers == null)
                    throw new NullReferenceException("WebSocket connection hasn't started yet.");

                return _headers;
            }
        }

        private HttpResponseHeaders _headers;

And where headers are validated: https://github.com/dotnet/corefx/blob/master/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs#L181-L183

The _headers can be set once the required headers are validated.

                ValidateHeader(response.Headers, HttpKnownHeaderNames.Connection, "Upgrade");
                ValidateHeader(response.Headers, HttpKnownHeaderNames.Upgrade, "websocket");
                ValidateHeader(response.Headers, HttpKnownHeaderNames.SecWebSocketAccept, secKeyAndSecWebSocketAccept.Value);

                _headers = response.Headers;

Original Post:

Hi! Pardon me if I opened the issue in the wrong repo.

I was wondering if there was a way to get response headers after ws handshake was completed.
Something like WebSocketReceiveResult.Headers or similar.

Not sure if I'm looking in the right place. Any help will be appreciated.

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Net

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions