Skip to content

[bug] Websocket subprotocol is not chosen on client preferance #822

@KSDaemon

Description

@KSDaemon

Describe the bug

From the Websocket RFC6455:

For client side:

|Sec-WebSocket-Protocol| header field, with a list of values indicating which protocols the client would like to speak, ordered by preference.

And for server side:

Either a single value representing the subprotocol the server is ready to use or null. The value chosen MUST be derived from the client's handshake, specifically by selecting one of the values from the |Sec-WebSocket-Protocol| field that the server is willing to use for this connection (if any).

So if the client provides a few options for subprotocol. The server should choose the first one it supports.

Right now, if client provides a few options, lib choose the first one it supports (and not the first one from the client).

e.g. So if the client sends Sec-WebSocket-Protocol: wamp.2.cbor, wamp.2,json and server supports wamp.2,json, wamp.2.cbor then wamp.2,json will be chosen but not wamp.2.cbor as it should be.

A clear and concise description of what the bug is.

Lib version: all :)

Code Snippets
The problem is in server.go: selectSubprotocol func:

		clientProtocols := Subprotocols(r)
		for _, serverProtocol := range u.Subprotocols {
			for _, clientProtocol := range clientProtocols {
				if clientProtocol == serverProtocol {
					return clientProtocol
				}
			}
		}

should be changed to:

        clientProtocols := Subprotocols(r)
        for _, clientProtocol := range clientProtocols {
            for _, serverProtocol := range u.Subprotocols {
                if clientProtocol == serverProtocol {
                    return clientProtocol
                }
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions