You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/03/27 15:13:56 UTC

[incubator-apisix] branch master updated: bugfix: fail to get the http request header by `ngx.var. *` (#1348)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 51684de  bugfix: fail to get the http request header by `ngx.var. *` (#1348)
51684de is described below

commit 51684de77be53ae9cb451245399debd184e83d12
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Fri Mar 27 23:13:46 2020 +0800

    bugfix: fail to get the http request header by `ngx.var. *` (#1348)
    
    * bugfix: to get the HTTP request header by `ngx.var.*`, we need to convert
      the name to lower case with dashes replaced by underscores.
    
    more information:
    
    http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_
    
    * change: the key should be a string object when fetching a value from `ctx.var`.
    
    * bug(make lint): If the code style is bad, do exit with code 1.
---
 lua/apisix/core/ctx.lua              | 12 +++++
 t/core/ctx.t                         | 42 +++++++++++++++++
 t/node/chash-balance.t               | 10 ----
 t/node/chash-hashon.t                | 90 ++++++++++++++++++++++++++++++++++++
 t/node/route-domain-with-local-dns.t |  1 -
 utils/check-lua-code-style.sh        |  7 ++-
 6 files changed, 147 insertions(+), 15 deletions(-)

diff --git a/lua/apisix/core/ctx.lua b/lua/apisix/core/ctx.lua
index 1e5082b..0d6e364 100644
--- a/lua/apisix/core/ctx.lua
+++ b/lua/apisix/core/ctx.lua
@@ -25,6 +25,9 @@ local C            = ffi.C
 local sub_str      = string.sub
 local rawset       = rawset
 local ngx_var      = ngx.var
+local re_gsub      = ngx.re.gsub
+local type         = type
+local error        = error
 
 
 ffi.cdef[[
@@ -61,6 +64,10 @@ do
 
     local mt = {
         __index = function(t, key)
+            if type(key) ~= "string" then
+                error("invalid argument, expect string value", 2)
+            end
+
             local val
             local method = var_methods[key]
             if method then
@@ -77,6 +84,11 @@ do
                     end
                 end
 
+            elseif C.memcmp(key, "http_", 5) == 0 then
+                key = key:lower()
+                key = re_gsub(key, "-", "_", "jo")
+                val = get_var(key, t._request)
+
             else
                 val = get_var(key, t._request)
             end
diff --git a/t/core/ctx.t b/t/core/ctx.t
index c2e16aa..cea8ca5 100644
--- a/t/core/ctx.t
+++ b/t/core/ctx.t
@@ -113,3 +113,45 @@ cookie_c: ccc
 cookie_d: nil
 --- no_error_log
 [error]
+
+
+
+=== TEST 5: key is nil
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local ctx = {}
+            core.ctx.set_vars_meta(ctx)
+
+            ngx.say("cookie_a: ", ctx.var[nil])
+        }
+    }
+--- more_headers
+Cookie: a=a; b=bb; c=ccc
+--- request
+GET /t?a=aaa
+--- error_code: 500
+--- error_log
+invalid argument, expect string value
+
+
+
+=== TEST 6: key is number
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local ctx = {}
+            core.ctx.set_vars_meta(ctx)
+
+            ngx.say("cookie_a: ", ctx.var[2222])
+        }
+    }
+--- more_headers
+Cookie: a=a; b=bb; c=ccc
+--- request
+GET /t?a=aaa
+--- error_code: 500
+--- error_log
+invalid argument, expect string value
diff --git a/t/node/chash-balance.t b/t/node/chash-balance.t
index dd24e69..52abaa4 100644
--- a/t/node/chash-balance.t
+++ b/t/node/chash-balance.t
@@ -14,16 +14,6 @@
 # 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);
diff --git a/t/node/chash-hashon.t b/t/node/chash-hashon.t
index 0348352..80ac311 100644
--- a/t/node/chash-hashon.t
+++ b/t/node/chash-hashon.t
@@ -535,3 +535,93 @@ chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
 chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
 chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
 chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
+
+
+
+=== TEST 11: set route(key contains uppercase letters and hyphen)
+--- 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,
+                 [[{
+                    "uri": "/server_port",
+                    "upstream": {
+                        "key": "X-Sessionid",
+                        "type": "chash",
+                        "hash_on": "header",
+                        "nodes": {
+                            "127.0.0.1:1980": 1,
+                            "127.0.0.1:1981": 1
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 12: hit routes with header
+--- config
+    location /t {
+        content_by_lua_block {
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port
+                        .. "/server_port"
+
+            local ports_count = {}
+            for i = 1, 6 do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri, {
+                    method = "GET",
+                    headers = {
+                        ["X-Sessionid"] = "chash_val_" .. i
+                    }
+                })
+                if not res then
+                    ngx.say(err)
+                    return
+                end
+                ports_count[res.body] = (ports_count[res.body] or 0) + 1
+            end
+
+            local ports_arr = {}
+            for port, count in pairs(ports_count) do
+                table.insert(ports_arr, {port = port, count = count})
+            end
+
+            local function cmd(a, b)
+                return a.port > b.port
+            end
+            table.sort(ports_arr, cmd)
+
+            ngx.say(require("cjson").encode(ports_arr))
+            ngx.exit(200)
+        }
+    }
+--- request
+GET /t
+--- response_body
+[{"count":3,"port":"1981"},{"count":3,"port":"1980"}]
+--- no_error_log
+[error]
+--- error_log
+chash_key: "chash_val_1"
+chash_key: "chash_val_2"
+chash_key: "chash_val_3"
+chash_key: "chash_val_4"
+chash_key: "chash_val_5"
+chash_key: "chash_val_6"
diff --git a/t/node/route-domain-with-local-dns.t b/t/node/route-domain-with-local-dns.t
index d68acd9..45050b0 100644
--- a/t/node/route-domain-with-local-dns.t
+++ b/t/node/route-domain-with-local-dns.t
@@ -68,7 +68,6 @@ passed
 
 
 
-
 === TEST 2: /not_found
 --- request
 GET /not_found
diff --git a/utils/check-lua-code-style.sh b/utils/check-lua-code-style.sh
index 9a73bfa..34555f2 100755
--- a/utils/check-lua-code-style.sh
+++ b/utils/check-lua-code-style.sh
@@ -32,10 +32,9 @@ luacheck -q lua
     lua/apisix/plugins/limit-count/*.lua > \
     /tmp/check.log 2>&1 || (cat /tmp/check.log && exit 1)
 
-count=`grep -E ".lua:[0-9]+:" /tmp/check.log -c | true`
-
-if [ $count -ne 0 ]
-then
+grep -E "ERROR.*.lua:" /tmp/check.log > /tmp/error.log | true
+if [ -s /tmp/error.log ]; then
+    echo "=====bad style====="
     cat /tmp/check.log
     exit 1
 fi