vapor / websocket-kit

WebSocket client library built on SwiftNIO
https://docs.vapor.codes/4.0/advanced/websockets/
MIT License
281 stars 80 forks source link

Add access to Request or headers in `onUpgrade` closure on client-side #151

Open edonv opened 5 months ago

edonv commented 5 months ago

Is your feature request related to a problem? Please describe. I'm using WebSocketKit on both the Vapor server-side, and from a client-side. For my own custom authentication system, I'd like to send something in the initial request headers from the client (already possible), then do some stuff with it to generate an authentication token on the server-side (already possible, server-side shouldUpgrade closure), and then send the token back in the response headers (already possible, server-side shouldUpgrade closure). The problem is the client being able to receive that. As of now, WebSocketKit doesn't give the client-side connecting functions access to the Request or headers in the upgrading Request from the server.

I found where it's accessed in the package code, but it's not made accessible to the client: https://github.com/vapor/websocket-kit/blob/4232d34efa49f633ba61afde365d3896fc7f8740/Sources/WebSocketKit/WebSocketClient.swift#L137-L143

I even did a test of my workflow and put a breakpoint there to inspect req, and it had the header I was looking for.

Describe the solution you'd like Is it possible to add a req or headers parameter to the onUpgrade closure? Or a way to access it from theWebSocket` instance, like a new property on it?

Describe alternatives you've considered Alternatively, I can set up my workflow to be sent via standard message as soon as the connection is upgraded (which is probably what I'll do until this is resolved), but I love for this to be integrated into the connection setup.

Thanks in advance for any help with this!

lmcd commented 4 months ago

I need access to this information too