Skip to content

Commit 78d91d8

Browse files
fix: preserve uri args if path has not been modified (#13080)
1 parent 93116f9 commit 78d91d8

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

apisix/plugins/ext-plugin/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,10 @@ local rpc_handlers = {
706706
end
707707

708708
core.request.set_uri_args(ctx, args)
709+
end
709710

710-
if path then
711-
var.upstream_uri = path .. '?' .. var.args
712-
end
711+
if path then
712+
var.upstream_uri = path .. (var.is_args or '') .. (var.args or '')
713713
end
714714
end
715715

t/lib/ext-plugin.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ function _M.go(case)
399399
local action = http_req_call_rewrite.End(builder)
400400
build_action(action, http_req_call_action.Rewrite)
401401

402+
elseif case.rewrite_path_only then
403+
local path = builder:CreateString("/plugin_proxy_rewrite_args")
404+
http_req_call_rewrite.Start(builder)
405+
http_req_call_rewrite.AddPath(builder, path)
406+
local action = http_req_call_rewrite.End(builder)
407+
build_action(action, http_req_call_action.Rewrite)
408+
402409
elseif case.rewrite_bad_path == true then
403410
local path = builder:CreateString("/plugin_proxy_rewrite_args?a=2")
404411
http_req_call_rewrite.Start(builder)

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,3 +807,77 @@ cat
807807
}
808808
--- response_body
809809
abc
810+
811+
812+
813+
=== TEST 29: rewrite path only (preserve original query args)
814+
--- config
815+
location /t {
816+
content_by_lua_block {
817+
local json = require("toolkit.json")
818+
local t = require("lib.test_admin")
819+
820+
local code, message, res = t.test('/apisix/admin/routes/1',
821+
ngx.HTTP_PUT,
822+
[[{
823+
"uri": "/hello",
824+
"plugins": {
825+
"ext-plugin-pre-req": {
826+
}
827+
},
828+
"upstream": {
829+
"nodes": {
830+
"127.0.0.1:1980": 1
831+
},
832+
"type": "roundrobin"
833+
}
834+
}]]
835+
)
836+
837+
if code >= 300 then
838+
ngx.status = code
839+
ngx.say(message)
840+
return
841+
end
842+
843+
ngx.say(message)
844+
}
845+
}
846+
--- response_body
847+
passed
848+
849+
850+
851+
=== TEST 30: hit (original query args should be preserved when only path is rewritten)
852+
--- request
853+
GET /hello?c=foo&d=bar
854+
--- extra_stream_config
855+
server {
856+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
857+
858+
content_by_lua_block {
859+
local ext = require("lib.ext-plugin")
860+
ext.go({rewrite_path_only = true})
861+
}
862+
}
863+
--- response_body
864+
uri: /plugin_proxy_rewrite_args
865+
c: foo
866+
d: bar
867+
868+
869+
870+
=== TEST 31: hit (when no uri args are passed, there are no failures)
871+
--- request
872+
GET /hello
873+
--- extra_stream_config
874+
server {
875+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
876+
877+
content_by_lua_block {
878+
local ext = require("lib.ext-plugin")
879+
ext.go({rewrite_path_only = true})
880+
}
881+
}
882+
--- response_body
883+
uri: /plugin_proxy_rewrite_args

0 commit comments

Comments
 (0)