Skip to content

Commit

Permalink
bugfix: keep args of uri when using proxy-write plugin. (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuaijinchao authored and moonming committed Oct 9, 2019
1 parent 91ee8e1 commit ff0d82c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lua/apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ end

do
local upstream_vars = {
uri = "upstream_uri",
scheme = "upstream_scheme",
host = "upstream_host",
upgrade = "upstream_upgrade",
Expand All @@ -75,9 +74,16 @@ function _M.rewrite(conf, ctx)
end
end

local upstream_uri = conf.uri or ctx.var.uri
if ctx.var.is_args == "?" then
ctx.var.upstream_uri = upstream_uri .. "?" .. (ctx.var.args or "")
else
ctx.var.upstream_uri = upstream_uri
end

if conf.enable_websocket then
ctx.var["upstream_upgrade"] = ctx.var["http_upgrade"]
ctx.var["upstream_connection"] = ctx.var["http_connection"]
ctx.var.upstream_upgrade = ctx.var.http_upgrade
ctx.var.upstream_connection = ctx.var.http_connection
end

-- TODO: support deleted header
Expand Down
8 changes: 8 additions & 0 deletions t/lib/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ function _M.plugin_proxy_rewrite()
ngx.say("scheme: ", ngx.var.scheme)
end

function _M.plugin_proxy_rewrite_args()
ngx.say("uri: ", ngx.var.uri)
local args = ngx.req.get_uri_args()
for k,v in pairs(args) do
ngx.say(k, ": ", v)
end
end

function _M.status()
ngx.say("ok")
end
Expand Down
99 changes: 99 additions & 0 deletions t/plugin/proxy-rewrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,102 @@ x-api-test: hello
x-real-ip: 127.0.0.1
--- no_error_log
[error]



=== TEST 20: set route(rewrite uri args)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite_args",
"enable_websocket": false
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 21: rewrite uri args
--- request
GET /hello?q=apisix&a=iresty HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite_args
q: apisix
a: iresty
--- no_error_log
[error]



=== TEST 16: set route(rewrite uri empty args)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite_args",
"enable_websocket": false
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]


=== TEST 22: rewrite uri empty args
--- request
GET /hello HTTP/1.1
--- response_body
uri: /plugin_proxy_rewrite_args
--- no_error_log
[error]

0 comments on commit ff0d82c

Please sign in to comment.