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

nic-6443 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 4d13c0f50 fix(core.request): set_header must update the cached headers 
with normalized key (#13517)
4d13c0f50 is described below

commit 4d13c0f50ddbe2472e2a32415ec578465cad1227
Author: Nic <[email protected]>
AuthorDate: Fri Jun 12 11:05:14 2026 +0800

    fix(core.request): set_header must update the cached headers with 
normalized key (#13517)
---
 apisix/core/request.lua | 12 ++++++++----
 t/core/request.t        | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/apisix/core/request.lua b/apisix/core/request.lua
index 65c01c5ea..e04adb672 100644
--- a/apisix/core/request.lua
+++ b/apisix/core/request.lua
@@ -161,14 +161,18 @@ local function modify_header(ctx, header_name, 
header_value, override)
         -- we can only update part of the cache instead of invalidating the 
whole
         a6_request.clear_request_header()
         if ctx and ctx.headers then
-            if override or not ctx.headers[header_name] then
-                ctx.headers[header_name] = header_value
+            -- the cached table from ngx.req.get_headers() stores keys in
+            -- lower case, so normalize the key to avoid leaving a stale
+            -- entry when the passed name uses a different case
+            local cache_key = str_lower(header_name)
+            if override or not ctx.headers[cache_key] then
+                ctx.headers[cache_key] = header_value
             else
-                local values = ctx.headers[header_name]
+                local values = ctx.headers[cache_key]
                 if type(values) == "table" then
                     table_insert(values, header_value)
                 else
-                    ctx.headers[header_name] = {values, header_value}
+                    ctx.headers[cache_key] = {values, header_value}
                 end
             end
         end
diff --git a/t/core/request.t b/t/core/request.t
index 14c2e901e..806959e0d 100644
--- a/t/core/request.t
+++ b/t/core/request.t
@@ -542,3 +542,38 @@ same table: true
 decode_count: 1
 after set_body model: claude
 decode_count: 2
+
+
+
+=== TEST 18: set_header with a different case should not leave a stale entry 
in the cached headers
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            ngx.ctx.api_ctx = {}
+            local ctx = ngx.ctx.api_ctx
+
+            -- warm the headers cache
+            core.request.headers(ctx)
+
+            core.request.set_header(ctx, "X-Mixed-Case", "new")
+
+            local count = 0
+            local value
+            for k, v in pairs(core.request.headers(ctx)) do
+                if string.lower(k) == "x-mixed-case" then
+                    count = count + 1
+                    value = v
+                end
+            end
+            ngx.say("count: ", count)
+            ngx.say("value: ", value)
+            ngx.say("header: ", core.request.header(ctx, "X-Mixed-Case"))
+        }
+    }
+--- more_headers
+x-mixed-case: old
+--- response_body
+count: 1
+value: new
+header: new

Reply via email to