diff --git a/lua/apisix.lua b/lua/apisix.lua index e95bb087..8c5a8f27 100644 --- a/lua/apisix.lua +++ b/lua/apisix.lua @@ -31,6 +31,10 @@ end local function run_plugin(phase, filter_plugins, api_ctx) api_ctx = api_ctx or ngx.ctx.api_ctx + if not api_ctx then + return + end + filter_plugins = filter_plugins or api_ctx.filter_plugins if not filter_plugins then return @@ -66,7 +70,7 @@ function _M.rewrite_phase() local ok = router():dispatch(method, uri, api_ctx) if not ok then core.log.warn("not find any matched route") - return core.response.say(404) + return core.response.exit(404) end local local_plugins = core.lrucache.global("/local_plugins", nil, @@ -103,7 +107,7 @@ end function _M.balancer_phase() local api_ctx = ngx.ctx.api_ctx - if not api_ctx.filter_plugins then + if not api_ctx or not api_ctx.filter_plugins then return end diff --git a/lua/apisix/core/response.lua b/lua/apisix/core/response.lua index d8970cab..f6909e84 100644 --- a/lua/apisix/core/response.lua +++ b/lua/apisix/core/response.lua @@ -9,7 +9,6 @@ local select = select local type = type local ngx_exit = ngx.exit local insert_tab = table.insert -local clear_tab = table.clear local concat_tab = table.concat @@ -19,9 +18,10 @@ local _M = {version = 0.1} local resp_exit do local t = {} + local idx = 1 function resp_exit(code, ...) - clear_tab(t) + idx = 0 if type(code) ~= "number" then insert_tab(t, code) @@ -39,15 +39,19 @@ function resp_exit(code, ...) if err then error("failed to encode data: " .. err, -2) else - insert_tab(t, body) + idx = idx + 1 + insert_tab(t, idx, body) end else - insert_tab(t, v) + idx = idx + 1 + insert_tab(t, idx, v) end end - ngx_say(concat_tab(t)) + if idx > 0 then + ngx_say(concat_tab(t, "", 1, idx)) + end if code then ngx_exit(code) diff --git a/lua/apisix/plugins/example-plugin.lua b/lua/apisix/plugins/example-plugin.lua index 5df17f50..577943e8 100644 --- a/lua/apisix/plugins/example-plugin.lua +++ b/lua/apisix/plugins/example-plugin.lua @@ -36,8 +36,7 @@ end function _M.access(conf, ctx) core.log.warn("plugin access phase, conf: ", core.json.encode(conf)) - -- core.log.warn(" ctx: ", core.json.encode(ctx, true)) - ngx.say("hit example plugin") + -- return 200, {message = "hit example plugin"} end