fix(wasm): ensure the process body flag is independent (#6496)

This commit is contained in:
罗泽轩 2022-03-04 14:56:02 +08:00 committed by GitHub
parent 66a5a2a44a
commit 380f762f30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -16,6 +16,7 @@
--
local core = require("apisix.core")
local support_wasm, wasm = pcall(require, "resty.proxy-wasm")
local ngx_var = ngx.var
local schema = {
@ -77,8 +78,12 @@ local function http_request_wrapper(self, conf, ctx)
end
-- $wasm_process_req_body is predefined in ngx_tpl.lua
local handle_body = ctx.var.wasm_process_req_body
local handle_body = ngx_var.wasm_process_req_body
if handle_body ~= '' then
-- reset the flag so we can use it for the next Wasm plugin
-- use ngx.var to bypass the cache
ngx_var.wasm_process_req_body = ''
local body, err = core.request.get_body()
if err ~= nil then
core.log.error(name, ": failed to get request body: ", err)

54
t/wasm/request-body.t vendored
View File

@ -42,6 +42,9 @@ wasm:
- name: wasm-request-body
priority: 7997
file: t/wasm/request-body/main.go.wasm
- name: wasm-request-body2
priority: 7996
file: t/wasm/request-body/main.go.wasm
_EOC_
$block->set_value("extra_yaml_config", $extra_yaml_config);
});
@ -150,3 +153,54 @@ hello
--- grep_error_log eval
qr/request get body: \w+/
--- grep_error_log_out
=== TEST 6: ensure the process body flag is plugin independent
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
},
"plugins": {
"wasm-request-body": {
"conf": "{\"processReqBody\":true, \"start\":1, \"size\":3}"
},
"wasm-request-body2": {
"conf": "{\"processReqBody\":false, \"start\":2, \"size\":3}"
}
}
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 7: hit
--- request
POST /hello
hello
--- grep_error_log eval
qr/request get body: \w+/
--- grep_error_log_out
request get body: ell