apache / apisix-java-plugin-runner

APISIX Plugin Runner in Java
https://apisix.apache.org/
Apache License 2.0
128 stars 95 forks source link

bug: POST request with body contain chinese characters exist encoding problem #154

Open lbrant opened 2 years ago

lbrant commented 2 years ago

Issue description

Environment

apisix-java-plugin-runner version:0.2.0 apsix version:2.13.1

Minimal test code / Steps to reproduce the issue

  1. Test post request contain chinese characters in OKHttp: {"source":"高德地图","content":"https://surl.amap.com/1PoSAsa1l4Ud","deviceId":"123456"}
  2. inner OKHttp,body will be translated to utf-8 format bytes: Charset charset = Util.UTF_8; byte[] bytes = content.getBytes(charset);
  3. Custom filter in apisix-java-plugin-runner, get request body: {"source":"高德地图","content":"https://surl.amap.com/1PoSAsa1l4Ud","deviceId":"123456"}
tzssangglass commented 2 years ago

apisix-java-plugin-runner also use UTF_8, see: https://github.com/apache/apisix-java-plugin-runner/pull/53/files

lbrant commented 2 years ago

@tzssangglass

I mean body in request, fixed by modifing ExtraInfoResponse.getResult:

from:

public String getResult() {
      StringBuilder builder = new StringBuilder();
       for (int i = 0; i < this.resp.resultLength(); i++) {
            builder.append((char) this.resp.result(i));
       }
       return builder.toString();
    }

to

public String getResult() {
        byte[] bytes = new byte[this.resp.resultLength()];
        for (int i = 0; i < this.resp.resultLength(); i++) {
            bytes[i] = (byte) this.resp.result(i);
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }
tzssangglass commented 2 years ago

would you like to submit a PR to fix it?

dargoner commented 1 year ago

@tzssangglass

I mean body in request, fixed by modifing ExtraInfoResponse.getResult:

from:

public String getResult() {
      StringBuilder builder = new StringBuilder();
       for (int i = 0; i < this.resp.resultLength(); i++) {
            builder.append((char) this.resp.result(i));
       }
       return builder.toString();
    }

to

public String getResult() {
        byte[] bytes = new byte[this.resp.resultLength()];
        for (int i = 0; i < this.resp.resultLength(); i++) {
            bytes[i] = (byte) this.resp.result(i);
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }

确实有个问题

fitz-97 commented 1 year ago

0.4.0 Cannot be modified manually

tzssangglass commented 1 year ago

We can make it configurable by reading configuration from the configuration file.