funky-eyes commented on code in PR #7451:
URL: https://github.com/apache/incubator-seata/pull/7451#discussion_r2157282328


##########
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:
   done



##########
core/src/test/java/org/apache/seata/core/rpc/netty/http/Http2HttpHandlerTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.ObjectMapper;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.embedded.EmbeddedChannel;
+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.Http2Headers;
+import io.netty.handler.codec.http2.Http2HeadersFrame;
+import io.netty.handler.codec.http2.Http2StreamFrame;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class Http2HttpHandlerTest {
+    private Http2HttpHandler handler;
+    private EmbeddedChannel channel;
+    private TestController testController = new TestController();
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    class TestController {
+        public String handleRequest(String param) {
+            return "Processed: " + param;
+        }
+    }
+
+    @BeforeEach
+    void setUp() throws Exception {
+        handler = new Http2HttpHandler();
+        channel = new EmbeddedChannel(handler);
+        // 注册controller
+        Method method = TestController.class.getMethod("handleRequest", 
String.class);
+        ParamMetaData paramMetaData = new ParamMetaData();
+        
paramMetaData.setParamConvertType(ParamMetaData.ParamConvertType.REQUEST_PARAM);
+        paramMetaData.setParamName("param");
+        ParamMetaData[] paramMetaDatas = new ParamMetaData[]{paramMetaData};
+        HttpInvocation invocation = new HttpInvocation();
+        invocation.setController(testController);
+        invocation.setMethod(method);
+        invocation.setPath("/test");
+        invocation.setParamMetaData(paramMetaDatas);
+        ControllerManager.addHttpInvocation(invocation);
+    }
+
+    private Http2StreamFrame waitForHttp2Response(long timeoutMs) {
+        long startTime = System.currentTimeMillis();
+        Http2StreamFrame response = null;
+        while (response == null && (System.currentTimeMillis() - startTime) < 
timeoutMs) {
+            response = channel.readOutbound();
+            if (response == null) {
+                try {
+                    Thread.sleep(10);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    throw new RuntimeException("Interrupted while waiting for 
response", e);
+                }
+            }
+        }
+        return response;
+    }
+
+    @Test
+    void testHttp2GetRequestWithParameters() throws Exception {
+        // 构造 GET 请求
+        Http2Headers headers = new DefaultHttp2Headers();
+        headers.method("GET");
+        headers.path("/test?param=testValue");
+        Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, 
true);
+        channel.writeInbound(headersFrame);
+
+        // 使用等待方法获取响应

Review Comment:
   done



-- 
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

Reply via email to