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: after updating the header, get the old value from the ctx.var #11329

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions apisix/core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ local function modify_header(ctx, header_name, header_value, override)
req_add_header(header_name, header_value)
end

if ctx and ctx.var then
-- when the header is updated, clear cache of ctx.var
ctx.var["http_" .. str_lower(header_name)] = nil
end

if is_apisix_or and not changed then
-- if the headers are not changed before,
-- we can only update part of the cache instead of invalidating the whole
Expand Down
27 changes: 27 additions & 0 deletions t/core/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,30 @@ test
test
--- error_log
DEPRECATED: use add_header(ctx, header_name, header_value) instead



=== TEST 16: after setting the header, ctx.var can still access the correct value
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
ngx.ctx.api_ctx = {}
local ctx = ngx.ctx.api_ctx
core.ctx.set_vars_meta(ctx)

ctx.var.http_server = "ngx"
ngx.say(ctx.var.http_server)

core.request.set_header(ctx, "server", "test")
ngx.say(ctx.var.http_server)

-- case-insensitive
core.request.set_header(ctx, "Server", "apisix")
ngx.say(ctx.var.http_server)
}
}
--- response_body
ngx
test
apisix
Loading