This is an automated email from the ASF dual-hosted git repository. membphis 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 97ed426 fix: refresh cached header when we set request header (#2917) 97ed426 is described below commit 97ed426b4ef4d403213d78b3e4883f1b238f3c8a Author: 罗泽轩 <spacewander...@gmail.com> AuthorDate: Mon Dec 7 21:50:31 2020 +0800 fix: refresh cached header when we set request header (#2917) --- apisix/core/request.lua | 17 ++++++++++++++++- apisix/plugins/hmac-auth.lua | 6 +++--- apisix/plugins/request-id.lua | 2 +- apisix/plugins/wolf-rbac.lua | 6 +++--- apisix/plugins/zipkin.lua | 2 +- t/core/request.t | 24 ++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/apisix/core/request.lua b/apisix/core/request.lua index 2e5af94..f359460 100644 --- a/apisix/core/request.lua +++ b/apisix/core/request.lua @@ -68,12 +68,27 @@ function _M.header(ctx, name) end -function _M.set_header(header_name, header_value) +function _M.set_header(ctx, header_name, header_value) + 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 header_name, err = _validate_header_name(header_name) if err then error(err) end + + if ctx and ctx.headers then + ctx.headers[header_name] = header_value + end + ngx.req.set_header(header_name, header_value) end diff --git a/apisix/plugins/hmac-auth.lua b/apisix/plugins/hmac-auth.lua index c11d31f..15e19a3 100644 --- a/apisix/plugins/hmac-auth.lua +++ b/apisix/plugins/hmac-auth.lua @@ -117,12 +117,12 @@ local function array_to_map(arr) end -local function remove_headers(...) +local function remove_headers(ctx, ...) local headers = { ... } if headers and #headers > 0 then for _, header in ipairs(headers) do core.log.info("remove_header: ", header) - core.request.set_header(header, nil) + core.request.set_header(ctx, header, nil) end end end @@ -389,7 +389,7 @@ local function get_params(ctx) core.log.info("keep_headers: ", keep_headers) if not keep_headers then - remove_headers(signature_key, algorithm_key, signed_headers_key) + remove_headers(ctx, signature_key, algorithm_key, signed_headers_key) end core.log.info("params: ", core.json.delay_encode(params)) diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua index 0de07fa..60e590a 100644 --- a/apisix/plugins/request-id.lua +++ b/apisix/plugins/request-id.lua @@ -45,7 +45,7 @@ function _M.rewrite(conf, ctx) local headers = ngx.req.get_headers() local uuid_val = uuid() if not headers[conf.header_name] then - core.request.set_header(conf.header_name, uuid_val) + core.request.set_header(ctx, conf.header_name, uuid_val) end if conf.include_in_response then diff --git a/apisix/plugins/wolf-rbac.lua b/apisix/plugins/wolf-rbac.lua index 4966601..20392e9 100644 --- a/apisix/plugins/wolf-rbac.lua +++ b/apisix/plugins/wolf-rbac.lua @@ -307,9 +307,9 @@ function _M.rewrite(conf, ctx) core.response.set_header(prefix .. "UserId", userId) core.response.set_header(prefix .. "Username", username) core.response.set_header(prefix .. "Nickname", ngx.escape_uri(nickname)) - core.request.set_header(prefix .. "UserId", userId) - core.request.set_header(prefix .. "Username", username) - core.request.set_header(prefix .. "Nickname", ngx.escape_uri(nickname)) + core.request.set_header(ctx, prefix .. "UserId", userId, ctx) + core.request.set_header(ctx, prefix .. "Username", username) + core.request.set_header(ctx, prefix .. "Nickname", ngx.escape_uri(nickname)) end if res.status ~= 200 then diff --git a/apisix/plugins/zipkin.lua b/apisix/plugins/zipkin.lua index dced36c..fc75f8d 100644 --- a/apisix/plugins/zipkin.lua +++ b/apisix/plugins/zipkin.lua @@ -164,7 +164,7 @@ function _M.access(conf, ctx) local outgoing_headers = {} tracer:inject(opentracing.proxy_span, "http_headers", outgoing_headers) for k, v in pairs(outgoing_headers) do - core.request.set_header(k, v) + core.request.set_header(ctx, k, v) end end diff --git a/t/core/request.t b/t/core/request.t index 5f6fd7e..fb4c4c6 100644 --- a/t/core/request.t +++ b/t/core/request.t @@ -354,3 +354,27 @@ X-Real-IP: 10.0.0.1 1.1 --- no_error_log [error] + + + +=== TEST 10: set header +--- config + location = /hello { + content_by_lua_block { + local core = require("apisix.core") + ngx.ctx.api_ctx = {} + local h = core.request.header(nil, "Test") + local ctx = ngx.ctx.api_ctx + core.request.set_header(ctx, "Test", "t") + local h2 = core.request.header(ctx, "Test") + ngx.say(h) + ngx.say(h2) + } + } +--- request +GET /hello +--- response_body +nil +t +--- no_error_log +[error]