Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(var): patch set_header #97

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ globals = {
req = {
set_uri_args = {
read_only = false
},
set_header = {
read_only = false
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions lualib/resty/kong/var.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ local NGX_DECLINED = ngx.DECLINED

local variable_index = {}
local metatable_patched
local str_replace_char
local replace_dashes_lower


--Add back if stream module is implemented to aid readability
Expand All @@ -50,6 +52,11 @@ if subsystem == "http" then
--ngx_lua_kong_ffi_var_get_by_index = C.ngx_http_lua_kong_ffi_var_get_by_index
--ngx_lua_kong_ffi_var_set_by_index = C.ngx_http_lua_kong_ffi_var_set_by_index
--ngx_lua_kong_ffi_var_load_indexes = C.ngx_http_lua_kong_ffi_var_load_indexes

str_replace_char = require("resty.core.utils").str_replace_char
replace_dashes_lower = function(str)
return str_replace_char(str:lower(), "-", "_")
end
end


Expand Down Expand Up @@ -159,6 +166,16 @@ local function patch_functions()
variable_index.args = nil
return orig_set_uri_args(...)
end

local orig_set_header = req.set_header

req.set_header = function(...)
local normalized_header = replace_dashes_lower(...)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps I need to unpack the data structure(…) and retrieve the first element; this should be clear.

Copy link
Member

@samugi samugi Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, doesn't the current implementation mean we're passing both header_name and header_value from ngx.req.set_header(header_name, header_value), to replace_dashes_lower?

We can probably patch this set_header function with a function that takes exactly 2 arguments, then use the first (header_name) to invalidate the index.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to

  req.set_header = function(name, value)
    local normalized_header = replace_dashes_lower(name)

I did not choose the

  req.set_header = function(...)
    local args = table.unpack(...)
    local normalized_header = replace_dashes_lower(args[1])

I feel that this will degrade performance.

normalized_header = "http_" .. normalized_header
variable_index[normalized_header] = nil

return orig_set_header(...)
end
end


Expand Down
57 changes: 56 additions & 1 deletion t/005-indexed-var-openresty-suites.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3);
plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3 + 3);


#no_diff();
#no_long_string();
Expand Down Expand Up @@ -387,3 +388,57 @@ foo=bar&added=yes
[error]
[crit]
[alert]



=== TEST 14: patch metatable does not invalidate function req.set_header
--- http_config
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
lua_kong_load_var_index default;

init_by_lua_block {
require("resty.kong.var").patch_metatable()
}

--- config
location /auth {
content_by_lua_block {
ngx.say(ngx.var.http_authorization)
ngx.req.set_header("Authorization", "foo")
ngx.say(ngx.var.http_authorization)
ngx.req.set_header("Authorization", "bar")
ngx.say(ngx.var.http_authorization)
ngx.req.set_header("Authorization", "baz")
ngx.say(ngx.var.http_authorization)
ngx.req.set_header("Authorization", "qux")
ngx.say(ngx.var.http_authorization)

ngx.say(ngx.var.http_kong_debug)
ngx.req.set_header("Kong-Debug", "true")
ngx.say(ngx.var.http_kong_debug)
ngx.req.set_header("Kong-Debug", "false")
ngx.say(ngx.var.http_kong_debug)
ngx.req.set_header("Kong-Debug", "true")
ngx.say(ngx.var.http_kong_debug)
ngx.req.set_header("Kong-Debug", "false")
ngx.say(ngx.var.http_kong_debug)
}
}

--- request
GET /auth
--- response_body
nil
foo
bar
baz
qux
nil
true
false
true
false
--- no_error_log
[error]
[crit]
[alert]