From 88da2ecdd2a331d62f4c6d9849910d164ad33ead Mon Sep 17 00:00:00 2001 From: Weifeng Ding <1106628276@qq.com> Date: Sat, 23 Sep 2023 01:15:37 +0800 Subject: [PATCH] feat: support rewrite request body in external plugins (#9990) --- apisix/plugins/ext-plugin/init.lua | 8 ++++ rockspec/apisix-master-0.rockspec | 2 +- t/lib/ext-plugin.lua | 13 +++++++ t/plugin/ext-plugin/http-req-call.t | 57 +++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua index b6fcf6fea82c..7d47bb96efbb 100644 --- a/apisix/plugins/ext-plugin/init.lua +++ b/apisix/plugins/ext-plugin/init.lua @@ -65,6 +65,7 @@ local ipairs = ipairs local pairs = pairs local tostring = tostring local type = type +local ngx = ngx local events_list @@ -655,6 +656,13 @@ local rpc_handlers = { end end + local body_len = rewrite:BodyLength() + if body_len > 0 then + local body = rewrite:BodyAsString() + ngx.req.read_body() + ngx.req.set_body_data(body) + end + local len = rewrite:RespHeadersLength() if len > 0 then local rewrite_resp_headers = {} diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec index 9d52c71827c8..ec99128f30f3 100644 --- a/rockspec/apisix-master-0.rockspec +++ b/rockspec/apisix-master-0.rockspec @@ -67,7 +67,7 @@ dependencies = { "luasec = 0.9-1", "lua-resty-consul = 0.3-2", "penlight = 1.13.1", - "ext-plugin-proto = 0.6.0", + "ext-plugin-proto = 0.6.1", "casbin = 1.41.8-1", "inspect == 3.1.1", "lualdap = 1.2.6-1", diff --git a/t/lib/ext-plugin.lua b/t/lib/ext-plugin.lua index b19a79838591..0ebf7192b804 100644 --- a/t/lib/ext-plugin.lua +++ b/t/lib/ext-plugin.lua @@ -470,6 +470,19 @@ function _M.go(case) local action = http_req_call_rewrite.End(builder) build_action(action, http_req_call_action.Rewrite) + elseif case.rewrite_request_body == true then + local len = 4 + http_req_call_rewrite.StartBodyVector(builder, len) + builder:PrependByte(string.byte("\n")) + builder:PrependByte(string.byte("c")) + builder:PrependByte(string.byte("b")) + builder:PrependByte(string.byte("a")) + local b = builder:EndVector(len) + http_req_call_rewrite.Start(builder) + http_req_call_rewrite.AddBody(builder, b) + local action = http_req_call_rewrite.End(builder) + build_action(action, http_req_call_action.Rewrite) + else http_req_call_resp.Start(builder) end diff --git a/t/plugin/ext-plugin/http-req-call.t b/t/plugin/ext-plugin/http-req-call.t index 6fc8240a7612..782dfa05c678 100644 --- a/t/plugin/ext-plugin/http-req-call.t +++ b/t/plugin/ext-plugin/http-req-call.t @@ -750,3 +750,60 @@ cat X-Resp: foo X-Req: bar X-Same: one, two + + + +=== TEST 27: add route +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin") + + local code, message, res = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/echo", + "plugins": { + "ext-plugin-pre-req": { + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + ngx.say(message) + } + } +--- response_body +passed + + + +=== TEST 28: test rewrite request body +--- request +GET /echo +--- response_body chomp +cat +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_request_body = true}) + } + } +--- response_body +abc