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

[GitHub] [apisix] Sn0rt commented on pull request #9266: fix: hold_body_chunk can not be used in more than one plugin

Sn0rt commented on PR #9266:
URL: https://github.com/apache/apisix/pull/9266#issuecomment-1504722636

   # how to reprorude the duplicate log
   
   to create a new test file.
   
   ```lua
   #
   # Licensed to the Apache Software Foundation (ASF) under one or more
   # contributor license agreements.  See the NOTICE file distributed with
   # this work for additional information regarding copyright ownership.
   # The ASF licenses this file to You under the Apache License, Version 2.0
   # (the "License"); you may not use this file except in compliance with
   # the License.  You may obtain a copy of the License at
   #
   #     http://www.apache.org/licenses/LICENSE-2.0
   #
   # Unless required by applicable law or agreed to in writing, software
   # distributed under the License is distributed on an "AS IS" BASIS,
   # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   # See the License for the specific language governing permissions and
   # limitations under the License.
   #
   use t::APISIX 'no_plan';
   
   no_long_string();
   no_shuffle();
   no_root_location();
   
   add_block_preprocessor(sub {
       my ($block) = @_;
   
       if (!$block->request) {
           $block->set_value("request", "GET /t");
       }
   });
   
   run_tests;
   
   __DATA__
   
   === TEST 1: simulate simple SOAP proxy
   --- config
   location /demo {
       content_by_lua_block {
           local core = require("apisix.core")
           local body = core.request.get_body()
           local xml2lua = require("xml2lua")
           local xmlhandler = require("xmlhandler.tree")
           local handler = xmlhandler:new()
           local parser = xml2lua.parser(handler)
           parser:parse(body)
   
           ngx.print(string.format([[
   <SOAP-ENV:Envelope
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header/>
       <SOAP-ENV:Body>
           <ns2:getCountryResponse
               xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
               <ns2:country>
                   <ns2:name>%s</ns2:name>
                   <ns2:population>46704314</ns2:population>
                   <ns2:capital>Madrid</ns2:capital>
                   <ns2:currency>EUR</ns2:currency>
               </ns2:country>
           </ns2:getCountryResponse>
       </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>
           ]], handler.root["soap-env:Envelope"]["soap-env:Body"]["ns0:getCountryRequest"]["ns0:name"]))
       }
   }
       location /t {
           content_by_lua_block {
               local t = require("lib.test_admin")
   
               local req_template = ngx.encode_base64[[
   <?xml version="1.0"?>
   <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Body>
     <ns0:getCountryRequest xmlns:ns0="http://spring.io/guides/gs-producing-web-service">
      <ns0:name>{{_escape_xml(name)}}</ns0:name>
     </ns0:getCountryRequest>
    </soap-env:Body>
   </soap-env:Envelope>
               ]]
   
               local rsp_template = ngx.encode_base64[[
   {% if Envelope.Body.Fault == nil then %}
   {
      "status":"{{_ctx.var.status}}",
      "currency":"{{Envelope.Body.getCountryResponse.country.currency}}",
      "population":{{Envelope.Body.getCountryResponse.country.population}},
      "capital":"{{Envelope.Body.getCountryResponse.country.capital}}",
      "name":"{{Envelope.Body.getCountryResponse.country.name}}"
   }
   {% else %}
   {
      "message":{*_escape_json(Envelope.Body.Fault.faultstring[1])*},
      "code":"{{Envelope.Body.Fault.faultcode}}"
      {% if Envelope.Body.Fault.faultactor ~= nil then %}
      , "actor":"{{Envelope.Body.Fault.faultactor}}"
      {% end %}
   }
   {% end %}
               ]]
   
               local code, body = t.test('/apisix/admin/routes/1',
                   ngx.HTTP_PUT,
                   string.format([[{
                       "uri": "/ws",
                       "plugins": {
                           "proxy-rewrite": {
                               "uri": "/demo"
                           },
                           "body-transformer": {
                               "request": {
                                   "template": "%s"
                               },
                               "response": {
                                   "input_format": "xml",
                                   "template": "%s"
                               }
                           }
                       },
                       "upstream": {
                           "type": "roundrobin",
                           "nodes": {
                               "127.0.0.1:%d": 1
                           }
                       }
                   }]], req_template, rsp_template, ngx.var.server_port)
               )
   
               if code >= 300 then
                   ngx.status = code
                   return
               end
   
               local code, body = t.test('/apisix/admin/global_rules/1',
                   ngx.HTTP_PUT,
                   [[{
                       "plugins": {
                           "http-logger": {
                               "uri": "http://127.0.0.1:1980/log",
                               "batch_max_size": 1,
                               "include_resp_body": true
                           }
                       }
                   }]]
                   )
   
               if code >= 300 then
                   ngx.status = code
                   return
               end
   
               ngx.sleep(0.5)
   
               local core = require("apisix.core")
               local http = require("resty.http")
               local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/ws"
               local body = [[{"name": "Spain"}]]
               local opt = {method = "POST", body = body, headers = {["Content-Type"] = "application/json"}}
               local httpc = http.new()
               local res = httpc:request_uri(uri, opt)
               assert(res.status == 200)
               local data1 = core.json.decode(res.body)
               local data2 = core.json.decode[[{"status":"200","currency":"EUR","population":46704314,"capital":"Madrid","name":"Spain"}]]
               assert(core.json.stably_encode(data1), core.json.stably_encode(data2))
           }
       }
   ```
   
   
   try to run test file and ignore the output of test.
   
   ```shell
   $ prove -I. -I../test-nginx/inc -I../test-nginx/lib -r t/plugin/body-transformer.t
   ```
   
   to check the log of apisix. 
   and you will find the log of response body has been duplicated
   
   ```
   23/04/12 06:09:41 [info] 52938#52938: *158 [lua] balancer.lua:384: run(): proxy request to 127.0.0.1:1984 while connecting to upstream, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:180: hold_body_chunk(): nil while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:181: hold_body_chunk(): Stack trace
   stack traceback:
   	/home/ubuntu/apisix/apisix/core/response.lua:181: in function 'hold_body_chunk'
   	/home/ubuntu/apisix/apisix/utils/log-util.lua:298: in function 'collect_body'
   	/home/ubuntu/apisix/apisix/plugins/http-logger.lua:153: in function 'phase_func'
   	/home/ubuntu/apisix/apisix/plugin.lua:1131: in function 'run_plugin'
   	/home/ubuntu/apisix/apisix/plugin.lua:1164: in function 'run_global_rules'
   	/home/ubuntu/apisix/apisix/init.lua:402: in function 'common_phase'
   	/home/ubuntu/apisix/apisix/init.lua:746: in function 'http_body_filter_phase'
   	body_filter_by_lua:2: in main chunk while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:182: hold_body_chunk(): null while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [info] 52938#52938: *158 [lua] response.lua:183: hold_body_chunk():  chunk <SOAP-ENV:Envelope
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header/>
       <SOAP-ENV:Body>
           <ns2:getCountryResponse
               xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
               <ns2:country>
                   <ns2:name>Spain</ns2:name>
                   <ns2:population>46704314</ns2:population>
                   <ns2:capital>Madrid</ns2:capital>
                   <ns2:currency>EUR</ns2:currency>
               </ns2:country>
           </ns2:getCountryResponse>
       </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>
            oef false while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:180: hold_body_chunk(): table: 0x7f8bb45e7d38 while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:181: hold_body_chunk(): Stack trace
   stack traceback:
   	/home/ubuntu/apisix/apisix/core/response.lua:181: in function 'hold_body_chunk'
   	/home/ubuntu/apisix/apisix/plugins/body-transformer.lua:195: in function 'phase_func'
   	/home/ubuntu/apisix/apisix/plugin.lua:1131: in function 'common_phase'
   	/home/ubuntu/apisix/apisix/init.lua:746: in function 'http_body_filter_phase'
   	body_filter_by_lua:2: in main chunk while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:182: hold_body_chunk(): {"1":"<SOAP-ENV:Envelope\n    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <SOAP-ENV:Header/>\n    <SOAP-ENV:Body>\n        <ns2:getCountryResponse\n            xmlns:ns2=\"http://spring.io/guides/gs-producing-web-service\">\n            <ns2:country>\n                <ns2:name>Spain</ns2:name>\n                <ns2:population>46704314</ns2:population>\n                <ns2:capital>Madrid</ns2:capital>\n                <ns2:currency>EUR</ns2:currency>\n            </ns2:country>\n        </ns2:getCountryResponse>\n    </SOAP-ENV:Body>\n</SOAP-ENV:Envelope>\n        ","n":1} while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [info] 52938#52938: *158 [lua] response.lua:183: hold_body_chunk():  chunk <SOAP-ENV:Envelope
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header/>
       <SOAP-ENV:Body>
           <ns2:getCountryResponse
               xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
               <ns2:country>
                   <ns2:name>Spain</ns2:name>
                   <ns2:population>46704314</ns2:population>
                   <ns2:capital>Madrid</ns2:capital>
                   <ns2:currency>EUR</ns2:currency>
               </ns2:country>
           </ns2:getCountryResponse>
       </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>
            oef false while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:180: hold_body_chunk(): table: 0x7f8bb45e7d38 while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:181: hold_body_chunk(): Stack trace
   stack traceback:
   	/home/ubuntu/apisix/apisix/core/response.lua:181: in function 'hold_body_chunk'
   	/home/ubuntu/apisix/apisix/utils/log-util.lua:298: in function 'collect_body'
   	/home/ubuntu/apisix/apisix/plugins/http-logger.lua:153: in function 'phase_func'
   	/home/ubuntu/apisix/apisix/plugin.lua:1131: in function 'run_plugin'
   	/home/ubuntu/apisix/apisix/plugin.lua:1164: in function 'run_global_rules'
   	/home/ubuntu/apisix/apisix/init.lua:402: in function 'common_phase'
   	/home/ubuntu/apisix/apisix/init.lua:746: in function 'http_body_filter_phase'
   	body_filter_by_lua:2: in main chunk while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:182: hold_body_chunk(): {"1":"<SOAP-ENV:Envelope\n    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <SOAP-ENV:Header/>\n    <SOAP-ENV:Body>\n        <ns2:getCountryResponse\n            xmlns:ns2=\"http://spring.io/guides/gs-producing-web-service\">\n            <ns2:country>\n                <ns2:name>Spain</ns2:name>\n                <ns2:population>46704314</ns2:population>\n                <ns2:capital>Madrid</ns2:capital>\n                <ns2:currency>EUR</ns2:currency>\n            </ns2:country>\n        </ns2:getCountryResponse>\n    </SOAP-ENV:Body>\n</SOAP-ENV:Envelope>\n        ","2":"<SOAP-ENV:Envelope\n    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <SOAP-ENV:Header/>\n    <SOAP-ENV:Body>\n        <ns2:getCountryResponse\n            xmlns:ns2=\"http://spring.io/guides/gs-producing-web-service\">\n            <ns2:country>\n                <ns2:name>Spain</ns
 2:name>\n                <ns2:population>46704314</ns2:population>\n                <ns2:capital>Madrid</ns2:capital>\n                <ns2:currency>EUR</ns2:currency>\n            </ns2:country>\n        </ns2:getCountryResponse>\n    </SOAP-ENV:Body>\n</SOAP-ENV:Envelope>\n        ","n":2} while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [info] 52938#52938: *158 [lua] response.lua:183: hold_body_chunk():  chunk  oef true while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:180: hold_body_chunk(): nil while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:181: hold_body_chunk(): Stack trace
   stack traceback:
   	/home/ubuntu/apisix/apisix/core/response.lua:181: in function 'hold_body_chunk'
   	/home/ubuntu/apisix/apisix/plugins/body-transformer.lua:195: in function 'phase_func'
   	/home/ubuntu/apisix/apisix/plugin.lua:1131: in function 'common_phase'
   	/home/ubuntu/apisix/apisix/init.lua:746: in function 'http_body_filter_phase'
   	body_filter_by_lua:2: in main chunk while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [notice] 52938#52938: *158 [lua] response.lua:182: hold_body_chunk(): null while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [info] 52938#52938: *158 [lua] response.lua:183: hold_body_chunk():  chunk  oef true while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [error] 52938#52938: *158 [lua] body-transformer.lua:145: transform(): response template rendering: [string "context=... or {}..."]:5: attempt to index global 'Envelope' (a nil value) while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [error] 52938#52938: *158 [lua] body-transformer.lua:202: phase_func(): failed to transform response body:  while sending to client, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [info] 52938#52938: *158 [lua] log-util.lua:215: get_log_entry(): metadata: null while logging request, client: 127.0.0.1, server: localhost, request: "POST /ws HTTP/1.1", upstream: "http://127.0.0.1:1984/demo", host: "127.0.0.1:1984"
   2023/04/12 06:09:41 [info] 52938#52938: *163 [lua] http-logger.lua:91: func(): sending a batch logs to http://127.0.0.1:1980/log, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:1984
   2023/04/12 06:09:41 [warn] 52938#52938: *165 [lua] server.lua:444: go(): request log: {"apisix_latency":99.999998092651,"client_ip":"127.0.0.1","latency":101.99999809265,"request":{"headers":{"content-length":"313","content-type":"application/json","host":"127.0.0.1:1984","user-agent":"lua-resty-http/0.16.1 (Lua) ngx_lua/10021"},"method":"POST","querystring":{},"size":167,"uri":"/ws","url":"http://127.0.0.1:1984/ws"},"response":{"body":"<SOAP-ENV:Envelope\n    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <SOAP-ENV:Header/>\n    <SOAP-ENV:Body>\n        <ns2:getCountryResponse\n            xmlns:ns2=\"http://spring.io/guides/gs-producing-web-service\">\n            <ns2:country>\n                <ns2:name>Spain</ns2:name>\n                <ns2:population>46704314</ns2:population>\n                <ns2:capital>Madrid</ns2:capital>\n                <ns2:currency>EUR</ns2:currency>\n            </ns2:country>\n        </ns2:getCountryResponse>\n    </SOAP-ENV:Bod
 y>\n</SOAP-ENV:Envelope>\n        <SOAP-ENV:Envelope\n    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <SOAP-ENV:Header/>\n    <SOAP-ENV:Body>\n        <ns2:getCountryResponse\n            xmlns:ns2=\"http://spring.io/guides/gs-producing-web-service\">\n            <ns2:country>\n                <ns2:name>Spain</ns2:name>\n                <ns2:population>46704314</ns2:population>\n                <ns2:capital>Madrid</ns2:capital>\n                <ns2:currency>EUR</ns2:currency>\n            </ns2:country>\n        </ns2:getCountryResponse>\n    </SOAP-ENV:Body>\n</SOAP-ENV:Envelope>\n        ","headers":{"connection":"close","content-type":"text/plain","date":"Wed, 12 Apr 2023 06:09:41 GMT","server":"APISIX/3.2.0","transfer-encoding":"chunked"},"size":161,"status":200},"route_id":"1","server":{"hostname":"ip-172-31-36-124","version":"3.2.0"},"service_id":"","start_time":1681279781019,"upstream":"127.0.0.1:1984","upstream_latency":2}, client: 127.0.0.1, server:
  , request: "POST /log? HTTP/1.1", host: "127.0.0.1"
   2023/04/12 06:09:41 [info] 52938#52938: *165 client 127.0.0.1 closed keepalive connection
   2023/04/12 06:09:41 [notice] 52937#52937: signal 15 (SIGTERM)
   ```


-- 
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