Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ws] Protocol handling #31

Open
1 task
tobiasdiez opened this issue Mar 23, 2024 · 2 comments
Open
1 task

[ws] Protocol handling #31

tobiasdiez opened this issue Mar 23, 2024 · 2 comments

Comments

@tobiasdiez
Copy link

tobiasdiez commented Mar 23, 2024

Describe the feature

I couldn't find a way to get sec-websocket-protocol, and based on this value abort the connection or not. The upgrade hook looks promising, but it seems only possible to return headers there and not completely abort the upgrade. It would also be nice if one could later get the protocol from Peer.

(Background: graphql-ws supports only the graphql-ws subprotocol and normally rejects the (deprecated) subscriptions-transport-ws subprotocol. Such a check is currently hard to implement.)

Additional information

  • Would you be willing to help implement this feature?
@pi0
Copy link
Member

pi0 commented Mar 25, 2024

Do you mind to make a minimal reproduction? 🙏🏼 (this is how i can help to move it forward as soon as possible)

@tobiasdiez
Copy link
Author

tobiasdiez commented Mar 26, 2024

Since it's more a feature request than a bug report, I don't have a reproduction. But what I would like to do is something of the form:

const hooks = defineHooks({
    message(peer, message) {
      if (peer.websocketProtocol == 'graphql-ws')
           handleGraphqlWs(peer, message)
      else
           handleOldWs(peer, message)
    },
    upgrade(req) {
      if (!req.websocketProtocol in ['graphql-ws', 'subscription-ws'])
           return new Response("Upgrade failed due to unsupported protocol", { status: 500 });
    },
})

Another idea is perhaps to add all the sec-websocket headers as a second argument to upgrade(req, { extensions, key, protocol, version })

Edit: Looking at point 4 of the specs it's perhaps better to return the subprotocol directly. I.e. something like

upgrade(req) {
      if ('graphql-ws' in req.websocketProtocols)
           return { protocol: 'graphql-ws' } // prefer graphql-ws if given
      else if ('subscription-ws' in req.websocketProtocols)
           return { protocol: 'subscription-ws' }
      else 
           return { protocol: null } // This will fail the handshake
    },

and crossws then maps this correctly to Sec-WebSocket-Protocol. In particular, if null is returned for the protocol, then no Sec-WebSocket-Protocol should be added in the response, which then aborts the upgrade handshake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants