Skip to content

Commit 88da2ec

Browse files
authored
feat: support rewrite request body in external plugins (#9990)
1 parent fac2210 commit 88da2ec

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

apisix/plugins/ext-plugin/init.lua

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ local ipairs = ipairs
6565
local pairs = pairs
6666
local tostring = tostring
6767
local type = type
68+
local ngx = ngx
6869

6970

7071
local events_list
@@ -655,6 +656,13 @@ local rpc_handlers = {
655656
end
656657
end
657658

659+
local body_len = rewrite:BodyLength()
660+
if body_len > 0 then
661+
local body = rewrite:BodyAsString()
662+
ngx.req.read_body()
663+
ngx.req.set_body_data(body)
664+
end
665+
658666
local len = rewrite:RespHeadersLength()
659667
if len > 0 then
660668
local rewrite_resp_headers = {}

rockspec/apisix-master-0.rockspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ dependencies = {
6767
"luasec = 0.9-1",
6868
"lua-resty-consul = 0.3-2",
6969
"penlight = 1.13.1",
70-
"ext-plugin-proto = 0.6.0",
70+
"ext-plugin-proto = 0.6.1",
7171
"casbin = 1.41.8-1",
7272
"inspect == 3.1.1",
7373
"lualdap = 1.2.6-1",

t/lib/ext-plugin.lua

+13
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,19 @@ function _M.go(case)
470470
local action = http_req_call_rewrite.End(builder)
471471
build_action(action, http_req_call_action.Rewrite)
472472

473+
elseif case.rewrite_request_body == true then
474+
local len = 4
475+
http_req_call_rewrite.StartBodyVector(builder, len)
476+
builder:PrependByte(string.byte("\n"))
477+
builder:PrependByte(string.byte("c"))
478+
builder:PrependByte(string.byte("b"))
479+
builder:PrependByte(string.byte("a"))
480+
local b = builder:EndVector(len)
481+
http_req_call_rewrite.Start(builder)
482+
http_req_call_rewrite.AddBody(builder, b)
483+
local action = http_req_call_rewrite.End(builder)
484+
build_action(action, http_req_call_action.Rewrite)
485+
473486
else
474487
http_req_call_resp.Start(builder)
475488
end

t/plugin/ext-plugin/http-req-call.t

+57
Original file line numberDiff line numberDiff line change
@@ -750,3 +750,60 @@ cat
750750
X-Resp: foo
751751
X-Req: bar
752752
X-Same: one, two
753+
754+
755+
756+
=== TEST 27: add route
757+
--- config
758+
location /t {
759+
content_by_lua_block {
760+
local json = require("toolkit.json")
761+
local t = require("lib.test_admin")
762+
763+
local code, message, res = t.test('/apisix/admin/routes/1',
764+
ngx.HTTP_PUT,
765+
[[{
766+
"uri": "/echo",
767+
"plugins": {
768+
"ext-plugin-pre-req": {
769+
}
770+
},
771+
"upstream": {
772+
"nodes": {
773+
"127.0.0.1:1980": 1
774+
},
775+
"type": "roundrobin"
776+
}
777+
}]]
778+
)
779+
780+
if code >= 300 then
781+
ngx.status = code
782+
ngx.say(message)
783+
return
784+
end
785+
786+
ngx.say(message)
787+
}
788+
}
789+
--- response_body
790+
passed
791+
792+
793+
794+
=== TEST 28: test rewrite request body
795+
--- request
796+
GET /echo
797+
--- response_body chomp
798+
cat
799+
--- extra_stream_config
800+
server {
801+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
802+
803+
content_by_lua_block {
804+
local ext = require("lib.ext-plugin")
805+
ext.go({rewrite_request_body = true})
806+
}
807+
}
808+
--- response_body
809+
abc

0 commit comments

Comments
 (0)