Skip to content

Commit

Permalink
fix: invalidate cache in core.request.add_haeder and fix some calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangfucheng committed Feb 21, 2023
1 parent e021266 commit 01a1743
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
31 changes: 19 additions & 12 deletions apisix/core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,13 @@ function _M.header(ctx, name)
return _headers(ctx)[name]
end


function _M.set_header(ctx, header_name, header_value)
local function modify_header(ctx, header_name, header_value, update_func, override)
if type(ctx) == "string" then
-- It would be simpler to keep compatibility if we put 'ctx'
-- after 'header_value', but the style is too ugly!
header_value = header_name
header_name = ctx
ctx = nil

log.warn("DEPRECATED: use set_header(ctx, header_name, header_value) instead")
end

local err
Expand All @@ -131,26 +128,36 @@ function _M.set_header(ctx, header_name, header_value)
changed = a6_request.is_request_header_set()
end

ngx.req.set_header(header_name, header_value)
update_func(header_name, header_value)

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
a6_request.clear_request_header()
if ctx and ctx.headers then
ctx.headers[header_name] = header_value
if override or not ctx.headers[header_name] then
ctx.headers[header_name] = header_value
else
ctx.headers[header_name] = ctx.headers[header_name] .. header_value
end
end
end
end

function _M.add_header(header_name, header_value)
local err
header_name, err = _validate_header_name(header_name)
if err then
error(err)
function _M.set_header(ctx, header_name, header_value)
if type(ctx) == "string" then
log.warn("DEPRECATED: use set_header(ctx, header_name, header_value) instead")
end

modify_header(ctx, header_name, header_value, ngx.req.set_header, true)
end

function _M.add_header(ctx, header_name, header_value)
if type(ctx) == "string" then
log.warn("DEPRECATED: use add_header(ctx, header_name, header_value) instead")
end

req_add_header(header_name, header_value)
modify_header(ctx, header_name, header_value, req_add_header, false)
end

-- return the remote address of client which directly connecting to APISIX.
Expand Down
6 changes: 3 additions & 3 deletions apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,18 @@ do
for i = 1, field_cnt, 2 do
local val = core.utils.resolve_var(hdr_op.add[i + 1], ctx.var)
local header = hdr_op.add[i]
core.request.add_header(header, val)
core.request.add_header(ctx, header, val)
end

local field_cnt = #hdr_op.set
for i = 1, field_cnt, 2 do
local val = core.utils.resolve_var(hdr_op.set[i + 1], ctx.var)
core.request.set_header(hdr_op.set[i], val)
core.request.set_header(ctx, hdr_op.set[i], val)
end

local field_cnt = #hdr_op.remove
for i = 1, field_cnt do
core.request.set_header(hdr_op.remove[i], nil)
core.request.set_header(ctx, hdr_op.remove[i], nil)
end

end
Expand Down
40 changes: 40 additions & 0 deletions t/core/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,43 @@ while ($i <= 101) {
$s
--- response_body
101
=== TEST 15: add header
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
ngx.ctx.api_ctx = {}
local ctx = ngx.ctx.api_ctx
core.request.add_header(ctx, "test_header", "test")
local h = core.request.header(ctx, "test_header")
ngx.say(h)
core.request.add_header(ctx, "test_header", "t2")
local h2 = core.request.header(ctx, "test_header")
ngx.say(h2)
}
}
--- response_body
test
testt2
=== TEST 16: call add_header with deprecated way
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
ngx.ctx.api_ctx = {}
local ctx = ngx.ctx.api_ctx
core.request.add_header("test_header", "test")
local h = core.request.header(ctx, "test_header")
ngx.say(h)
}
}
--- response_body
test
--- error_log
DEPRECATED: use add_header(ctx, header_name, header_value) instead
9 changes: 9 additions & 0 deletions t/plugin/proxy-rewrite3.t
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,12 @@ passed
GET /echo HTTP/1.1
--- response_headers
test: test_in_set
=== TEST 21: deprecated calls test
--- request
GET /echo HTTP/1.1
--- no_error_log
DEPRECATED: use add_header(ctx, header_name, header_value) instead
DEPRECATED: use set_header(ctx, header_name, header_value) instead

0 comments on commit 01a1743

Please sign in to comment.