Skip to content
Permalink
Browse files
fix: stop request but not calling setStatusCode will trigger an excep…
…tion In APISIX (#56)
  • Loading branch information
tzssangglass committed Aug 9, 2021
1 parent bcf2ce3 commit 5a6c83709085527702dfc428117c2ae87f230d31
Showing 2 changed files with 29 additions and 0 deletions.
@@ -263,6 +263,24 @@ void testEncodeDataGreaterLargeThen256() {
Assertions.assertEquals(bytes[0], array[1]);
Assertions.assertEquals(bytes[1], array[2]);
Assertions.assertEquals(bytes[2], array[3]);
}

@Test
@DisplayName("test stop the request without setStatusCode")
void testDoFilterWithoutSetStatusCode() {
HttpResponse httpResponse = new HttpResponse(0L);
// only set header, without setStatusCode, use 200 as default
httpResponse.setHeader("Foo", "Bar");
ByteBuffer result = flatBuffersEncoder.encode(httpResponse);
result.position(4);
io.github.api7.A6.HTTPReqCall.Resp resp = io.github.api7.A6.HTTPReqCall.Resp.getRootAsResp(result);
Assertions.assertEquals(resp.actionType(), Action.Stop);
Stop stop = (Stop) resp.action(new Stop());
Assertions.assertEquals(stop.status(), 200);
for (int i = 0; i < stop.headersLength(); i++) {
if (stop.headers(i).name().equals("Foo")) {
Assertions.assertEquals(stop.headers(i).value(), "Bar");
}
}
}
}
@@ -22,6 +22,8 @@
import io.github.api7.A6.HTTPReqCall.Rewrite;
import io.github.api7.A6.HTTPReqCall.Stop;
import io.github.api7.A6.TextEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@@ -38,6 +40,7 @@
* }
*/
public class HttpResponse implements A6Response {
private final Logger logger = LoggerFactory.getLogger(HttpResponse.class);

private final long requestId;

@@ -194,6 +197,14 @@ private int buildStopResp(FlatBufferBuilder builder) {
Stop.startStop(builder);
if (!Objects.isNull(statusCode)) {
Stop.addStatus(builder, statusCode);
} else {
/**
* Avoid APISIX using 0 as the default HTTP Status Code
* {@link org.apache.apisix.plugin.runner.HttpResponse#setStatusCode(int statusCode)}
* @see https://github.com/apache/apisix-java-plugin-runner/issues/55
*/
Stop.addStatus(builder, 200);
logger.info("Use 200 as the default HTTP Status Code when setStatusCode is not called");
}
if (-1 != headerIndex) {
Stop.addHeaders(builder, headerIndex);

0 comments on commit 5a6c837

Please sign in to comment.