You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/05/25 02:17:43 UTC

[apisix] branch master updated: feat: support appending query string in redirect (#4298)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new a1872a4  feat: support appending query string in redirect (#4298)
a1872a4 is described below

commit a1872a4842b25120502dce97c00dd9cdcefcc758
Author: Gary-Airwallex <ga...@airwallex.com>
AuthorDate: Tue May 25 10:17:36 2021 +0800

    feat: support appending query string in redirect (#4298)
---
 apisix/plugins/redirect.lua        | 13 +++++-
 docs/en/latest/plugins/redirect.md |  1 +
 docs/zh/latest/plugins/redirect.md |  1 +
 t/plugin/redirect.t                | 90 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/apisix/plugins/redirect.lua b/apisix/plugins/redirect.lua
index c2c2aec..f5e5f1d 100644
--- a/apisix/plugins/redirect.lua
+++ b/apisix/plugins/redirect.lua
@@ -51,7 +51,8 @@ local schema = {
             }
         },
         http_to_https = {type = "boolean"},
-        encode_uri = {type = "boolean", default = false}
+        encode_uri = {type = "boolean", default = false},
+        append_query_string = {type = "boolean", default = false},
     },
     oneOf = {
         {required = {"uri"}},
@@ -191,8 +192,8 @@ function _M.rewrite(conf, ctx)
             return
         end
 
+        local index = str_find(new_uri, "?")
         if conf.encode_uri then
-            local index = str_find(new_uri, "?")
             if index then
                 new_uri = core.utils.uri_safe_encode(str_sub(new_uri, 1, index-1)) ..
                           str_sub(new_uri, index)
@@ -201,6 +202,14 @@ function _M.rewrite(conf, ctx)
             end
         end
 
+        if conf.append_query_string and ctx.var.is_args == "?" then
+            if index then
+                new_uri = new_uri .. "&" .. (ctx.var.args or "")
+            else
+                new_uri = new_uri .. "?" .. (ctx.var.args or "")
+            end
+        end
+
         core.response.set_header("Location", new_uri)
         return ret_code
     end
diff --git a/docs/en/latest/plugins/redirect.md b/docs/en/latest/plugins/redirect.md
index 9ce47bc..4b1a016 100644
--- a/docs/en/latest/plugins/redirect.md
+++ b/docs/en/latest/plugins/redirect.md
@@ -42,6 +42,7 @@ URI redirect.
 | regex_uri | array[string] | optional    |         |                   | Use regular expression to match URL from client, when the match is successful, the URL template will be redirected to. If the match is not successful, the URL from the client will be forwarded to the upstream. Only one of `uri` and `regex_uri` can be exist. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL t [...]
 | ret_code      | integer | optional    | 302     |  [200, ...]     | Response code                                                                                                                                                                                                                                                                                                                                                                                                                      |
 | encode_uri    | boolean | optional    | false   |       | When set to `true` the uri in `Location` header will be encoded  as per [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) |
+| append_query_string    | boolean | optional    | false   |       | When set to `true`, add the query string from the original request to the location header. If the configured `uri` / `regex_uri` already contains a query string, the query string from request will be appended to that after an `&`. Caution: don't use this if you've already handled the query string, e.g. with nginx variable $request_uri, to avoid duplicates. |
 
 Only one of `http_to_https`, `uri` or `regex_uri` can be specified.
 
diff --git a/docs/zh/latest/plugins/redirect.md b/docs/zh/latest/plugins/redirect.md
index 1408e68..fb62635 100644
--- a/docs/zh/latest/plugins/redirect.md
+++ b/docs/zh/latest/plugins/redirect.md
@@ -32,6 +32,7 @@ URI 重定向插件。
 | regex_uri | array[string] | 可选        |         |                   | 转发到上游的新 `uri` 地址, 使用正则表达式匹配来自客户端的 `uri`,当匹配成功后使用模板替换发送重定向到客户端, 未匹配成功时将客户端请求的 `uri` 转发至上游。`uri` 和 `regex_uri` 不可以同时存在。例如:["^/iresty/(.*)/(.*)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的 `uri` 正则表达式,第二个元素代表匹配成功后发送重定向到客户端的 `uri` 模板。 |
 | ret_code      | integer | 可选        | 302     | [200, ...] | 请求响应码                                                                                                                                                                                                                    |
 | encode_uri    | boolean | 可选        | false   |       | 当设置为 `true` 时,对返回的 `Location` header进行编码,编码格式参考 [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) |
+| append_query_string    | boolean | optional    | false   |       | 当设置为`true`时,将请求url的query部分添加到Location里。如果在`uri`或`regex_uri`中配置了query, 那么请求的query会被追加在这个query后,以`&`分隔。 注意:如果已经处理了query,比如使用了nginx变量`$request_uri`,那么启用此功能会造成query重复 |
 
 `http_to_https`,`uri` 或 `regex_uri` 三个中只能配置一个。
 
diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t
index 20e5b4a..3c47e97 100644
--- a/t/plugin/redirect.t
+++ b/t/plugin/redirect.t
@@ -910,3 +910,93 @@ Location: /hello/with%20space
 --- error_code: 301
 --- no_error_log
 [error]
+
+
+
+=== TEST 37: add plugin with new uri: $uri (append_query_string = true)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "redirect": {
+                            "uri": "$uri",
+                            "ret_code": 302,
+                            "append_query_string": true
+                        }
+                    },
+                    "uri": "/hello"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 38: redirect
+--- request
+GET /hello?name=json
+--- response_headers
+Location: /hello?name=json
+--- error_code: 302
+--- no_error_log
+[error]
+
+
+
+=== TEST 39: add plugin with new uri: $uri?type=string (append_query_string = true)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "redirect": {
+                            "uri": "$uri?type=string",
+                            "ret_code": 302,
+                            "append_query_string": true
+                        }
+                    },
+                    "uri": "/hello"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 40: redirect
+--- request
+GET /hello?name=json
+--- response_headers
+Location: /hello?type=string&name=json
+--- error_code: 302
+--- no_error_log
+[error]