From 3553205e897a9964f592918b973cef954d2ae499 Mon Sep 17 00:00:00 2001 From: AlinsRan Date: Wed, 5 Jun 2024 10:24:57 +0800 Subject: [PATCH] fix: after updating the header, get the old value from the ctx.var --- apisix/core/request.lua | 5 +++++ t/core/request.t | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/apisix/core/request.lua b/apisix/core/request.lua index e76bbf79a417..c5278b6b8072 100644 --- a/apisix/core/request.lua +++ b/apisix/core/request.lua @@ -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 diff --git a/t/core/request.t b/t/core/request.t index c343e2d28fa8..322050830a14 100644 --- a/t/core/request.t +++ b/t/core/request.t @@ -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