Skip to content
Permalink
Browse files
fix: pre-read requests prevent read/write index confusion (#113)
  • Loading branch information
tzssangglass committed Jan 11, 2022
1 parent d9b9214 commit 1a682617a12a2350370f9bf5d6daa3f0bf11f099
Showing 3 changed files with 20 additions and 2 deletions.
@@ -142,6 +142,10 @@ private void handleHttpReqCall(ChannelHandlerContext ctx, HttpRequest request) {

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 @@ private void handleHttpReqCall(ChannelHandlerContext ctx, HttpRequest request) {
}
}

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);
@@ -204,7 +204,7 @@ void testFetchBody() {
}

@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 @@ void testGetVarsInPluginFilter() {
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());
}

}
@@ -21,6 +21,7 @@
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 String getSourceIP() {
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;

0 comments on commit 1a68261

Please sign in to comment.