diff --git a/lua/apisix/core/ctx.lua b/lua/apisix/core/ctx.lua index 1e5082b1..0d6e364e 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 c2e16aa4..cea8ca54 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 dd24e69f..52abaa4a 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 0348352f..80ac3110 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 d68acd90..45050b05 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 9a73bfad..34555f28 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