YongGoose commented on code in PR #7451:
URL: https://github.com/apache/incubator-seata/pull/7451#discussion_r2166799600


##########
core/src/main/java/org/apache/seata/core/protocol/detector/HttpDetector.java:
##########
@@ -52,7 +67,59 @@ private boolean startsWith(ByteBuf buffer, String prefix) {
 
     @Override
     public ChannelHandler[] getHandlers() {
-        return new ChannelHandler[] {new HttpServerCodec(), new 
HttpObjectAggregator(1048576), new HttpDispatchHandler()
+        HttpServerCodec sourceCodec = new HttpServerCodec();
+        HttpServerUpgradeHandler upgradeHandler = 
getHttpServerUpgradeHandler(sourceCodec);
+
+        ChannelInboundHandlerAdapter upgradeCleanupHandler = new 
ChannelInboundHandlerAdapter() {
+            @Override
+            public void userEventTriggered(ChannelHandlerContext ctx, Object 
evt) throws Exception {
+                if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) {
+                    ChannelPipeline p = ctx.pipeline();
+                    p.remove(HttpObjectAggregator.class);
+                    p.remove(HttpDispatchHandler.class);
+                }
+                super.userEventTriggered(ctx, evt);
+            }
+        };
+
+        ChannelInboundHandlerAdapter finalExceptionHandler = new 
ChannelInboundHandlerAdapter() {
+            @Override
+            public void exceptionCaught(ChannelHandlerContext ctx, Throwable 
cause) {
+                if (cause instanceof java.io.IOException) {
+                    LOGGER.trace("Connection closed by client: {}", 
cause.getMessage());
+                } else {
+                    LOGGER.error("Exception caught in HTTP pipeline: ", cause);
+                }
+                ctx.close();
+            }
+        };
+
+        return new ChannelHandler[] {
+            sourceCodec,
+            upgradeHandler,
+            upgradeCleanupHandler,
+            new HttpObjectAggregator(1048576),
+            new HttpDispatchHandler(),
+            finalExceptionHandler
+        };
+    }
+
+    private static HttpServerUpgradeHandler 
getHttpServerUpgradeHandler(HttpServerCodec sourceCodec) {
+        HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = 
protocol -> {
+            if 
(AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, 
protocol)) {
+                return new Http2ServerUpgradeCodec(
+                        Http2FrameCodecBuilder.forServer().build(),
+                        new Http2MultiplexHandler(new 
ChannelInitializer<Http2StreamChannel>() {
+                            @Override
+                            protected void initChannel(Http2StreamChannel ch) {
+                                ch.pipeline().addLast(new Http2HttpHandler());
+                            }
+                        }));
+            } else {
+                return null;

Review Comment:
   Wouldn’t passing null cause a `NullPointerException`?
   
   
https://github.com/netty/netty/blob/dd42323c07db572265607cfad3c43995b6846a19/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java#L243



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to