This is an automated email from the ASF dual-hosted git repository.
spacewander 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 5a6c837 fix: stop request but not calling setStatusCode will trigger
an exception In APISIX (#56)
5a6c837 is described below
commit 5a6c83709085527702dfc428117c2ae87f230d31
Author: tzssangglass <[email protected]>
AuthorDate: Mon Aug 9 15:18:40 2021 +0800
fix: stop request but not calling setStatusCode will trigger an exception
In APISIX (#56)
---
.../runner/codec/impl/FlatBuffersEncoderTest.java | 18 ++++++++++++++++++
.../org/apache/apisix/plugin/runner/HttpResponse.java | 11 +++++++++++
2 files changed, 29 insertions(+)
diff --git
a/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
b/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
index 2d3ddbd..7cb9c11 100644
---
a/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
+++
b/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
@@ -263,6 +263,24 @@ class FlatBuffersEncoderTest {
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");
+ }
+ }
}
}
diff --git
a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
index 916f748..409e955 100644
---
a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
+++
b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
@@ -22,6 +22,8 @@ import io.github.api7.A6.HTTPReqCall.Resp;
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 @@ import java.util.Objects;
* }
*/
public class HttpResponse implements A6Response {
+ private final Logger logger = LoggerFactory.getLogger(HttpResponse.class);
private final long requestId;
@@ -194,6 +197,14 @@ public class HttpResponse implements A6Response {
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);