Skip to content

feat: add WebSocket proxy support#26

Open
nTele-Dev wants to merge 1 commit intopingooio:mainfrom
nTele-Dev:feat/websocket-proxy
Open

feat: add WebSocket proxy support#26
nTele-Dev wants to merge 1 commit intopingooio:mainfrom
nTele-Dev:feat/websocket-proxy

Conversation

@nTele-Dev
Copy link
Copy Markdown

Summary

Adds WebSocket upgrade proxying to the HTTP proxy service. Previously, WebSocket connections failed because Connection and Upgrade headers were stripped as hop-by-hop headers before forwarding.

Changes

  • Detect WebSocket upgrade requests (Upgrade: websocket) before header stripping
  • Preserve Connection and Upgrade headers for WebSocket requests
  • Force HTTP/1.1 for WebSocket (HTTP/2 does not support the Upgrade mechanism)
  • On 101 Switching Protocols from upstream, establish bidirectional byte relay using tokio::io::copy_bidirectional
  • Forward Sec-WebSocket-Accept and other upgrade headers from upstream to client

How it works

The HTTP listener already uses hyper's serve_connection_with_upgrades(), so the server-side upgrade plumbing is in place. This PR adds the client-side:

  1. hyper::upgrade::on(&mut req) prepares the client upgrade future
  2. Request is forwarded to upstream with Connection/Upgrade headers intact
  3. On 101 response, hyper::upgrade::on(&mut res) prepares the upstream upgrade future
  4. A background task relays bytes bidirectionally between client and upstream

Testing

Tested against a Go reverse proxy (rpc-gateway-go) that itself proxies WebSocket connections to a VoIP call event stream. Verified:

  • WebSocket upgrade handshake completes through Pingoo
  • Bidirectional message flow works (JSON events from server, subscribe commands from client)
  • Clean shutdown when either side closes

Fixes #23

WebSocket upgrade requests (Upgrade: websocket) are now proxied through
to upstream backends instead of being rejected.

Previously, the Connection and Upgrade headers were unconditionally
stripped as hop-by-hop headers before forwarding. While this is correct
per RFC 7230, RFC 6455 Section 4.1 requires these headers for the
WebSocket handshake.

Changes:
- Detect WebSocket upgrade requests before header stripping
- Preserve Connection and Upgrade headers for WS requests
- Force HTTP/1.1 for WS requests (HTTP/2 does not support Upgrade)
- On 101 from upstream, set up bidirectional byte relay via
  tokio::io::copy_bidirectional
- Forward Sec-WebSocket-Accept and other upgrade headers from upstream

Works because the HTTP listener already uses hyper serve_connection_with_upgrades.

Fixes pingooio#23
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

Successfully merging this pull request may close these issues.

Question: Does HTTP proxy support WebSocket connections?

2 participants