Skip to content

Commit 8afa49e

Browse files
authored
bug: ExtraInfoResponse class getResult method bug (#244)
1 parent 25512a2 commit 8afa49e

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

runner-core/src/main/java/org/apache/apisix/plugin/runner/handler/RpcCallHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private void doPostFilter(ChannelHandlerContext ctx) {
215215
}
216216

217217
private void handleExtraInfo(ChannelHandlerContext ctx, ExtraInfoResponse request) {
218-
String result = request.getResult();
218+
byte[] result = request.getResult();
219219
String varsKey = queue.poll();
220220
if (Objects.isNull(varsKey)) {
221221
logger.error("queue is empty");
@@ -233,7 +233,7 @@ private void handleExtraInfo(ChannelHandlerContext ctx, ExtraInfoResponse reques
233233
}
234234
}
235235
else {
236-
nginxVars.put(varsKey, result);
236+
nginxVars.put(varsKey, new String(result));
237237
}
238238

239239
if (queue.isEmpty()) {

runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/ExtraInfoResponse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public static ExtraInfoResponse from(ByteBuffer buffer) {
3535
return new ExtraInfoResponse(req);
3636
}
3737

38-
public String getResult() {
39-
StringBuilder builder = new StringBuilder();
38+
public byte[] getResult() {
39+
byte[] byteArray = new byte[this.resp.resultLength()];
4040
for (int i = 0; i < this.resp.resultLength(); i++) {
41-
builder.append((char) this.resp.result(i));
41+
byteArray[i] = (byte) this.resp.result(i);
4242
}
43-
return builder.toString();
43+
return byteArray;
4444
}
4545

4646
@Override

runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpRequest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.util.StringUtils;
2525

2626
import java.nio.ByteBuffer;
27+
import java.nio.charset.Charset;
2728
import java.util.HashMap;
2829
import java.util.Map;
2930
import java.util.Objects;
@@ -50,7 +51,7 @@ public class HttpRequest implements A6Request {
5051

5152
private Map<String, String> vars;
5253

53-
private String body;
54+
private byte[] body;
5455

5556
public HttpRequest(Req req) {
5657
this.req = req;
@@ -300,10 +301,18 @@ public void setVars(Map<String, String> vars) {
300301
}
301302

302303
public String getBody() {
303-
return body;
304+
return new String(body);
305+
}
306+
307+
public String getBody(Charset charset) {
308+
return new String(body, charset);
304309
}
305310

306311
public void setBody(String body) {
312+
this.body = body.getBytes();
313+
}
314+
315+
public void setBody(byte[] body) {
307316
this.body = body;
308317
}
309318

runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/PostRequest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.util.CollectionUtils;
2424

2525
import java.nio.ByteBuffer;
26+
import java.nio.charset.Charset;
2627
import java.util.HashMap;
2728
import java.util.Map;
2829
import java.util.Objects;
@@ -38,7 +39,7 @@ public class PostRequest implements A6Request {
3839

3940
private Integer status;
4041

41-
private String body;
42+
private byte[] body;
4243

4344
private Map<String, String> vars;
4445

@@ -94,11 +95,19 @@ public Integer getUpstreamStatusCode() {
9495
}
9596

9697
public void setBody(String body) {
98+
this.body = body.getBytes();
99+
}
100+
101+
public void setBody(byte[] body) {
97102
this.body = body;
98103
}
99104

100105
public String getBody() {
101-
return body;
106+
return new String(body);
107+
}
108+
109+
public String getBody(Charset charset) {
110+
return new String(body, charset);
102111
}
103112

104113
public String getVars(String key) {

runner-plugin-sdk/src/test/java/org/apache/apisix/plugin/runner/PostResponseTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.apisix.plugin.runner;
219

320
import org.junit.jupiter.api.DisplayName;

0 commit comments

Comments
 (0)