eclipse-vertx / vertx-http-proxy

vertx http proxy
Eclipse Public License 2.0
55 stars 36 forks source link

Error for modify body before gzip response #89

Closed nnzbz closed 4 months ago

nnzbz commented 4 months ago

Questions

Error for modify body before gzip response,browser developer tools network show "(failed)net::ERR_CONTENT_DECODING_FAILED"

Version

4.5.8

Context

My code referenced https://vertx.io/docs/vertx-http-proxy/java/#_interception_control, but when proxy response gzip occur "ERR_CONTENT_DECODING_FAILED" error in brower.

Do you have a reproducer?

No

Steps to reproduce

No

Extra

No

tsegismont commented 4 months ago

Can you please add more info to help us reproduce?

A complete reproducer is even better

nnzbz commented 4 months ago

client request:

Screenshot 2024-07-10 at 16 47 41

response:

Screenshot 2024-07-10 at 16 43 55

server http config:

Screenshot 2024-07-10 at 16 51 51

proxy config(forward kibana):

Screenshot 2024-07-10 at 16 54 20

modify code:

Screenshot 2024-07-10 at 16 57 36
tsegismont commented 4 months ago

So, if I understand correctly:

Correct?

nnzbz commented 4 months ago

Yes, that's right

tsegismont commented 4 months ago

I was able to reproduce.

The problem is that the Kibana response body is encoded with gzip (as indicated by the content-encoding header). So when you get the buffered content (with BufferingWriteStream) you cannot transform to String, because you're actually looking at binary content.

Instead, you must create the response like this:

          Buffer content = bufferingWriteStream.content();
          proxyResponse.setBody(Body.body(content));
          return context.sendResponse();

If you want to inspect the string content, you can do it like this:

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content.getBytes()))) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = gis.read(buffer)) != -1) {
              baos.write(buffer, 0, len);
            }
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
          String html = baos.toString(StandardCharsets.UTF_8);