You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by mo...@apache.org on 2023/09/22 17:15:43 UTC

[apisix] branch master updated: feat: support rewrite request body in external plugins (#9990)

This is an automated email from the ASF dual-hosted git repository.

monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 88da2ecdd feat: support rewrite request body in external plugins (#9990)
88da2ecdd is described below

commit 88da2ecdd2a331d62f4c6d9849910d164ad33ead
Author: Weifeng Ding <11...@qq.com>
AuthorDate: Sat Sep 23 01:15:37 2023 +0800

    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 b6fcf6fea..7d47bb96e 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 9d52c7182..ec99128f3 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 b19a79838..0ebf7192b 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 6fc8240a7..782dfa05c 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