Skip to content
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

Merged

Conversation

lilien1010
Copy link
Member

@lilien1010 lilien1010 commented Feb 10, 2021

What this PR does / why we need it:

Allow user modify http reponse with conditional vars.

Eg:

  1. we can rewrite response code 502 to 504 with this plugin, rather than write a new plugin
  2. add special http header for request witch specific vars
  3. dropif directives when transfer from nginx to APISIX

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@lilien1010 lilien1010 changed the title support conditinal response rewrite feature:support conditinal response rewrite Feb 10, 2021
@lilien1010 lilien1010 changed the title feature:support conditinal response rewrite feature:support conditional response rewrite Feb 11, 2021
@lilien1010 lilien1010 changed the title feature:support conditional response rewrite feat: support conditional response rewrite Feb 12, 2021
@lilien1010 lilien1010 force-pushed the support-conditional-response-rewrite branch from c0f146e to 32bd68d Compare February 12, 2021 16:04
@lilien1010 lilien1010 marked this pull request as ready for review February 12, 2021 17:21
@moonming
Copy link
Member

Can this function be realized by plugin orchestration?

@membphis
Copy link
Member

This PR only changes the response HTTP status, and we still cannot use proxy_next_upstream.

@lilien1010
Copy link
Member Author

@membphis @moonming
the 504/502 example might not quite reasonable
my idea is that vars allows do conditional overwrite logic(mainly rewrite header), which is useful for users to transfer their dated mannuly maintained nginx.conf that with numberous if directives to APISIX.

also the plugin orchestration is a little bit obscure for someone new to APISIX.

@lilien1010
Copy link
Member Author

the idea to achieve this feature was inspired by friends in Tencent while they are moving their old nginx to APISIX.

@@ -107,6 +145,12 @@ function _M.body_filter(conf, ctx)
end

function _M.header_filter(conf, ctx)

Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return true
end


do

function _M.body_filter(conf, ctx)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return true
end


do

function _M.body_filter(conf, ctx)

local ok = vars_matched(conf, ctx)
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if conf.vars then
local ok, err = expr.new(conf.vars)
if not ok then
return nil, "failed to validate the 'vars' expression: " .. err
Copy link
Member

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.

Copy link
Member Author

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}
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space found

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -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). |
Copy link
Contributor

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".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Member

@spacewander spacewander left a 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.

@membphis
Copy link
Member

membphis commented Mar 1, 2021

@lilien1010 please merge the master branch: Conflicting files

@lilien1010 lilien1010 force-pushed the support-conditional-response-rewrite branch from 629be80 to c712dea Compare March 3, 2021 02:56
@lilien1010 lilien1010 merged commit 66539b3 into apache:master Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants