Skip to content

Commit

Permalink
fix: after updating the header, get the old value from the ctx.var (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan authored Jun 6, 2024
1 parent d77d672 commit 953be46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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

0 comments on commit 953be46

Please sign in to comment.