Skip to content

Commit

Permalink
fix: hold_body_chunk should use seperate buffer for each plugin in ca…
Browse files Browse the repository at this point in the history
…se of pollution (#9266)
  • Loading branch information
Sn0rt committed Apr 13, 2023
1 parent 9098f8c commit f39cadd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
13 changes: 9 additions & 4 deletions apisix/core/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions t/core/response.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion t/plugin/grpc-transcode2.t
Original file line number Diff line number Diff line change
Expand Up @@ -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"/
Expand Down

0 comments on commit f39cadd

Please sign in to comment.