From 4edbd8c9ea191a4ded46c8b370a4d61fb03d61eb Mon Sep 17 00:00:00 2001 From: levy liu Date: Mon, 20 Mar 2023 14:23:47 +0800 Subject: [PATCH 1/4] feat: support variable when rewrite header in proxy rewrite plugin --- apisix/core/utils.lua | 41 +++++++ apisix/plugins/proxy-rewrite.lua | 27 ++++- docs/en/latest/plugins/proxy-rewrite.md | 4 +- docs/zh/latest/plugins/proxy-rewrite.md | 4 +- t/plugin/proxy-rewrite3.t | 149 ++++++++++++++++++++++++ 5 files changed, 216 insertions(+), 9 deletions(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 01c8b34c8503..9e1658753b5f 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -335,4 +335,45 @@ end _M.resolve_var = resolve_var +local resolve_var_with_captures +do + local _captures + -- escape is not supported very well, like there si a redundant '\' after escape "$1" + local pat = [[ (?= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed + + + +=== TEST 28: hit +--- request +GET /test/plugin/proxy/rewrite HTTP/1.1 +--- response_headers +X-Request-ID: plugin/proxy/rewrite + + + +=== TEST 29: use variables in header when not matched regex_uri +--- 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, + [[{ + "uri": "/echo*", + "plugins": { + "proxy-rewrite": { + "regex_uri": ["^/test/(.*)/(.*)/(.*)", "/echo"], + "headers": { + "add": { + "X-Request-ID": "$1/$2/$3" + } + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed + + + +=== TEST 30: hit +--- request +GET /echo HTTP/1.1 +--- more_headers +X-Foo: Foo +--- response_headers +X-Foo: Foo + + + +=== TEST 31: use variables in headers when captured by regex_uri +--- 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, + [[{ + "uri": "/test/*", + "plugins": { + "proxy-rewrite": { + "regex_uri": ["^/test/(not_matched)?.*", "/echo"], + "headers": { + "add": { + "X-Request-ID": "test1/$1/$2/test2" + } + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed + + + +=== TEST 32: hit +--- request +GET /test/plugin/proxy/rewrite HTTP/1.1 +--- response_headers +X-Request-ID: test1///test2 From a50cd9c4ca2acafab365a6436227dabff9f12191 Mon Sep 17 00:00:00 2001 From: levy liu Date: Mon, 20 Mar 2023 14:35:59 +0800 Subject: [PATCH 2/4] style: minor tweaks --- apisix/plugins/proxy-rewrite.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua index 38ad0c62e413..7a2c4045c7ad 100644 --- a/apisix/plugins/proxy-rewrite.lua +++ b/apisix/plugins/proxy-rewrite.lua @@ -340,7 +340,8 @@ function _M.rewrite(conf, ctx) local field_cnt = #hdr_op.add for i = 1, field_cnt, 2 do - local val = core.utils.resolve_var_with_captures(hdr_op.add[i + 1], ctx.proxy_rewrite_regex_uri_captures) + local val = core.utils.resolve_var_with_captures(hdr_op.add[i + 1], + ctx.proxy_rewrite_regex_uri_captures) val = core.utils.resolve_var(val, ctx.var) local header = hdr_op.add[i] core.request.add_header(ctx, header, val) @@ -348,7 +349,8 @@ function _M.rewrite(conf, ctx) local field_cnt = #hdr_op.set for i = 1, field_cnt, 2 do - local val = core.utils.resolve_var_with_captures(hdr_op.set[i + 1], ctx.proxy_rewrite_regex_uri_captures) + local val = core.utils.resolve_var_with_captures(hdr_op.set[i + 1], + ctx.proxy_rewrite_regex_uri_captures) val = core.utils.resolve_var(val, ctx.var) core.request.set_header(ctx, hdr_op.set[i], val) end From 79f924199f6b04aa1797a15246810370ecdd72b3 Mon Sep 17 00:00:00 2001 From: levy liu Date: Thu, 23 Mar 2023 00:02:21 +0800 Subject: [PATCH 3/4] refactor: minor tweaks --- apisix/core/utils.lua | 2 +- apisix/plugins/proxy-rewrite.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 9e1658753b5f..70531e7c1fa9 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -338,7 +338,7 @@ _M.resolve_var = resolve_var local resolve_var_with_captures do local _captures - -- escape is not supported very well, like there si a redundant '\' after escape "$1" + -- escape is not supported very well, like there is a redundant '\' after escape "$1" local pat = [[ (? Date: Thu, 23 Mar 2023 09:41:01 +0800 Subject: [PATCH 4/4] refactor: minor tweaks --- apisix/plugins/proxy-rewrite.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua index 0fb49842c9d4..8b1df5aaa876 100644 --- a/apisix/plugins/proxy-rewrite.lua +++ b/apisix/plugins/proxy-rewrite.lua @@ -293,11 +293,9 @@ function _M.rewrite(conf, ctx) end local m, err = re_match(upstream_uri, conf.regex_uri[1], "jo") - if not m then - if err then - core.log.error("match error in proxy-rewrite plugin, please check: ", err) - return 500 - end + if not m and err then + core.log.error("match error in proxy-rewrite plugin, please check: ", err) + return 500 end ctx.proxy_rewrite_regex_uri_captures = m