GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow WebSocket extension negotiation to be disabled per response (#1… · netty/netty@a0d62cd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http/websocketx/extensions
      • WebSocketServerExtensionHandler.java
    • test/java/io/netty/handler/codec/http/websocketx/extensions
      • WebSocketServerExtensionHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/websocketx/extensions/WebSocketServerExtensionHandler.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/extensions/WebSocketServerExtensionHandler.java
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,25 @@ protected void onHttpResponseWrite(ChannelHandlerContext ctx, HttpResponse respo
213213
}
214214
}
215215

216+
/**
217+
* Returns {@code true} if WebSocket extensions negotiated from the request should be acknowledged for this
218+
* response.
219+
* <p>
220+
* This method can be overridden to make the final extension negotiation decision when the handshake response is
221+
* written.
222+
*/
223+
protected boolean isExtensionNegotiationEnabled(ChannelHandlerContext ctx, HttpResponse response) {
224+
return true;
225+
}
226+
216227
private void handlePotentialUpgrade(final ChannelHandlerContext ctx,
217228
Future<Void> future, HttpResponse httpResponse,
218229
final List<WebSocketServerExtension> validExtensionsList) {
219230
HttpHeaders headers = httpResponse.headers();
220231

221232
if (WebSocketExtensionUtil.isWebsocketUpgrade(headers)) {
222-
if (validExtensionsList != null && !validExtensionsList.isEmpty()) {
233+
if (isExtensionNegotiationEnabled(ctx, httpResponse)
234+
&& validExtensionsList != null && !validExtensionsList.isEmpty()) {
223235
String headerValue = headers.getAsString(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
224236
List<WebSocketExtensionData> extraExtensions =
225237
new ArrayList<>(extensionHandshakers.size());
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/websocketx/extensions/WebSocketServerExtensionHandlerTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/websocketx/extensions/WebSocketServerExtensionHandlerTest.java
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.handler.codec.http.websocketx.extensions;
1717

18+
import io.netty.channel.ChannelHandlerContext;
1819
import io.netty.channel.embedded.EmbeddedChannel;
1920
import io.netty.handler.codec.http.HttpHeaderNames;
2021
import io.netty.handler.codec.http.HttpRequest;
@@ -211,6 +212,45 @@ public void testNoneExtensionMatchingSuccess() throws Exception {
211212
verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown2"));
212213
}
213214

215+
@Test
216+
public void testExtensionNegotiationDisabledForResponse() throws Exception {
217+
// initialize
218+
when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).
219+
thenReturn(mainExtensionMock);
220+
221+
when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
222+
223+
// execute
224+
WebSocketServerExtensionHandler extensionHandler =
225+
new WebSocketServerExtensionHandler(mainHandshakerMock) {
226+
@Override
227+
protected boolean isExtensionNegotiationEnabled(ChannelHandlerContext ctx, HttpResponse response) {
228+
return false;
229+
}
230+
};
231+
EmbeddedChannel ch = new EmbeddedChannel(extensionHandler);
232+
233+
HttpRequest req = newUpgradeRequest("main");
234+
ch.writeInbound(req);
235+
236+
HttpResponse res = newUpgradeResponse(null);
237+
ch.writeOutbound(res);
238+
239+
HttpResponse res2 = ch.readOutbound();
240+
241+
// test
242+
assertNull(ch.pipeline().context(extensionHandler));
243+
assertFalse(res2.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
244+
assertNull(ch.pipeline().get(DummyDecoder.class));
245+
assertNull(ch.pipeline().get(DummyEncoder.class));
246+
247+
verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("main"));
248+
verify(mainExtensionMock, times(2)).rsv();
249+
verify(mainExtensionMock, never()).newReponseData();
250+
verify(mainExtensionMock, never()).newExtensionEncoder();
251+
verify(mainExtensionMock, never()).newExtensionDecoder();
252+
}
253+
214254
@Test
215255
public void testExtensionHandlerNotRemovedByFailureWritePromise() throws Exception {
216256
// initialize

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.