Skip to content

fix: preserve generated server WebSocket handshake metadata - #1608

Open
He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:fix/ws-negotiated-response-headers
Open

He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:fix/ws-negotiated-response-headers

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Motivation

The server must keep the framework-generated WebSocket Accept metadata without taking control of headers that applications select.

Modification

Track only the generated Sec-WebSocket-Accept metadata in an immutable internal marker, not in Flow attributes. Leave application-selected protocol, extensions, and other ordinary headers alone. Keep the existing echo test unchanged, add separate response-renderer assertions, and document the behavior in the release notes.

Result

The framework maintains the generated Accept metadata while applications retain control of ordinary negotiation and response headers. Public method signatures are unchanged.

Tests

JDK 17: all five focused tests passed on each of Scala 2.13.18 and Scala 3.3.9 (5/5 each), using the exact changed source with cached compiled support. The reviewed JVM method descriptors still match. This is not a full CI or MiMa run.

References

None - Informed by Netty 4.2's outbound response construction test and RFC 6455 §4.2.2 / §11.3.2–§11.3.4. Uses Pekko HTTP's own upgrade flow; no Netty code copied.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerns:

  • When no subprotocol was negotiated, a Sec-WebSocket-Protocol/Sec-WebSocket-Extensions header the application added itself is now silently removed at render time, and one the app deliberately removed is put back. Code that calls handleMessages(flow) and then adds Sec-WebSocket-Protocol by hand would lose it, and browsers fail the connection in that case. Is overriding the application wanted, or would removing duplicate Sec-WebSocket-Accept headers be enough? Either way it needs a release note.
  • Passing the generated headers via the handler Flow's stream Attributes feels hacky; a field on the internal header class would be more direct.
  • The existing "for echoing user handler" test in WebSocketServerSpec is rewritten from an exact wire assertion to a byte loop checking three fields, dropping Upgrade/Connection checks. Could the original stay and the new scenario be a separate test?
  • The WebSocketDirectivesSpec tests call WebSocketUpgradeResponseHeaders.forRendering directly rather than going through the renderer.

He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 26, 2026
Motivation: The server generates the acceptance value for a WebSocket
upgrade. Application response decorations should not break that
required handshake field or override application-managed headers.

Modification: Store the generated acceptance in the internal WebSocket
switch header, not Flow attributes. The HTTP/1 renderer emits that
field once for the WebSocket upgrade and preserves all other headers.
Add a behavior release note and direct renderer coverage.

Result: Application-selected subprotocols and extensions are unchanged.
Other protocol upgrades use the existing rendering path. No public
Java or Scala API signatures change.

Tests: JDK 17 on Scala 2.13.18 and 3.3.9: five focused positive
renderer and WebSocket checks each. The new restoration assertion
fails on main as expected. Scoped formatting and JVM signatures
of the existing internal marker and companion were checked.

References: RFC 6455 sections 4.2.2 and 11.3.3; PJ's review on apache#1608.
@He-Pin
He-Pin force-pushed the fix/ws-negotiated-response-headers branch from 0cffc8e to 71c5685 Compare November 26, 2026 12:24
@He-Pin
He-Pin requested a review from pjfanning November 26, 2026 12:25

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is much narrower now: only Sec-WebSocket-Accept is restored, application changes to the other headers are left alone, the existing echo test is untouched and the new tests go through the renderer. One thing left:

UpgradeToOtherProtocolResponseHeader changes from a final case class to a sealed case class so that WebSocketUpgradeResponseHeader can extend it. Extending a case class brings the usual problems: equals/hashCode only look at handler, so the subclass compares equal to a plain UpgradeToOtherProtocolResponseHeader with the same handler, and copy returns the base class and loses accept. Nothing depends on that today, but an optional field on the existing class (accept: Option[...] or OptionVal), or a common trait with two final classes, would avoid it.

Motivation:
Application response decoration can remove the server-generated WebSocket
acceptance header. Preserve it without overriding application negotiations.

Modification:
Retain the generated acceptance in a final WebSocket marker. Share the
existing completion-close handling with the final generic upgrade marker
without deriving one case class from another. Restrict acceptance handling
to server WebSocket switches. Add focused renderer tests and a release note.

Result:
The response emits the generated acceptance once. Other response headers,
generic upgrades and the completion-close behavior are unchanged.

References:
RFC 6455 sections 4.2.2 and 11.3.3; review of apache#1608.
@He-Pin
He-Pin force-pushed the fix/ws-negotiated-response-headers branch from 71c5685 to 73e58bb Compare November 27, 2026 00:00
@He-Pin
He-Pin requested a review from pjfanning November 27, 2026 00:00
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.

2 participants