Skip to content

Commit

Permalink
feat: support rewrite request body in external plugins (#9990)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubikplanet authored Sep 22, 2023
1 parent fac2210 commit 88da2ec
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions apisix/plugins/ext-plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ local ipairs = ipairs
local pairs = pairs
local tostring = tostring
local type = type
local ngx = ngx


local events_list
Expand Down Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions t/lib/ext-plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions t/plugin/ext-plugin/http-req-call.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 88da2ec

Please sign in to comment.