You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/03/04 07:05:46 UTC

[apisix] branch master updated: feat: add `rewrite:RespHeaders` and modify the upstream response headers via `request` implementation (#6426)

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 c4229d1  feat: add `rewrite:RespHeaders` and modify the upstream response headers via `request` implementation (#6426)
c4229d1 is described below

commit c4229d15efe8cac6677f8124cf92d3512d1b0180
Author: 蚊子先生 <52...@users.noreply.github.com>
AuthorDate: Fri Mar 4 15:05:35 2022 +0800

    feat: add `rewrite:RespHeaders` and modify the upstream response headers via `request` implementation (#6426)
    
    Co-authored-by: 帅进超 <sh...@gmail.com>
---
 apisix/plugins/ext-plugin/init.lua  | 23 ++++++++++++++++++++
 rockspec/apisix-master-0.rockspec   |  2 +-
 t/lib/ext-plugin.lua                | 32 ++++++++++++++++++++++++++++
 t/lib/server.lua                    |  8 ++++++-
 t/plugin/ext-plugin/http-req-call.t | 42 +++++++++++++++++++++++++++++++++++++
 5 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua
index 4ba3ce5..cbf3e3e 100644
--- a/apisix/plugins/ext-plugin/init.lua
+++ b/apisix/plugins/ext-plugin/init.lua
@@ -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({
@@ -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 = {}
diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec
index 55f06b4..c28d0dc 100644
--- a/rockspec/apisix-master-0.rockspec
+++ b/rockspec/apisix-master-0.rockspec
@@ -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",
diff --git a/t/lib/ext-plugin.lua b/t/lib/ext-plugin.lua
index 33e87d5..6f93674 100644
--- a/t/lib/ext-plugin.lua
+++ b/t/lib/ext-plugin.lua
@@ -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
diff --git a/t/lib/server.lua b/t/lib/server.lua
index 0735685..029f463 100644
--- a/t/lib/server.lua
+++ b/t/lib/server.lua
@@ -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)
@@ -536,5 +543,4 @@ function _M.go()
     return _M[action]()
 end
 
-
 return _M
diff --git a/t/plugin/ext-plugin/http-req-call.t b/t/plugin/ext-plugin/http-req-call.t
index 5fc9ec2..f965932 100644
--- a/t/plugin/ext-plugin/http-req-call.t
+++ b/t/plugin/ext-plugin/http-req-call.t
@@ -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: