You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "coolboywcm (via GitHub)" <gi...@apache.org> on 2023/08/03 06:51:32 UTC

[GitHub] [apisix] coolboywcm opened a new issue, #9961: bug: http-logger reseponse body decode fail

coolboywcm opened a new issue, #9961:
URL: https://github.com/apache/apisix/issues/9961

   ### Current Behavior
   
   Version: APISIX/3.1.0
   After enabling the http-logger plug-in, if response has been compress by gzip, the response body receives garbled characters
   The configuration is as follows
   
   "http-logger": {
         "_meta": {
           "disable": false
         },
         "include_resp_body": true,
         "uri": "http://xxx.xx.xx.xx:80/apisix/addLog"
       }
   
   See the response body by http-logger:
   "response": {
       "body":"\u001F�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000",
           "headers": {
               "content-encoding": "gzip",
               "content-length": "37",
               "content-type": "application/json;charset=UTF-8",
               "date": "Thu, 03 Aug 2023 06:32:24 GMT",
               "server": "APISIX/3.1.0",
               ........
           },
           "size": 377,
           "status": 200
   }
   
   The upstream server uses gzip to compress the response, and the http-logger plug-in records the response-body, directly converting the gzip content into a string, resulting in garbled characters
   
   ### Expected Behavior
   
   "response": {
       "body":{"key1": "123234"},
       "headers": {
               "content-encoding": "gzip",
               "content-length": "37",
               "content-type": "application/json;charset=UTF-8",
               "date": "Thu, 03 Aug 2023 06:32:24 GMT",
               "server": "APISIX/3.1.0",
               ........
           },
           "size": 377,
           "status": 200
   }
   
   ### Error Logs
   
   _No response_
   
   ### Steps to Reproduce
   
   Open the  plug-in http-logger in route
   Call the route interface
   View the http-logger uri server console and view the log message
   
   ### Environment
   
   APISIX version (3.1.0):
   Operating system (CentOS Linux release 7.6.1810):
   OpenResty / Nginx version (nginx version: openresty/1.21.4.1):
   etcd version, if relevant ("etcd_version":"3.4.0"):
   APISIX Dashboard version, if relevant(3.0.0)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] coolboywcm commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "coolboywcm (via GitHub)" <gi...@apache.org>.
coolboywcm commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1665119637

   > 
   "uri": "http://xxx.xx.xx.xx:80/apisix/addLog"
   
   controller method  addLog  is as follows
   
   ```
   import com.alibaba.fastjson.JSONArray;
   import com.alibaba.fastjson.JSONObject;
   
   public void addLog() throws IOException {
           String encoding = this.getRequest().getCharacterEncoding();
           System.out.println("1encoding=========================================>" + encoding);
           if (encoding == null) {
               encoding = StandardCharsets.UTF_8.name(); 
           }
           System.out.println("2encoding=========================================>" + encoding);
           StringBuilder stringBuilder = new StringBuilder();
           BufferedReader reader = new BufferedReader(new InputStreamReader(this.getRequest().getInputStream(), encoding));
           String line;
           while ((line = reader.readLine()) != null) {
               stringBuilder.append(line);
           }
         System.out.println("00000000000000stringBuilder.toString()============================================>" + stringBuilder.toString());
           String jsonString = stringBuilder.toString();
           JSONObject logJson = JSONArray.parseArray(jsonString).getJSONObject(0);
           jsonString = logJson.toJSONString();
           System.out.println("3333333333333jsonString============================================>" + jsonString);
           String resp_body = logJson.getJSONObject("response").getString("body");
           System.out.println("addLog=================================>111resp_body:" + resp_body);
           resp_body = decompress(resp_body.getBytes(encoding);
           System.out.println("addLog=================================>222resp_body:" + resp_body);
   }
   ```
   method  decompress  is as follows
   
   ```
   import java.util.zip.GZIPInputStream;
   import java.util.zip.GZIPOutputStream;
   public static String decompress(byte[] compressedData) throws IOException {
           try (ByteArrayInputStream bis = new ByteArrayInputStream(compressedData);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                GZIPInputStream gzipIS = new GZIPInputStream(bis)) {
   
               byte[] buffer = new byte[1024];
               int len;
               while ((len = gzipIS.read(buffer)) > 0) {
                   bos.write(buffer, 0, len);
               }
               return bos.toString("UTF-8");
           }
       }
   ```
   
   console print result:
   
   ```
   1encoding=========================================>UTF-8
   2encoding=========================================>UTF-8
   00000000000000stringBuilder.toString()============================================>[{"response":{"status":200,"headers":{"connection":"close","access-control-allow-origin":"*","server":"APISIX\/3.1.0","content-type":"application\/json;charset=UTF-8","content-encoding":"gzip","date":"Fri, 04 Aug 2023 06:52:28 GMT","access-control-allow-headers":"Content-Type,X-Requested-With,withCredentials","access-control-allow-methods":"GET, POST","content-length":"37"},"body":"\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000","size":377}}]
   3333333333333jsonString============================================>{"response":{"status":200,"headers":{"connection":"close","access-control-allow-origin":"*","server":"APISIX\/3.1.0","content-type":"application\/json;charset=UTF-8","content-encoding":"gzip","date":"Fri, 04 Aug 2023 06:52:28 GMT","access-control-allow-headers":"Content-Type,X-Requested-With,withCredentials","access-control-allow-methods":"GET, POST","content-length":"37"},"body":"\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000","size":377}}
   addLog=================================>111resp_body:\u001F�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000
   java.util.zip.ZipException: Not in GZIP format
           at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
           at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
           at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
           at com.ApiSixController.decompress(ApiSixController.java:332)
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] coolboywcm commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "coolboywcm (via GitHub)" <gi...@apache.org>.
coolboywcm commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1665139768

   > > > @coolboywcm This is not a bug but a feature request. Currently the logger doesn't decompress the compressed body and logs the body as it is.
   > > 
   > > 
   > > I tried to decompress and convert the response-body of http-logger into a byte stream, but it failed. During the process of converting the gzip content into a string forcibly by http-logger, the original data has been damaged.
   > 
   > For repro purposes, can you show the code how you were decompressing the body?
   
   In the above reply, I provided the code and the result


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] coolboywcm commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "coolboywcm (via GitHub)" <gi...@apache.org>.
coolboywcm commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1663460464

   I tried to decompress and convert the response-body of http-logger into a byte stream, but it failed. During the process of converting the gzip content into a string forcibly by http-logger, the original data has been damaged.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1670741625

   Currently gzipd responses are not handled properly as the supported concat_methods are: application/json and text/plain with no consideration of gzipped responses.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1663880041

   @coolboywcm This is not a bug but a feature request. Currently the logger doesn't decompress the compressed body and logs the body as it is.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1672562780

   @coolboywcm Thanks for sharing the solution.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1664998771

   > > @coolboywcm This is not a bug but a feature request. Currently the logger doesn't decompress the compressed body and logs the body as it is.
   > 
   > I tried to decompress and convert the response-body of http-logger into a byte stream, but it failed. During the process of converting the gzip content into a string forcibly by http-logger, the original data has been damaged.
   
   For repro purposes, can you show the code how you were decompressing the body?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] coolboywcm commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "coolboywcm (via GitHub)" <gi...@apache.org>.
coolboywcm commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1665340474

   > @coolboywcm APISIX doesn't tamper with the response and passes it as it is. Can you confirm if the response sent by your upstream directly to see if the this invalid response is coming from upstream?
   
   I'm  really sure it's the gzip response mode of the upstream service causing garbled logs, which I've reproduced in my development environment.
   **non-gzip** mode, the upstream service code is as follows:
   
   ```
   public void test_no_gzip(){
           HttpServletResponse response =  getResponse();
           response.setContentType("application/json;charset=UTF-8");
           JSONObject result = new JSONObject();
           result.put("key1","123234");
           try {
               ServletOutputStream servletOutputStream = response.getOutputStream();
               servletOutputStream.write(result.toJSONString().getBytes("UTF-8"));
               servletOutputStream.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       
   ```
   Received log content:
   
   `{"response":{"status":200,"headers":{"access-control-allow-headers":"Content-Type,X-Requested-With,withCredentials","connection":"close","date":"Fri, 04 Aug 2023 09:39:26 GMT","access-control-allow-origin":"*","server":"APISIX\/3.1.0","content-type":"application\/json;charset=UTF-8","access-control-allow-methods":"GET, POST","content-length":"17"},"body":"{\"key1\":\"123234\"}","size":333}}
   `
   
   **gzip** mode, the upstream service code is as follows:
   
   ```
       public void test_gzip(){
           HttpServletResponse response =  getResponse();
           response.setHeader("Content-Encoding", "gzip");
           response.setContentType("application/json;charset=UTF-8");
           JSONObject result = new JSONObject();
           result.put("key1","123234");
           try {
               GZIPOutputStream gzipOutputStream = new GZIPOutputStream(response.getOutputStream());
               gzipOutputStream.write(result.toJSONString().getBytes("UTF-8"));
               gzipOutputStream.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   ```
       
    Received log content:   
   
   ` {"response":{"status":200,"headers":{"connection":"close","access-control-allow-origin":"*","server":"APISIX\/3.1.0","content-type":"application\/json;charset=UTF-8","date":"Fri, 04 Aug 2023 09:47:27 GMT","content-encoding":"gzip","access-control-allow-headers":"Content-Type,X-Requested-With,withCredentials","access-control-allow-methods":"GET, POST","content-length":"37"},"body":"\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000","size":377}}`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1670745342

   @coolboywcm If you're trying to log the responses then it's suggested that you set the `Accept-encoding` as something other than gzip as only json responses are supported for structured logging. You can close this issue if your query is resolved


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] coolboywcm commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "coolboywcm (via GitHub)" <gi...@apache.org>.
coolboywcm commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1664805363

   > @coolboywcm This is not a bug but a feature request. Currently the logger doesn't decompress the compressed body and logs the body as it is.
   
   I tried to decompress and convert the response-body of http-logger into a byte stream, but it failed. During the process of converting the gzip content into a string forcibly by http-logger, the original data has been damaged.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1665257175

   @coolboywcm APISIX doesn't tamper with the response and passes it as it is. Can you confirm if the response sent by your upstream directly to see if the this invalid response is coming from upstream?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] coolboywcm commented on issue #9961: bug: http-logger reseponse body decode fail

Posted by "coolboywcm (via GitHub)" <gi...@apache.org>.
coolboywcm commented on issue #9961:
URL: https://github.com/apache/apisix/issues/9961#issuecomment-1672376416

   I tried to modify the source code, import lua_zlib model, decompressed resp_body before generating the log, and solved this problem.
   The code is as follows:
   
   ```
   local zlib  = require("zlib")
   function _M.collect_body(conf, ctx)
     ......
     local headers = ngx_header
     if headers["content-encoding"] == "gzip" then
         local inflated_body, inflate_err = zlib.inflate()(final_body, "finish")
         .........
        ctx.resp_body = inflated_body
        .........
    end
   ...........
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] Revolyssup closed issue #9961: bug: http-logger reseponse body decode fail

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup closed issue #9961: bug: http-logger reseponse body decode fail
URL: https://github.com/apache/apisix/issues/9961


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org