kingluo commented on issue #9228:
URL: https://github.com/apache/apisix/issues/9228#issuecomment-1495272975

   @leslie-tsang
   IMO, the best solution is to export the plugin_name in the `ctx` when 
running the plugin, so that `hold_body_chunk()` could collect the body in the 
buffer bound to the plugin to avoid pollution.
   
   **Tested patch:**
   
   ```diff
   diff --git a/apisix/core/response.lua b/apisix/core/response.lua
   index b934d94b..c7eebdc3 100644
   --- a/apisix/core/response.lua
   +++ b/apisix/core/response.lua
   @@ -178,13 +178,15 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
        local body_buffer
        local chunk, eof = arg[1], arg[2]
        if type(chunk) == "string" and chunk ~= "" then
   -        body_buffer = ctx._body_buffer
   +        if not ctx._body_buffer then
   +            ctx._body_buffer = {}
   +        end
            if not body_buffer then
                body_buffer = {
                    chunk,
                    n = 1
                }
   -            ctx._body_buffer = body_buffer
   +            ctx._body_buffer[ctx._plugin_name] = body_buffer
            else
                local n = body_buffer.n + 1
                body_buffer.n = n
   @@ -193,13 +195,13 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
        end
    
        if eof then
   -        body_buffer = ctx._body_buffer
   +        body_buffer = ctx._body_buffer[ctx._plugin_name]
            if not body_buffer then
                return chunk
            end
    
            body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
   -        ctx._body_buffer = nil
   +        ctx._body_buffer[ctx._plugin_name] = nil
            return body_buffer
        end
    
   diff --git a/apisix/plugin.lua b/apisix/plugin.lua
   index 3b5cecac..a7d55db7 100644
   --- a/apisix/plugin.lua
   +++ b/apisix/plugin.lua
   @@ -1091,7 +1091,9 @@ function _M.run_plugin(phase, plugins, api_ctx)
                    end
    
                    plugin_run = true
   +                api_ctx._plugin_name = plugins[i]["name"]
                    local code, body = phase_func(conf, api_ctx)
   +                api_ctx._plugin_name = nil
                    if code or body then
                        if is_http then
                            if code >= 400 then
   @@ -1126,7 +1128,9 @@ function _M.run_plugin(phase, plugins, api_ctx)
            local conf = plugins[i + 1]
            if phase_func and meta_filter(api_ctx, plugins[i]["name"], conf) 
then
                plugin_run = true
   +            api_ctx._plugin_name = plugins[i]["name"]
                phase_func(conf, api_ctx)
   +            api_ctx._plugin_name = nil
            end
        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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to