-
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 conditional response rewrite #3577
feat: support conditional response rewrite #3577
Conversation
c0f146e
to
32bd68d
Compare
Can this function be realized by plugin orchestration? |
This PR only changes the response HTTP status, and we still cannot use |
@membphis @moonming also the plugin orchestration is a little bit obscure for someone new to APISIX. |
the idea to achieve this feature was inspired by friends in Tencent while they are moving their old nginx to APISIX. |
apisix/plugins/response-rewrite.lua
Outdated
@@ -107,6 +145,12 @@ function _M.body_filter(conf, ctx) | |||
end | |||
|
|||
function _M.header_filter(conf, ctx) | |||
|
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.
Don't need to extra blank line
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.
fixed
apisix/plugins/response-rewrite.lua
Outdated
return true | ||
end | ||
|
||
|
||
do | ||
|
||
function _M.body_filter(conf, ctx) | ||
|
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.
Ditto
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.
fixed
apisix/plugins/response-rewrite.lua
Outdated
return true | ||
end | ||
|
||
|
||
do | ||
|
||
function _M.body_filter(conf, ctx) | ||
|
||
local ok = vars_matched(conf, ctx) |
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.
We should cache the result from header_filter.
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
apisix/plugins/response-rewrite.lua
Outdated
if conf.vars then | ||
local ok, err = expr.new(conf.vars) | ||
if not ok then | ||
return nil, "failed to validate the 'vars' expression: " .. err |
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.
Need a test to validate the vars.
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.
fixed
local plugin = require("apisix.plugins.response-rewrite") | ||
local ok, err = plugin.check_schema({ | ||
vars = { | ||
{"status","==",200} |
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.
Need a case fails to validate, as the successful case is already covered later.
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.
added
apisix/plugins/response-rewrite.lua
Outdated
@@ -107,6 +143,11 @@ function _M.body_filter(conf, ctx) | |||
end | |||
|
|||
function _M.header_filter(conf, ctx) | |||
ctx.reponse_rewrite_matched = vars_matched(conf, ctx) |
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.
Extra space found
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.
fixed
doc/plugins/response-rewrite.md
Outdated
@@ -45,6 +45,7 @@ response rewrite plugin, rewrite the content returned by the upstream as well as | |||
| body | string | optional | | | New `body` to client, and the content-length will be reset too. | | |||
| body_base64 | boolean | optional | false | | Identify if `body` in configuration need base64 decoded before rewrite to client. | | |||
| headers | object | optional | | | Set the new `headers` for client, can set up multiple. If it exists already from upstream, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. | | |||
| vars | array[] | optional | | | A DSL to evaluate with the given ngx.var. See `vars` [lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list). | |
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.
Should mention here that "if vars
is absent, rewrite operations will be executed unconditionally".
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.
added
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.
Please merge master to make CI pass.
@lilien1010 please merge the master branch: |
629be80
to
c712dea
Compare
What this PR does / why we need it:
Allow user modify http reponse with conditional
vars
.Eg:
vars
if
directives when transfer from nginx to APISIXPre-submission checklist: