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. |
| end | ||
|
|
||
| function _M.header_filter(conf, ctx) | ||
|
|
There was a problem hiding this comment.
Don't need to extra blank line
| do | ||
|
|
||
| function _M.body_filter(conf, ctx) | ||
|
|
|
|
||
| function _M.body_filter(conf, ctx) | ||
|
|
||
| local ok = vars_matched(conf, ctx) |
There was a problem hiding this comment.
We should cache the result from header_filter.
| 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.
Need a test to validate the vars.
| local plugin = require("apisix.plugins.response-rewrite") | ||
| local ok, err = plugin.check_schema({ | ||
| vars = { | ||
| {"status","==",200} |
There was a problem hiding this comment.
Need a case fails to validate, as the successful case is already covered later.
| end | ||
|
|
||
| function _M.header_filter(conf, ctx) | ||
| ctx.reponse_rewrite_matched = vars_matched(conf, ctx) |
| | 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.
Should mention here that "if vars is absent, rewrite operations will be executed unconditionally".
spacewander
left a comment
There was a problem hiding this comment.
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:
varsifdirectives when transfer from nginx to APISIXPre-submission checklist: