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 Jan 3, 2023
1 parent e021266 commit d5296c5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
16 changes: 15 additions & 1 deletion apisix/core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,28 @@ function _M.set_header(ctx, header_name, header_value)
end
end

function _M.add_header(header_name, header_value)
function _M.add_header(ctx, header_name, header_value)
if type(ctx) == "string" then
header_value = header_name
header_name = ctx
ctx = nil

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

local err
header_name, err = _validate_header_name(header_name)
if err then
error(err)
end

req_add_header(header_name, header_value)

if not ctx then
ctx = ngx.ctx.api_ctx
end

ctx.headers = {}
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
56 changes: 56 additions & 0 deletions t/core/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,59 @@ 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)
}
}
--- response_body
test
=== TEST 16: test invalidated cache for 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(ctx.headers.test_header)
core.request.add_header(ctx, "test_header", "abc")
ngx.say(ctx.headers.test_header)
}
}
--- response_body
test
nil
=== TEST 17: 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 d5296c5

Please sign in to comment.