nic-6443 opened a new pull request, #14209:
URL: https://github.com/apache/apisix/pull/14209

   ### Description
   
   `core.request.set_header` / `add_header` (and clearing a header with `nil`) 
are supposed to drop the cached `ctx.var` value of that header, but the value 
read through the usual spelling stays stale.
   
   `modify_header` clears `ctx.var["http_" .. str_lower(header_name)]`, i.e. 
`http_x-foo` for `X-Foo`. `ctx.var` however caches `http_*` lookups under 
whatever key the caller used, and route `vars`, plugins and most code read the 
underscore form `http_x_foo`. So this sequence returns the old value:
   
   ```lua
   local v1 = ctx.var.http_x_foo              -- "old", now cached under 
"http_x_foo"
   core.request.set_header(ctx, "X-Foo", "new")
   local v2 = ctx.var.http_x_foo              -- still "old"
   ```
   
   The same happens after `core.request.set_header(ctx, "X-Foo", nil)`: a 
header removed before proxying is still visible to every later phase that reads 
`ctx.var.http_x_foo`, and a route whose `vars` matched on that header has 
already put it in the cache.
   
   Since nginx treats `$http_*` names case-insensitively and with `-` equal to 
`_`, any spelling (`http_x_foo`, `http_x-foo`, `http_X-Foo`, ...) can end up as 
its own cache key, so clearing one more spelling in `modify_header` would still 
leave the others. Instead, `ctx.var` now stores every `http_*` entry, on read 
and on write, under the normalized key (lower case, `-` replaced with `_`), the 
same name it already used to fetch the nginx variable. Each header then has a 
single cache entry, and the existing invalidation in `modify_header` (which 
goes through `__newindex`) clears it whatever spelling the caller passes. 
Lookups with the normalized spelling still hit the cache directly; other 
spellings pay one lower/gsub before hitting the shared entry.
   
   A test in `t/core/request.t` reads the header under three spellings after 
set, add, clear and a direct `ctx.var` write; it fails without this change.
   
   No documentation describes this caching, so there is nothing to update in 
`docs/`.
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, 
please discuss on the [APISIX mailing 
list](https://github.com/apache/apisix/tree/master#community) first)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to