You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by tz...@apache.org on 2022/01/11 02:32:34 UTC
[apisix-java-plugin-runner] branch main updated: fix: pre-read requests prevent read/write index confusion (#113)
This is an automated email from the ASF dual-hosted git repository.
tzssangglass pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/apisix-java-plugin-runner.git
The following commit(s) were added to refs/heads/main by this push:
new 1a68261 fix: pre-read requests prevent read/write index confusion (#113)
1a68261 is described below
commit 1a682617a12a2350370f9bf5d6daa3f0bf11f099
Author: tzssangglass <tz...@gmail.com>
AuthorDate: Tue Jan 11 10:31:19 2022 +0800
fix: pre-read requests prevent read/write index confusion (#113)
---
.../apisix/plugin/runner/handler/HTTPReqCallHandler.java | 12 ++++++++++++
.../apache/apisix/plugin/runner/handler/ExtraInfoTest.java | 5 ++++-
.../java/org/apache/apisix/plugin/runner/HttpRequest.java | 5 ++++-
3 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/runner-core/src/main/java/org/apache/apisix/plugin/runner/handler/HTTPReqCallHandler.java b/runner-core/src/main/java/org/apache/apisix/plugin/runner/handler/HTTPReqCallHandler.java
index 439c582..2152703 100644
--- a/runner-core/src/main/java/org/apache/apisix/plugin/runner/handler/HTTPReqCallHandler.java
+++ b/runner-core/src/main/java/org/apache/apisix/plugin/runner/handler/HTTPReqCallHandler.java
@@ -142,6 +142,10 @@ public class HTTPReqCallHandler extends SimpleChannelInboundHandler<A6Request> {
PluginFilterChain chain = conf.getChain();
+ // here we pre-read parameters in the req to
+ // prevent confusion over the read/write index of the req.
+ preReadReq();
+
// if the filter chain is empty, then return the response directly
if (Objects.isNull(chain) || 0 == chain.getFilters().size()) {
ChannelFuture future = ctx.writeAndFlush(currResp);
@@ -194,6 +198,14 @@ public class HTTPReqCallHandler extends SimpleChannelInboundHandler<A6Request> {
}
}
+ private void preReadReq() {
+ currReq.getHeader();
+ currReq.getPath();
+ currReq.getMethod();
+ currReq.getArgs();
+ currReq.getSourceIP();
+ }
+
private void errorHandle(ChannelHandlerContext ctx, int code) {
A6ErrResponse errResponse = new A6ErrResponse(code);
ctx.writeAndFlush(errResponse);
diff --git a/runner-core/src/test/java/org/apache/apisix/plugin/runner/handler/ExtraInfoTest.java b/runner-core/src/test/java/org/apache/apisix/plugin/runner/handler/ExtraInfoTest.java
index 51daa05..b564597 100644
--- a/runner-core/src/test/java/org/apache/apisix/plugin/runner/handler/ExtraInfoTest.java
+++ b/runner-core/src/test/java/org/apache/apisix/plugin/runner/handler/ExtraInfoTest.java
@@ -204,7 +204,7 @@ class ExtraInfoTest {
}
@Test
- @DisplayName("test fetch request body of extra info")
+ @DisplayName("test get vars in plugin filter")
void testGetVarsInPluginFilter() {
FlatBufferBuilder builder = new FlatBufferBuilder();
@@ -276,6 +276,9 @@ class ExtraInfoTest {
Assertions.assertTrue(bytes.toString().contains("server_port: 9080"));
Assertions.assertTrue(bytes.toString().contains("content_type: application/json"));
Assertions.assertTrue(bytes.toString().contains("body: abcd"));
+
+ // test pre-read request in HttpCallHandler
+ Assertions.assertEquals(HttpRequest.Method.GET, request.getMethod());
}
}
diff --git a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpRequest.java b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpRequest.java
index 38d0212..d4f75b4 100644
--- a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpRequest.java
+++ b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpRequest.java
@@ -21,6 +21,7 @@ import io.github.api7.A6.HTTPReqCall.Req;
import io.github.api7.A6.TextEntry;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
import java.nio.ByteBuffer;
import java.util.HashMap;
@@ -84,7 +85,9 @@ public class HttpRequest implements A6Request {
for (int i = 0; i < req.srcIpLength(); i++) {
builder.append(req.srcIp(i)).append('.');
}
- sourceIP = builder.substring(0, builder.length() - 1);
+ if (StringUtils.hasText(builder.toString())) {
+ sourceIP = builder.substring(0, builder.length() - 1);
+ }
}
return sourceIP;