-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support variable when rewrite header in proxy rewrite plugin #9112
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ local ipairs = ipairs | |
local ngx = ngx | ||
local type = type | ||
local re_sub = ngx.re.sub | ||
local re_match = ngx.re.match | ||
local sub_str = string.sub | ||
local str_find = core.string.find | ||
|
||
|
@@ -41,6 +42,10 @@ local lrucache = core.lrucache.new({ | |
type = "plugin", | ||
}) | ||
|
||
core.ctx.register_var("proxy_rewrite_regex_uri_captures", function(ctx) | ||
return ctx.proxy_rewrite_regex_uri_captures | ||
soulbird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end) | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
|
@@ -257,6 +262,7 @@ do | |
return re_sub(s, [[\?]], "%3F", "jo") | ||
end | ||
|
||
|
||
function _M.rewrite(conf, ctx) | ||
for _, name in ipairs(upstream_names) do | ||
if conf[name] then | ||
|
@@ -278,15 +284,24 @@ function _M.rewrite(conf, ctx) | |
|
||
local uri, _, err = re_sub(upstream_uri, conf.regex_uri[1], | ||
conf.regex_uri[2], "jo") | ||
if uri then | ||
upstream_uri = uri | ||
else | ||
if not uri then | ||
local msg = "failed to substitute the uri " .. ctx.var.uri .. | ||
" (" .. conf.regex_uri[1] .. ") with " .. | ||
conf.regex_uri[2] .. " : " .. err | ||
core.log.error(msg) | ||
return 500, {message = msg} | ||
end | ||
|
||
local m, err = re_match(upstream_uri, conf.regex_uri[1], "jo") | ||
if not m then | ||
if err then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can merge the two conditions above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how? use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #9112 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done
leslie-tsang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
core.log.error("match error in proxy-rewrite plugin, please check: ", err) | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to return a 503 for this error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer 500, it's an unexpected condition |
||
end | ||
end | ||
ctx.proxy_rewrite_regex_uri_captures = m | ||
|
||
upstream_uri = uri | ||
end | ||
|
||
if not conf.use_real_request_uri_unsafe then | ||
|
@@ -325,14 +340,18 @@ function _M.rewrite(conf, ctx) | |
|
||
local field_cnt = #hdr_op.add | ||
for i = 1, field_cnt, 2 do | ||
local val = core.utils.resolve_var(hdr_op.add[i + 1], ctx.var) | ||
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) | ||
end | ||
|
||
local field_cnt = #hdr_op.set | ||
for i = 1, field_cnt, 2 do | ||
local val = core.utils.resolve_var(hdr_op.set[i + 1], ctx.var) | ||
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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done