From 380f762f30fb845893c1f46393c4de6e57b808a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Fri, 4 Mar 2022 14:56:02 +0800 Subject: [PATCH] fix(wasm): ensure the process body flag is independent (#6496) --- apisix/wasm.lua | 7 +++++- t/wasm/request-body.t | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/apisix/wasm.lua b/apisix/wasm.lua index 415e7fd6..d4060893 100644 --- a/apisix/wasm.lua +++ b/apisix/wasm.lua @@ -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) diff --git a/t/wasm/request-body.t b/t/wasm/request-body.t index 96d32175..9e056b0b 100644 --- a/t/wasm/request-body.t +++ b/t/wasm/request-body.t @@ -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