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/04/18 06:39:51 UTC

[apisix] branch master updated: feat(serverless): return like other plugins (#6870)

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 b08d8819e feat(serverless): return like other plugins (#6870)
b08d8819e is described below

commit b08d8819e2435cdb6099420925bfe228062708da
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Mon Apr 18 14:39:47 2022 +0800

    feat(serverless): return like other plugins (#6870)
    
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/plugins/serverless/init.lua | 41 ++++++++++++-------------------
 t/plugin/serverless.t              | 50 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 25 deletions(-)

diff --git a/apisix/plugins/serverless/init.lua b/apisix/plugins/serverless/init.lua
index 65ee7e81f..6ed8c9604 100644
--- a/apisix/plugins/serverless/init.lua
+++ b/apisix/plugins/serverless/init.lua
@@ -20,6 +20,13 @@ local loadstring = loadstring
 local require = require
 local type = type
 
+
+local phases = {
+    "rewrite", "access", "header_filter", "body_filter",
+    "log", "before_proxy"
+}
+
+
 return function(plugin_name, priority)
     local core = require("apisix.core")
 
@@ -34,8 +41,7 @@ return function(plugin_name, priority)
             phase = {
                 type = "string",
                 default = "access",
-                enum = {"rewrite", "access", "header_filter", "body_filter",
-                        "log", "before_proxy"}
+                enum = phases,
             },
             functions = {
                 type = "array",
@@ -75,7 +81,10 @@ return function(plugin_name, priority)
                                                    load_funcs, conf.functions)
 
         for _, func in ipairs(functions) do
-            func(conf, ctx)
+            local code, body = func(conf, ctx)
+            if code or body then
+                return code, body
+            end
         end
     end
 
@@ -105,28 +114,10 @@ return function(plugin_name, priority)
         return true
     end
 
-    function _M.rewrite(conf, ctx)
-        call_funcs('rewrite', conf, ctx)
-    end
-
-    function _M.access(conf, ctx)
-        call_funcs('access', conf, ctx)
-    end
-
-    function _M.before_proxy(conf, ctx)
-        call_funcs('before_proxy', conf, ctx)
-    end
-
-    function _M.header_filter(conf, ctx)
-        call_funcs('header_filter', conf, ctx)
-    end
-
-    function _M.body_filter(conf, ctx)
-        call_funcs('body_filter', conf, ctx)
-    end
-
-    function _M.log(conf, ctx)
-        call_funcs('log', conf, ctx)
+    for _, phase in ipairs(phases) do
+        _M[phase] = function (conf, ctx)
+            return call_funcs(phase, conf, ctx)
+        end
     end
 
     return _M
diff --git a/t/plugin/serverless.t b/t/plugin/serverless.t
index 3d891aaa3..e2cfeac0c 100644
--- a/t/plugin/serverless.t
+++ b/t/plugin/serverless.t
@@ -590,3 +590,53 @@ GET /echo?args=%40%23%24%25%5E%26
 args=@#$%^&
 --- no_error_log
 [error]
+
+
+
+=== TEST 25: return status code should exit the request like other plugins
+--- 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,
+                 [[{
+                    "plugins": {
+                        "serverless-post-function": {
+                        "functions" : ["return function(conf, ctx) return 403, 'forbidden' end",
+                                       "return function(conf, ctx) ngx.log(ngx.ERR, 'unreachable') end"]
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 26: check plugin
+--- request
+GET /hello
+--- error_code: 403
+--- response_body chomp
+forbidden
+--- no_error_log
+[error]