Skip to content

Expose upgrade response headers to WebSocket client onUpgrade - #164

Open
awaisdev5765 wants to merge 1 commit into
vapor:mainfrom
awaisdev5765:expose-upgrade-response-headers
Open

awaisdev5765 wants to merge 1 commit into
vapor:mainfrom
awaisdev5765:expose-upgrade-response-headers

Conversation

@awaisdev5765

Copy link
Copy Markdown

Motivation

Fixes #151 — clients currently cannot read the headers a server sends in the 101 Switching Protocols upgrade response (e.g. an auth token minted server-side during the upgrade flow).

Changes

  • New connect overloads whose onUpgradeWithResponse closure receives the HTTPResponseHead alongside the WebSocket:
    • WebSocket.connect(to:scheme:...)
    • WebSocketClient.connect(...) (incl. proxy variants)
    • async/await variants
  • Existing onUpgrade APIs are unchanged — they forward through to the new variants and discard the head, so this is fully source-compatible.
  • 2 new tests covering the Future and async flows (testUpgradeResponseHeadersAreReceived, testUpgradeResponseHeadersAreReceivedAsync).

Result

Full suite passes: 27 tests, 0 failures. A client can now do:

WebSocket.connect(to: url, headers: ["Auth": token], on: group) { ws, responseHead in
    let serverToken = responseHead.headers.first(name: "X-Auth-Token")
    // ...
}

The NIOWebSocketClientUpgrader's upgradePipelineHandler already receives
the 101 Switching Protocols response head, but it was not accessible to
client code. This prevented clients from reading headers the server
sends during the upgrade - e.g. an authentication token minted
server-side in the shouldUpgrade/upgrade flow.

Add connect overloads whose onUpgrade closure also receives the
HTTPResponseHead:

- WebSocket.connect(to:scheme:...) with onUpgradeWithResponse
- WebSocketClient.connect(...) with onUpgradeWithResponse (including
  proxy variants)
- async/await variants

Existing onUpgrade APIs are unchanged (they forward through to the new
variants, discarding the head), so this is fully source-compatible.

Fixes vapor#151
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.

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

2 participants