Skip to content

Commit

Permalink
feat: add rewrite:RespHeaders and modify the upstream response head…
Browse files Browse the repository at this point in the history
…ers via `request` implementation (#6426)

Co-authored-by: 帅进超 <shuaijinchao@gmail.com>
  • Loading branch information
rampagecong and shuaijinchao authored Mar 4, 2022
1 parent 380f762 commit c4229d1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
23 changes: 23 additions & 0 deletions apisix/plugins/ext-plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ local type = type


local events_list
local exclude_resp_header = {
["connection"] = true,
["content-length"] = true,
["transfer-encoding"] = true,
["location"] = true,
["server"] = true,
["www-authenticate"] = true,
["content-encoding"] = true,
["content-type"] = true,
["content-location"] = true,
["content-language"] = true,
}

local function new_lrucache()
return core.lrucache.new({
Expand Down Expand Up @@ -611,6 +623,17 @@ local rpc_handlers = {
end
end

local len = rewrite:RespHeadersLength()
if len > 0 then
for i = 1, len do
local entry = rewrite:RespHeaders(i)
local name = entry:Name()
if exclude_resp_header[str_lower(name)] == nil then
core.response.set_header(name, entry:Value())
end
end
end

local len = rewrite:ArgsLength()
if len > 0 then
local changed = {}
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dependencies = {
"luasec = 0.9-1",
"lua-resty-consul = 0.3-2",
"penlight = 1.9.2-1",
"ext-plugin-proto = 0.3.0",
"ext-plugin-proto = 0.4.0",
"casbin = 1.26.0",
"api7-snowflake = 2.0-1",
"inspect == 3.1.1",
Expand Down
32 changes: 32 additions & 0 deletions t/lib/ext-plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,38 @@ function _M.go(case)
local action = http_req_call_rewrite.End(builder)
build_action(action, http_req_call_action.Rewrite)

elseif case.rewrite_resp_header == true or case.rewrite_vital_resp_header == true then
local hdrs = {
{"X-Resp", "foo"},
{"X-Req", "bar"},
{"Content-Type", "application/json"},
{"Content-Encoding", "deflate"},
}
local len = #hdrs
local textEntries = {}
for i = 1, len do
local name = builder:CreateString(hdrs[i][1])
local value = builder:CreateString(hdrs[i][2])
text_entry.Start(builder)
text_entry.AddName(builder, name)
text_entry.AddValue(builder, value)
local c = text_entry.End(builder)
textEntries[i] = c
end
http_req_call_rewrite.StartRespHeadersVector(builder, len)
for i = len, 1, -1 do
builder:PrependUOffsetTRelative(textEntries[i])
end
local vec = builder:EndVector(len)

local path = builder:CreateString("/plugin_proxy_rewrite_resp_header")

http_req_call_rewrite.Start(builder)
http_req_call_rewrite.AddRespHeaders(builder, vec)
http_req_call_rewrite.AddPath(builder, path)
local action = http_req_call_rewrite.End(builder)
build_action(action, http_req_call_action.Rewrite)

else
http_req_call_resp.Start(builder)
end
Expand Down
8 changes: 7 additions & 1 deletion t/lib/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,13 @@ function _M.google_logging_entries()
ngx.say(data)
end

function _M.plugin_proxy_rewrite_resp_header()
ngx.req.read_body()
local s = "plugin_proxy_rewrite_resp_header"
ngx.header['Content-Length'] = #s + 1
ngx.say(s)
end

-- Please add your fake upstream above
function _M.go()
local action = string.sub(ngx.var.uri, 2)
Expand All @@ -536,5 +543,4 @@ function _M.go()
return _M[action]()
end


return _M
42 changes: 42 additions & 0 deletions t/plugin/ext-plugin/http-req-call.t
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,45 @@ cat
--- response_headers
X-Resp: foo
X-Req: bar
=== TEST 19: rewrite response header and call the upstream service
--- request
GET /hello
--- extra_stream_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
content_by_lua_block {
local ext = require("lib.ext-plugin")
ext.go({rewrite_resp_header = true})
}
}
--- response_body
plugin_proxy_rewrite_resp_header
--- response_headers
X-Resp: foo
X-Req: bar
=== TEST 20: rewrite non-important response headers and call the upstream service
--- request
GET /hello
--- extra_stream_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
content_by_lua_block {
local ext = require("lib.ext-plugin")
ext.go({rewrite_vital_resp_header = true})
}
}
--- response_body
plugin_proxy_rewrite_resp_header
--- response_headers
X-Resp: foo
X-Req: bar
Content-Type: text/plain
Content-Encoding:

0 comments on commit c4229d1

Please sign in to comment.