This is an automated email from the ASF dual-hosted git repository.
spacewander 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 7e292eadd fix: invalidate cache in core.request.add_haeder and fix
some calls (#8824)
7e292eadd is described below
commit 7e292eadd36e738f84647634b0c351bc268ea529
Author: Tristan <[email protected]>
AuthorDate: Mon Mar 13 11:07:22 2023 +0800
fix: invalidate cache in core.request.add_haeder and fix some calls (#8824)
---
apisix/core/request.lua | 40 ++++++++++++++++++++++++-----------
apisix/plugins/proxy-rewrite.lua | 6 +++---
t/core/request.t | 45 ++++++++++++++++++++++++++++++++++++++++
t/plugin/proxy-rewrite3.t | 5 ++++-
4 files changed, 80 insertions(+), 16 deletions(-)
diff --git a/apisix/core/request.lua b/apisix/core/request.lua
index 173fafc56..aa9dd03bf 100644
--- a/apisix/core/request.lua
+++ b/apisix/core/request.lua
@@ -42,6 +42,8 @@ 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 req_set_header = ngx.req.set_header
local _M = {}
@@ -108,8 +110,7 @@ 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!
@@ -117,7 +118,11 @@ function _M.set_header(ctx, header_name, header_value)
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
@@ -131,26 +136,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
+ req_set_header(header_name, header_value)
+ 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.
diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua
index 65ffdf3ab..0308557ee 100644
--- a/apisix/plugins/proxy-rewrite.lua
+++ b/apisix/plugins/proxy-rewrite.lua
@@ -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)
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
diff --git a/t/core/request.t b/t/core/request.t
index eab9986d4..9bf48ddba 100644
--- a/t/core/request.t
+++ b/t/core/request.t
@@ -439,3 +439,48 @@ 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
+ local json = require("toolkit.json")
+ 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(json.encode(h2))
+ core.request.add_header(ctx, "test_header", "t3")
+ local h3 = core.request.header(ctx, "test_header")
+ ngx.say(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
diff --git a/t/plugin/proxy-rewrite3.t b/t/plugin/proxy-rewrite3.t
index 613bd1b1a..eaea849cd 100644
--- a/t/plugin/proxy-rewrite3.t
+++ b/t/plugin/proxy-rewrite3.t
@@ -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