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 — Ensure writes will not fail when triggered after receiving UpgradeEve… · netty/netty@ddbac87

File tree

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

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpClientUpgradeHandler.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpClientUpgradeHandler.java
+10-5Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public interface UpgradeCodec {
105105

106106
private final SourceCodec sourceCodec;
107107
private final UpgradeCodec upgradeCodec;
108-
private boolean upgradeRequested;
108+
private UpgradeEvent currentUpgradeEvent;
109109

110110
/**
111111
* Constructs the client upgrade handler.
@@ -155,20 +155,20 @@ public void read(ChannelHandlerContext ctx) throws Exception {
155155
@Override
156156
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
157157
throws Exception {
158-
if (!(msg instanceof HttpRequest)) {
158+
if (!(msg instanceof HttpRequest) || currentUpgradeEvent == UpgradeEvent.UPGRADE_SUCCESSFUL) {
159159
ctx.write(msg, promise);
160160
return;
161161
}
162162

163-
if (upgradeRequested) {
163+
if (currentUpgradeEvent == UpgradeEvent.UPGRADE_ISSUED) {
164164
// Release message before failing the promise.
165165
ReferenceCountUtil.release(msg);
166166
promise.setFailure(new IllegalStateException(
167167
"Attempting to write HTTP request with upgrade in progress"));
168168
return;
169169
}
170170

171-
upgradeRequested = true;
171+
currentUpgradeEvent = UpgradeEvent.UPGRADE_ISSUED;
172172
setUpgradeRequestHeaders(ctx, (HttpRequest) msg);
173173

174174
// Continue writing the request.
@@ -189,7 +189,7 @@ protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
189189
throws Exception {
190190
FullHttpResponse response = null;
191191
try {
192-
if (!upgradeRequested) {
192+
if (currentUpgradeEvent != UpgradeEvent.UPGRADE_ISSUED) {
193193
throw new IllegalStateException("Read HTTP response without requesting protocol switch");
194194
}
195195

@@ -200,6 +200,7 @@ protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
200200
// and continue processing HTTP.
201201
// NOTE: not releasing the response since we're letting it propagate to the
202202
// next handler.
203+
currentUpgradeEvent = null;
203204
ctx.fireUserEventTriggered(UpgradeEvent.UPGRADE_REJECTED);
204205
removeThisHandler(ctx);
205206
ctx.fireChannelRead(msg);
@@ -234,6 +235,10 @@ protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
234235
sourceCodec.prepareUpgradeFrom(ctx);
235236
upgradeCodec.upgradeTo(ctx, response);
236237

238+
// Let's set currentUpgradeEvent to UPGRADE_SUCCESSFUL as we will notify the pipeline about the successful
239+
// upgrade now, which might results in a write that needs to be passed through.
240+
currentUpgradeEvent = UpgradeEvent.UPGRADE_SUCCESSFUL;
241+
237242
// Notify that the upgrade to the new protocol completed successfully.
238243
ctx.fireUserEventTriggered(UpgradeEvent.UPGRADE_SUCCESSFUL);
239244

Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpClientUpgradeHandlerTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpClientUpgradeHandlerTest.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.netty.channel.ChannelHandlerContext;
1919
import io.netty.channel.ChannelInboundHandlerAdapter;
20+
import io.netty.channel.ChannelPromise;
2021
import io.netty.channel.embedded.EmbeddedChannel;
2122

2223
import java.util.Collection;
@@ -80,6 +81,19 @@ public void testSuccessfulUpgrade() {
8081
HttpClientUpgradeHandler handler = new HttpClientUpgradeHandler(sourceCodec, upgradeCodec, 1024);
8182
UserEventCatcher catcher = new UserEventCatcher();
8283
EmbeddedChannel channel = new EmbeddedChannel(catcher);
84+
final HttpRequest afterUpgradeMessage =
85+
new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "netty.io");
86+
final ChannelPromise promise = channel.newPromise();
87+
channel.pipeline().addFirst(new ChannelInboundHandlerAdapter() {
88+
@Override
89+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
90+
if (evt == HttpClientUpgradeHandler.UpgradeEvent.UPGRADE_SUCCESSFUL) {
91+
ctx.writeAndFlush(afterUpgradeMessage, promise);
92+
}
93+
super.userEventTriggered(ctx, evt);
94+
}
95+
});
96+
8397
channel.pipeline().addFirst("upgrade", handler);
8498

8599
assertTrue(
@@ -106,6 +120,10 @@ public void testSuccessfulUpgrade() {
106120
FullHttpResponse response = channel.readInbound();
107121
assertEquals(HttpResponseStatus.OK, response.status());
108122
assertTrue(response.release());
123+
124+
assertTrue(promise.isSuccess());
125+
assertEquals(afterUpgradeMessage, channel.readOutbound());
126+
109127
assertFalse(channel.finish());
110128
}
111129

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.