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 88b92ae  feat: support dash in args (#4519) (#4676)
88b92ae is described below

commit 88b92ae9d0048313e3e187b3bd0d8c0815eaddb5
Author: ken zhou <[email protected]>
AuthorDate: Fri Aug 6 17:08:17 2021 +0800

    feat: support dash in args (#4519) (#4676)
    
    Co-authored-by: Alex Zhang <[email protected]>
    Co-authored-by: tzssangglass <[email protected]>
    Co-authored-by: jianzhou <[email protected]>
---
 apisix/admin/init.lua |  4 ++++
 apisix/core/ctx.lua   | 11 +++++++++++
 t/core/ctx.t          |  6 ++----
 t/core/ctx2.t         | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua
index d922fee..e142c9d 100644
--- a/apisix/admin/init.lua
+++ b/apisix/admin/init.lua
@@ -119,6 +119,7 @@ end
 local function run()
     local api_ctx = {}
     core.ctx.set_vars_meta(api_ctx)
+    ngx.ctx.api_ctx = api_ctx
 
     local ok, err = check_token(api_ctx)
     if not ok then
@@ -185,6 +186,7 @@ end
 local function run_stream()
     local api_ctx = {}
     core.ctx.set_vars_meta(api_ctx)
+    ngx.ctx.api_ctx = api_ctx
 
     local local_conf = core.config.local_conf()
     if not local_conf.apisix.stream_proxy then
@@ -255,6 +257,7 @@ end
 local function get_plugins_list()
     local api_ctx = {}
     core.ctx.set_vars_meta(api_ctx)
+    ngx.ctx.api_ctx = api_ctx
 
     local ok, err = check_token(api_ctx)
     if not ok then
@@ -270,6 +273,7 @@ end
 local function post_reload_plugins()
     local api_ctx = {}
     core.ctx.set_vars_meta(api_ctx)
+    ngx.ctx.api_ctx = api_ctx
 
     local ok, err = check_token(api_ctx)
     if not ok then
diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index aa52244..dc4c446 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -164,6 +164,17 @@ do
                     end
                 end
 
+            elseif core_str.has_prefix(key, "arg_") then
+                local arg_key = sub_str(key, 5)
+                local args = request.get_uri_args()[arg_key]
+                if args then
+                    if type(args) == "table" then
+                        val = args[1]
+                    else
+                        val = args
+                    end
+                end
+
             elseif core_str.has_prefix(key, "http_") then
                 key = key:lower()
                 key = re_gsub(key, "-", "_", "jo")
diff --git a/t/core/ctx.t b/t/core/ctx.t
index 9a2413b..7e28f0c 100644
--- a/t/core/ctx.t
+++ b/t/core/ctx.t
@@ -46,7 +46,7 @@ server_port: 1984
 
 
 
-=== TEST 2: http header + arg
+=== TEST 2: http header
 --- config
     location /t {
         content_by_lua_block {
@@ -55,14 +55,12 @@ server_port: 1984
             core.ctx.set_vars_meta(ctx)
 
             ngx.say("http_host: ", ctx.var["http_host"])
-            ngx.say("arg_a: ", ctx.var["arg_a"])
         }
     }
 --- request
-GET /t?a=aaa
+GET /t
 --- response_body
 http_host: localhost
-arg_a: aaa
 --- no_error_log
 [error]
 
diff --git a/t/core/ctx2.t b/t/core/ctx2.t
index 478f0a9..33c05d7 100644
--- a/t/core/ctx2.t
+++ b/t/core/ctx2.t
@@ -188,3 +188,55 @@ query repo {
 hello world
 --- error_log
 find ctx._graphql: true
+
+
+
+=== TEST 7: support dash in the args
+--- 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,
+                 [=[{
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello",
+                        "vars": [["arg_a-b", "==", "ab"]]
+                }]=]
+                )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+
+
+
+=== TEST 8: check (support dash in the args)
+--- request
+GET /hello?a-b=ab
+--- response_body
+hello world
+
+
+
+=== TEST 9: support dash in the args(Multi args with the same name, only fetch 
the first one)
+--- request
+GET /hello?a-b=ab&a-b=ccc
+--- response_body
+hello world
+
+
+
+=== TEST 10: support dash in the args(arg is missing)
+--- request
+GET /hello
+--- error_code: 404
+--- response_body
+{"error_msg":"404 Route Not Found"}

Reply via email to