This is an automated email from the ASF dual-hosted git repository.

alinsran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 953be46c3 fix: after updating the header, get the old value from the 
ctx.var (#11329)
953be46c3 is described below

commit 953be46c321d1f27efa83ac6c5886e4b9eefc8d3
Author: AlinsRan <[email protected]>
AuthorDate: Thu Jun 6 08:53:29 2024 +0800

    fix: after updating the header, get the old value from the ctx.var (#11329)
---
 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 e76bbf79a..c5278b6b8 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 c343e2d28..322050830 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

Reply via email to