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: `ExtraInfoResponse` class `getResult` method bug #243

Closed joyyir closed 1 year ago

joyyir commented 1 year ago

Issue description

There is a bug in the getResult method of the ExtraInfoResponse class. If the encoding of the upstream response is not UTF-8, The result string is broken. In my opinion, instead of casting to char, casting to byte is the correct way.

Original Code:

    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();
    }

Suggestion:

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

Environment

Minimal test code / Steps to reproduce the issue

  1. set Upstream which produces response other than UTF-8 encoding
  2. set ext-plugin-post-resp plugin setting in ApisixRoute
  3. log the result of PostRequest getBody() in postFilter method

What's the actual result? (including assertion message & call stack if applicable)

broken string

What's the expected result?

normal string