spacewander commented on a change in pull request #6750:
URL: https://github.com/apache/apisix/pull/6750#discussion_r840992956
##########
File path: apisix/plugins/response-rewrite.lua
##########
@@ -126,7 +177,25 @@ function _M.body_filter(conf, ctx)
return
end
- if conf.body then
+ if conf.filters then
+
+ local body = core.response.hold_body_chunk(ctx)
+ if not body then
+ return
+ end
+
+ for _, filter in ipairs(conf.filters) do
+ if filter.scope == "once" then
+ body = re_sub(body, filter.regex, filter.replace,
filter.options)
+ else
+ body = re_gsub(body, filter.regex, filter.replace,
filter.options)
+ end
+ end
+
+ ngx.arg[1] = body
+ return
+
+ elseif conf.body then
Review comment:
We can use `if` here directly.
##########
File path: docs/en/latest/plugins/response-rewrite.md
##########
@@ -32,13 +32,20 @@ response rewrite plugin, rewrite the content returned by
the upstream as well as
## Attributes
-| Name | Type | Requirement | Default | Valid | Description
|
-| ----------- | ------- | ----------- | ------- | ---------- |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
-| status_code | integer | optional | | [200, 598] | New `status
code` to client, keep the original response code by default.
|
-| 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. The value can
contain Nginx variables in `$var` format, like `$remote_addr $balancer_ip` |
-| 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). if the
`vars` is empty, then all rewrite operations will be executed unconditionally |
+| Name | Type | Requirement | Default | Valid |
Description
|
+|-----------------|---------|-------------|---------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| status_code | integer | optional | | [200, 598] | New
`status code` to client, keep the original response code by default.
|
+| 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. The value can
contain Nginx variables in `$var` format, like `$remote_addr $balancer_ip`. |
+| 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). if the
`vars` is empty, then all rewrite operations will be executed unconditionally.
|
+| filters | array[] | optional | | | A group
of filters that modify response body by replacing one specified string by
another.
|
+| filters.regex | string | optional | | | match
pattern on response body.
|
+| filters.scope | string | optional | "one" | "one","global" |
substitution range.
|
Review comment:
Should be `once`?
##########
File path: t/plugin/response-rewrite2.t
##########
@@ -0,0 +1,516 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: add plugin with valid filters
+--- config
+ location /t {
+ content_by_lua_block {
+ local plugin = require("apisix.plugins.response-rewrite")
+ local ok, err = plugin.check_schema({
+ filters = {
+ {
+ regex = "Hello",
+ scope = "global",
+ replace = "World",
+ options = "jo"
+ }
+ }
+ })
+ if not ok then
+ ngx.say(err)
+ end
+
+ ngx.say("done")
+ }
+ }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: add plugin with invalid filter scope
+--- config
+ location /t {
+ content_by_lua_block {
+ local plugin = require("apisix.plugins.response-rewrite")
+ local ok, err = plugin.check_schema({
+ filters = {
+ {
+ regex = "Hello",
+ scope = "two",
+ replace = "World",
+ options = "jo"
+ }
+ }
+ })
+ if not ok then
+ ngx.say(err)
+ else
+ ngx.say("done")
+ end
+ }
+ }
+--- request
+GET /t
+--- response_body
+property "filters" validation failed: failed to validate item 1: property
"scope" validation failed: matches none of the enum values
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: add plugin with invalid filter empty value
+--- config
+ location /t {
+ content_by_lua_block {
+ local plugin = require("apisix.plugins.response-rewrite")
+ local ok, err = plugin.check_schema({
+ filters = {
+ {
+ regex = "",
+ replace = "world"
+ }
+ }
+ })
+ if not ok then
+ ngx.say(err)
+ else
+ ngx.say("done")
+ end
+ }
+ }
+--- request
+GET /t
+--- response_body
+invalid value as filter field regex
+--- no_error_log
+[error]
+
+
+
+=== TEST 4: add plugin with invalid filter regex options
+--- config
+ location /t {
+ content_by_lua_block {
+ local plugin = require("apisix.plugins.response-rewrite")
+ local ok, err = plugin.check_schema({
+ filters = {
+ {
+ regex = "hello",
+ options = "h"
+ }
+ }
+ })
+ if not ok then
+ ngx.say(err)
+ else
+ ngx.say("done")
+ end
+ }
+ }
+--- request
+GET /t
+--- error_code eval
Review comment:
Why it's output is unlike the previous test?
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]