Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalidate cache in core.request.add_haeder and fix some calls #8824

Merged
merged 8 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions apisix/core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ local req_get_body_file = ngx.req.get_body_file
local req_get_post_args = ngx.req.get_post_args
local req_get_uri_args = ngx.req.get_uri_args
local req_set_uri_args = ngx.req.set_uri_args
local table_insert = table.insert


local _M = {}
Expand Down Expand Up @@ -108,16 +109,19 @@ 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, 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")
if override then
log.warn("DEPRECATED: use set_header(ctx, header_name, header_value) instead")
else
log.warn("DEPRECATED: use add_header(ctx, header_name, header_value) instead")
end
end

local err
Expand All @@ -131,26 +135,37 @@ 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)
if override then
ngx.req.set_header(header_name, header_value)
jiangfucheng marked this conversation as resolved.
Show resolved Hide resolved
else
req_add_header(header_name, header_value)
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
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
local values = ctx.headers[header_name]
if type(values) == "table" then
table_insert(values, header_value)
else
ctx.headers[header_name] = {values, header_value}
end
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)
end
function _M.set_header(ctx, header_name, header_value)
modify_header(ctx, header_name, header_value, true)
end

req_add_header(header_name, header_value)
function _M.add_header(ctx, header_name, header_value)
modify_header(ctx, header_name, header_value, 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 @@ -327,18 +327,18 @@ function _M.rewrite(conf, ctx)
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)
jiangfucheng marked this conversation as resolved.
Show resolved Hide resolved
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
44 changes: 44 additions & 0 deletions t/core/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,47 @@ while ($i <= 101) {
$s
--- response_body
101



=== TEST 15: add header
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
jiangfucheng marked this conversation as resolved.
Show resolved Hide resolved
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(core.json.encode(h2))
core.request.add_header(ctx, "test_header", "t3")
local h3 = core.request.header(ctx, "test_header")
ngx.say(core.json.encode(h3))
}
}
--- response_body
test
["test","t2"]
["test","t2","t3"]



=== 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
52 changes: 51 additions & 1 deletion t/plugin/proxy-rewrite3.t
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,14 @@ passed



=== TEST 20: set and test priority test
=== TEST 20: set and test priority test & deprecated calls test
--- request
GET /echo HTTP/1.1
--- response_headers
test: test_in_set
--- no_error_log
DEPRECATED: use add_header(ctx, header_name, header_value) instead
DEPRECATED: use set_header(ctx, header_name, header_value) instead



Expand Down Expand Up @@ -619,3 +622,50 @@ GET /test/plugin/proxy/rewrite HTTP/1.1
}
--- response_body
/plugin_proxy_rewrite?a=c



=== TEST 27: set route
jiangfucheng marked this conversation as resolved.
Show resolved Hide resolved
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"headers": {
"add":{"test": "t1"},
"set":{"test": "t2"}
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/echo"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 28: 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