fastify-websocket icon indicating copy to clipboard operation
fastify-websocket copied to clipboard

Setting custom headers on initial WebSocket HTTP Upgrade response

Open bahamut657 opened this issue 2 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I'm building an HTTP reverse proxy using fastify. In order to support other 3rd parts WebSocket connections, proxied by my software, I need a way to rewrite headers obtained in UPGRADE response from the replicated WS connection. Is there a way to write some custom header in the initial upgrade request?

Example

In my case I have to replicate this headers:

Connection: upgrade Content-Type: application/octet-stream Date: Tue, 24 May 2022 16:19:40 GMT Sec-WebSocket-Accept: MGhCOJFY8ayJUf3cJ+BcSgTwOOg= Sec-WebSocket-Extensions: permessage-deflate Sec-WebSocket-Protocol: tty Server: Python/3.9 aiohttp/3.8.1 Upgrade: websocket

But from the reverse proxy I obtain this response:

Connection: Upgrade Sec-WebSocket-Accept: 0hWXq3owSgRUjfyWd9dJpW6+lm0= Sec-WebSocket-Protocol: tty Upgrade: websocket

I have some missing fields and one to rewrite

bahamut657 avatar May 24 '22 16:05 bahamut657

I don't think we support this right now and I took a quick look and it's not immediately obvious to me how we might add support. Its a bit tricky to write HTTP headers at the right moment before the connection becomes a bidirectional socket, but, it is definitely technically possible. Want to try a PR?

airhorns avatar May 24 '22 16:05 airhorns

Sure, I can try a PR. Could you suggest me the right starting point (files/mechanisms) to look at?

bahamut657 avatar May 24 '22 17:05 bahamut657