From 3ae6f49f26bc793b7f1ed779749e0962f92e3883 Mon Sep 17 00:00:00 2001 From: DD Date: Wed, 11 Aug 2021 16:22:38 +0800 Subject: [PATCH 1/4] Update HttpServerUpgradeHandler.java --- .../io/netty/handler/codec/http/HttpServerUpgradeHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java index 88aa739b298..2aa6e157d56 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java @@ -323,7 +323,7 @@ private boolean upgrade(final ChannelHandlerContext ctx, final FullHttpRequest r // Make sure the CONNECTION header is present. List connectionHeaderValues = request.headers().getAll(HttpHeaderNames.CONNECTION); - if (connectionHeaderValues == null) { + if (connectionHeaderValues == null || connectionHeaderValues.isEmpty()) { return false; } From 10a336713cde8911faa37fee58935b6fdb2d4e1d Mon Sep 17 00:00:00 2001 From: "bob.dong" Date: Thu, 12 Aug 2021 12:03:43 +0800 Subject: [PATCH 2/4] add test --- .../http/HttpServerUpgradeHandlerTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java index bc4552afb4f..8ade9c59563 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java @@ -189,4 +189,41 @@ protected boolean shouldHandleUpgradeRequest(HttpRequest req) { assertNull(channel.readOutbound()); assertFalse(channel.finishAndReleaseAll()); } + + @Test + public void upgradeFail() { + final HttpServerCodec httpServerCodec = new HttpServerCodec(); + final UpgradeCodecFactory factory = new UpgradeCodecFactory() { + @Override + public UpgradeCodec newUpgradeCodec(CharSequence protocol) { + return new TestUpgradeCodec(); + } + }; + + HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory); + + EmbeddedChannel channel = new EmbeddedChannel(httpServerCodec, upgradeHandler); + + // Build a h2c upgrade request, but without connection header. + String upgradeString = "GET / HTTP/1.1\r\n" + + "Host: example.com\r\n" + + "Upgrade: h2c\r\n\r\n"; + ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII); + + assertTrue(channel.writeInbound(upgrade)); + assertNotNull(channel.pipeline().get(HttpServerCodec.class)); + assertNotNull(channel.pipeline().get(HttpServerUpgradeHandler.class)); // Should not be removed since upgrade failed. + assertNull(channel.pipeline().get("marker")); + + HttpRequest req = channel.readInbound(); + assertEquals(HttpVersion.HTTP_1_1, req.protocolVersion()); + assertTrue(req.headers().contains(HttpHeaderNames.UPGRADE, "h2c", false)); + assertFalse(req.headers().contains(HttpHeaderNames.CONNECTION)); + assertNull(channel.readInbound()); + + // No response should be written because we're just passing through. + channel.flushOutbound(); + assertNull(channel.readOutbound()); + assertFalse(channel.finishAndReleaseAll()); + } } From a7eec7afcaa939dee93f034f0fae177bdca5360d Mon Sep 17 00:00:00 2001 From: "bob.dong" Date: Thu, 12 Aug 2021 15:19:12 +0800 Subject: [PATCH 3/4] fix check-style --- .../netty/handler/codec/http/HttpServerUpgradeHandlerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java index 8ade9c59563..ae3d7897708 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java @@ -212,7 +212,7 @@ public UpgradeCodec newUpgradeCodec(CharSequence protocol) { assertTrue(channel.writeInbound(upgrade)); assertNotNull(channel.pipeline().get(HttpServerCodec.class)); - assertNotNull(channel.pipeline().get(HttpServerUpgradeHandler.class)); // Should not be removed since upgrade failed. + assertNotNull(channel.pipeline().get(HttpServerUpgradeHandler.class)); // Should not be removed. assertNull(channel.pipeline().get("marker")); HttpRequest req = channel.readInbound(); From b974258aa095c488ffc51388d1a1d017a82bd8b9 Mon Sep 17 00:00:00 2001 From: "bob.dong" Date: Thu, 12 Aug 2021 18:58:51 +0800 Subject: [PATCH 4/4] release request --- .../netty/handler/codec/http/HttpServerUpgradeHandlerTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java index ae3d7897708..a2e27e787f6 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java @@ -31,6 +31,7 @@ import io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodec; import io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory; import io.netty.util.CharsetUtil; +import io.netty.util.ReferenceCountUtil; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -219,6 +220,7 @@ public UpgradeCodec newUpgradeCodec(CharSequence protocol) { assertEquals(HttpVersion.HTTP_1_1, req.protocolVersion()); assertTrue(req.headers().contains(HttpHeaderNames.UPGRADE, "h2c", false)); assertFalse(req.headers().contains(HttpHeaderNames.CONNECTION)); + ReferenceCountUtil.release(req); assertNull(channel.readInbound()); // No response should be written because we're just passing through.