diff --git a/apisix/core/response.lua b/apisix/core/response.lua index b934d94b93af..cfbac1467341 100644 --- a/apisix/core/response.lua +++ b/apisix/core/response.lua @@ -177,14 +177,19 @@ end function _M.hold_body_chunk(ctx, hold_the_copy) local body_buffer local chunk, eof = arg[1], arg[2] + + if not ctx._body_buffer then + ctx._body_buffer = {} + end + if type(chunk) == "string" and chunk ~= "" then - body_buffer = ctx._body_buffer + body_buffer = ctx._body_buffer[ctx._plugin_name] 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 +198,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 7926e29862c7..e5e04dc39832 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -1093,7 +1093,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 @@ -1128,7 +1130,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 diff --git a/t/core/response.t b/t/core/response.t index c7cb92a56d56..dc748a7ad025 100644 --- a/t/core/response.t +++ b/t/core/response.t @@ -188,6 +188,7 @@ aaa: } body_filter_by_lua_block { local core = require("apisix.core") + ngx.ctx._plugin_name = "test" local final_body = core.response.hold_body_chunk(ngx.ctx) if not final_body then return diff --git a/t/plugin/grpc-transcode2.t b/t/plugin/grpc-transcode2.t index 0f7cc8d0ba99..da91d3ceb265 100644 --- a/t/plugin/grpc-transcode2.t +++ b/t/plugin/grpc-transcode2.t @@ -486,7 +486,7 @@ GET /grpc_plus?a=1&b=2 --- response_body eval qr/\{"result":3\}/ --- error_log eval -qr/request log: \{.*body":\"\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003"/ +qr/request log: \{.*body":\"\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003"/