YongGoose commented on code in PR #7451: URL: https://github.com/apache/incubator-seata/pull/7451#discussion_r2157049500
########## core/src/main/java/org/apache/seata/core/protocol/detector/HttpDetector.java: ########## @@ -52,10 +67,59 @@ private boolean startsWith(ByteBuf buffer, String prefix) { @Override public ChannelHandler[] getHandlers() { + 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(); + } + }; Review Comment: Is it okay not to override methods like `channelInactive` and `channelUnregistered`? ########## core/src/main/java/org/apache/seata/core/rpc/netty/http/Http2HttpHandler.java: ########## @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.core.rpc.netty.http; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.QueryStringDecoder; +import io.netty.handler.codec.http2.DefaultHttp2DataFrame; +import io.netty.handler.codec.http2.DefaultHttp2Headers; +import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame; +import io.netty.handler.codec.http2.Http2DataFrame; +import io.netty.handler.codec.http2.Http2Headers; +import io.netty.handler.codec.http2.Http2HeadersFrame; +import io.netty.handler.codec.http2.Http2StreamFrame; +import org.apache.seata.common.rpc.http.HttpContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; + +/** + * The http2 http handler. + */ +public class Http2HttpHandler extends BaseHttpChannelHandler<Http2StreamFrame> { + private static final Logger LOGGER = LoggerFactory.getLogger(Http2HttpHandler.class); + private Http2Headers http2Headers; + private ByteBuf bodyBuffer; + private boolean headersEndStream = false; + + @Override + protected void channelRead0(ChannelHandlerContext ctx, Http2StreamFrame msg) throws Exception { + if (bodyBuffer == null) { + bodyBuffer = ctx.alloc().buffer(); + } + try { + if (msg instanceof Http2HeadersFrame) { + Http2HeadersFrame headersFrame = (Http2HeadersFrame) msg; + this.http2Headers = headersFrame.headers(); + headersEndStream = headersFrame.isEndStream(); + if (headersEndStream) { + handleRequest(ctx); + } + } else if (msg instanceof Http2DataFrame) { + Http2DataFrame dataFrame = (Http2DataFrame) msg; + bodyBuffer.writeBytes(dataFrame.content()); + if (dataFrame.isEndStream()) { + handleRequest(ctx); + } + } Review Comment: [nit] If there’s a possibility that the type of msg could be extended in the future, using a `switch` statement could be a good option. ########## core/src/main/java/org/apache/seata/core/rpc/netty/http/Http2HttpHandler.java: ########## @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.core.rpc.netty.http; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.QueryStringDecoder; +import io.netty.handler.codec.http2.DefaultHttp2DataFrame; +import io.netty.handler.codec.http2.DefaultHttp2Headers; +import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame; +import io.netty.handler.codec.http2.Http2DataFrame; +import io.netty.handler.codec.http2.Http2Headers; +import io.netty.handler.codec.http2.Http2HeadersFrame; +import io.netty.handler.codec.http2.Http2StreamFrame; +import org.apache.seata.common.rpc.http.HttpContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; + +/** + * The http2 http handler. + */ +public class Http2HttpHandler extends BaseHttpChannelHandler<Http2StreamFrame> { + private static final Logger LOGGER = LoggerFactory.getLogger(Http2HttpHandler.class); + private Http2Headers http2Headers; + private ByteBuf bodyBuffer; + private boolean headersEndStream = false; + + @Override + protected void channelRead0(ChannelHandlerContext ctx, Http2StreamFrame msg) throws Exception { + if (bodyBuffer == null) { + bodyBuffer = ctx.alloc().buffer(); + } + try { + if (msg instanceof Http2HeadersFrame) { + Http2HeadersFrame headersFrame = (Http2HeadersFrame) msg; + this.http2Headers = headersFrame.headers(); + headersEndStream = headersFrame.isEndStream(); + if (headersEndStream) { + handleRequest(ctx); + } + } else if (msg instanceof Http2DataFrame) { + Http2DataFrame dataFrame = (Http2DataFrame) msg; + bodyBuffer.writeBytes(dataFrame.content()); + if (dataFrame.isEndStream()) { + handleRequest(ctx); + } + } + } catch (Exception e) { + if (bodyBuffer != null) { + bodyBuffer.release(); + bodyBuffer = null; + } + throw e; + } + } + + private void handleRequest(ChannelHandlerContext ctx) { Review Comment: The method looks well written overall. However, it might be a good idea to break it down into smaller methods. Repeated try blocks, for example, could make the code harder to understand for someone reading it for the first time during future maintenance. -- 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: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org