tokers commented on a change in pull request #4152:
URL: https://github.com/apache/apisix/pull/4152#discussion_r622017204
##########
File path: apisix/plugins/redirect.lua
##########
@@ -35,10 +36,26 @@ local schema = {
properties = {
ret_code = {type = "integer", minimum = 200, default = 302},
uri = {type = "string", minLength = 2, pattern = reg},
+ regex_uri = {
+ description = "new uri that substitute from client uri " ..
+ "for upstream, lower priority than uri property",
+ type = "array",
+ maxItems = 2,
+ minItems = 2,
+ items = {
+ description = "regex uri",
+ type = "string",
+ }
+ },
http_to_https = {type = "boolean"},
},
oneOf = {
- {required = {"uri"}},
+ {
+ anyOf = {
Review comment:
What about using `oneOf` so `uri` and `regex_uri` cannot co-exist.
##########
File path: apisix/plugins/redirect.lua
##########
@@ -129,17 +161,35 @@ function _M.rewrite(conf, ctx)
end
end
- if uri and ret_code then
- local new_uri, err = concat_new_uri(uri, ctx)
- if not new_uri then
- core.log.error("failed to generate new uri by: ", uri, " error: ",
- err)
- return 500
+ if ret_code then
+ if uri then
+ local new_uri, err = concat_new_uri(uri, ctx)
+ if not new_uri then
+ local msg = "failed to generate new uri by: " .. uri .. err
+ core.log.error(msg)
+ return 500
+ end
+
+ core.response.set_header("Location", new_uri)
+ return ret_code
+ elseif regex_uri then
+ local new_uri, n, err = re_sub(ctx.var.uri, regex_uri[1],
+ regex_uri[2], "jo")
+ if not new_uri then
+ local msg = "failed to substitute the uri " .. ctx.var.uri ..
Review comment:
Use `string.format` to make it clearer.
##########
File path: apisix/plugins/redirect.lua
##########
@@ -129,17 +161,35 @@ function _M.rewrite(conf, ctx)
end
end
- if uri and ret_code then
- local new_uri, err = concat_new_uri(uri, ctx)
- if not new_uri then
- core.log.error("failed to generate new uri by: ", uri, " error: ",
- err)
- return 500
+ if ret_code then
+ if uri then
+ local new_uri, err = concat_new_uri(uri, ctx)
+ if not new_uri then
+ local msg = "failed to generate new uri by: " .. uri .. err
+ core.log.error(msg)
+ return 500
+ end
+
+ core.response.set_header("Location", new_uri)
+ return ret_code
+ elseif regex_uri then
+ local new_uri, n, err = re_sub(ctx.var.uri, regex_uri[1],
+ regex_uri[2], "jo")
Review comment:
```suggestion
local new_uri, n, err = re_sub(ctx.var.uri, regex_uri[1],
regex_uri[2], "jo")
```
##########
File path: apisix/plugins/redirect.lua
##########
@@ -79,7 +96,21 @@ end
function _M.check_schema(conf)
- return core.schema.check(schema, conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.regex_uri and #conf.regex_uri > 0 then
+ local _, _, err = re_sub("/fake_uri", conf.regex_uri[1],
+ conf.regex_uri[2], "jo")
+ if err then
+ return false, "invalid regex_uri(" .. conf.regex_uri[1] ..
+ ", " .. conf.regex_uri[2] .. "): " .. err
Review comment:
You can use `string.format` to make the error message composition
clearer.
##########
File path: apisix/plugins/redirect.lua
##########
@@ -35,10 +36,26 @@ local schema = {
properties = {
ret_code = {type = "integer", minimum = 200, default = 302},
uri = {type = "string", minLength = 2, pattern = reg},
+ regex_uri = {
+ description = "new uri that substitute from client uri " ..
Review comment:
term "new uri" is not accurate to describe the `regex_uri` as it's
actually a tuple that contains two elements.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]