cmorten / opine

Minimalist web framework for Deno ported from ExpressJS.
https://github.com/cmorten/opine/blob/main/.github/API/api.md
MIT License
854 stars 43 forks source link

Request protocol for WebSocket upgrade request is always http #173

Closed lvl-mattwells closed 1 year ago

lvl-mattwells commented 1 year ago

Issue

Setup:

Details

Hello!

If I make a request using the browser's built-in WebSocket constructor like: new WebSocket("ws://localhost:5555/ws") I get req.protocol = http and req.proto = "". What I would expect is that I would get req.protocol = ws, and req.protocol = wss for secure connections.

For repro you could probably add a console.log(req.protocol) to your WebSockets example.

Any workarounds/suggestions would be appreciated! Thanks!

cmorten commented 1 year ago

Sorry taken a while to check notifications, this seems reasonable 🙂

cmorten commented 1 year ago

Actually, I don't believe this is what we want - the ws handshake is performed over HTTP so what you're seeing in Opine is that initial handshake request and the protocol is as expected.

Once the connection is established and the connection upgrade executed then the ws protocol is used, but by this point we're no longer in the remit of Opine's request middleware, instead performing event handling on the connections themselves.

It would be incorrect to indicate that the initial handshake is using a ws protocol when it isn't.

The opening handshake is intended to be compatible with HTTP-based server-side software and intermediaries, so that a single port can be used by both HTTP clients talking to that server and WebSocket clients talking to that server. To this end, the WebSocket client's handshake is an HTTP Upgrade request

See https://www.rfc-editor.org/rfc/rfc6455.