diff --git a/.luacheckrc b/.luacheckrc index 1145c0f4..03b9b076 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -13,3 +13,14 @@ not_globals = { ignore = { "6.", -- ignore whitespace warnings } + + +globals = { + ngx = { + req = { + set_uri_args = { + read_only = false + } + } + } +} diff --git a/lualib/resty/kong/var.lua b/lualib/resty/kong/var.lua index 2f6685a4..14f12247 100644 --- a/lualib/resty/kong/var.lua +++ b/lualib/resty/kong/var.lua @@ -6,6 +6,7 @@ local C = ffi.C local ffi_new = ffi.new local ffi_str = ffi.string local var = ngx.var +local req = ngx.req local type = type local error = error local assert = assert @@ -150,6 +151,16 @@ local function var_set_by_index(index, value) end +local function patch_functions() + local orig_set_uri_args = req.set_uri_args + + req.set_uri_args = function(...) + variable_index.args = nil + return orig_set_uri_args(...) + end +end + + local function patch_metatable() if get_phase() ~= "init" then error("patch_metatable can only be called in init phase") @@ -184,6 +195,8 @@ local function patch_metatable() return orig_set(self, name, value) end + + patch_functions() end diff --git a/t/005-indexed-var-openresty-suites.t b/t/005-indexed-var-openresty-suites.t index d34e1b2b..ecc8c78b 100644 --- a/t/005-indexed-var-openresty-suites.t +++ b/t/005-indexed-var-openresty-suites.t @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 9 + 4); +plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3); #no_diff(); #no_long_string(); @@ -359,3 +359,36 @@ variable not changeable ["GET /balancer?port=8091", "GET /balancer?port=8092"] --- response_body eval ["this is backend peer 8091", "this is backend peer 8092"] + +=== TEST 13: patch metatable does not invalidate function req.set_uri_args +--- http_config + lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;"; + # this is not required, but set explictly in tests + lua_kong_load_var_index $args; + + init_by_lua_block { + local var = require "resty.kong.var" + var.patch_metatable() + } + +--- config + set $args 'foo=bar'; + + location /t { + content_by_lua_block { + local a = ngx.var.args + ngx.req.set_uri_args(a .. "&added=yes") + ngx.say(ngx.var.args) + } + } + +--- request +GET /t +--- response_body_like +foo=bar&added=yes + +--- error_code: 200 +--- no_error_log +[error] +[crit] +[alert]