Skip to content

Commit 1af1ee4

Browse files
authored
feat: allow skipping filters (#63)
1 parent 39771eb commit 1af1ee4

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

lib/resty/apisix/response.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ typedef intptr_t ngx_int_t;
1313
ngx_int_t
1414
ngx_http_apisix_set_gzip(ngx_http_request_t *r, ngx_int_t num, size_t size,
1515
ngx_int_t level);
16+
ngx_int_t
17+
ngx_http_apisix_skip_header_filter_by_lua(ngx_http_request_t *r);
18+
ngx_int_t
19+
ngx_http_apisix_skip_body_filter_by_lua(ngx_http_request_t *r);
1620
]]
1721

1822

@@ -33,4 +37,26 @@ function _M.set_gzip(opts)
3337
end
3438

3539

40+
-- The skip_* methods must be called before any output is generated,
41+
-- so the flag can take effect
42+
function _M.skip_header_filter_by_lua()
43+
local r = get_request()
44+
local rc = C.ngx_http_apisix_skip_header_filter_by_lua(r)
45+
if rc == NGX_ERROR then
46+
return nil, "no memory"
47+
end
48+
return true
49+
end
50+
51+
52+
function _M.skip_body_filter_by_lua()
53+
local r = get_request()
54+
local rc = C.ngx_http_apisix_skip_body_filter_by_lua(r)
55+
if rc == NGX_ERROR then
56+
return nil, "no memory"
57+
end
58+
return true
59+
end
60+
61+
3662
return _M
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
diff --git src/ngx_http_lua_bodyfilterby.c src/ngx_http_lua_bodyfilterby.c
2+
index 9024889..88af761 100644
3+
--- src/ngx_http_lua_bodyfilterby.c
4+
+++ src/ngx_http_lua_bodyfilterby.c
5+
@@ -22,6 +22,9 @@
6+
#include "ngx_http_lua_misc.h"
7+
#include "ngx_http_lua_consts.h"
8+
#include "ngx_http_lua_output.h"
9+
+#if (NGX_HTTP_APISIX)
10+
+#include "ngx_http_apisix_module.h"
11+
+#endif
12+
13+
14+
static void ngx_http_lua_body_filter_by_lua_env(lua_State *L,
15+
@@ -241,6 +244,12 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
16+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
17+
"lua body filter for user lua code, uri \"%V\"", &r->uri);
18+
19+
+#if (NGX_HTTP_APISIX)
20+
+ if (ngx_http_apisix_is_body_filter_by_lua_skipped(r)) {
21+
+ return ngx_http_next_body_filter(r, in);
22+
+ }
23+
+#endif
24+
+
25+
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
26+
27+
if (llcf->body_filter_handler == NULL || r->header_only) {
28+
diff --git src/ngx_http_lua_headerfilterby.c src/ngx_http_lua_headerfilterby.c
29+
index ed0c3a6..5f04992 100644
30+
--- src/ngx_http_lua_headerfilterby.c
31+
+++ src/ngx_http_lua_headerfilterby.c
32+
@@ -19,6 +19,9 @@
33+
#include "ngx_http_lua_string.h"
34+
#include "ngx_http_lua_misc.h"
35+
#include "ngx_http_lua_consts.h"
36+
+#if (NGX_HTTP_APISIX)
37+
+#include "ngx_http_apisix_module.h"
38+
+#endif
39+
40+
41+
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
42+
@@ -80,6 +83,12 @@ ngx_http_lua_header_filter_by_chunk(lua_State *L, ngx_http_request_t *r)
43+
#endif
44+
ngx_http_lua_ctx_t *ctx;
45+
46+
+#if (NGX_HTTP_APISIX)
47+
+ if (ngx_http_apisix_is_header_filter_by_lua_skipped(r)) {
48+
+ return NGX_OK;
49+
+ }
50+
+#endif
51+
+
52+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
53+
if (ctx->exited) {
54+
old_exit_code = ctx->exit_code;

src/ngx_http_apisix_module.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,69 @@ ngx_http_apisix_mark_request_header_set(ngx_http_request_t *r)
574574

575575
ctx->request_header_set = 1;
576576
}
577+
578+
579+
ngx_int_t
580+
ngx_http_apisix_skip_header_filter_by_lua(ngx_http_request_t *r)
581+
{
582+
ngx_http_apisix_ctx_t *ctx;
583+
584+
ctx = ngx_http_apisix_get_module_ctx(r);
585+
if (ctx == NULL) {
586+
return NGX_ERROR;
587+
}
588+
589+
ctx->header_filter_by_lua_skipped = 1;
590+
return NGX_OK;
591+
}
592+
593+
594+
ngx_int_t
595+
ngx_http_apisix_is_header_filter_by_lua_skipped(ngx_http_request_t *r)
596+
{
597+
ngx_http_apisix_ctx_t *ctx;
598+
599+
ctx = ngx_http_apisix_get_module_ctx(r);
600+
if (ctx != NULL) {
601+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
602+
"apisix header_filter_by_lua %p skipped: %d",
603+
ctx, ctx->header_filter_by_lua_skipped);
604+
605+
return ctx->header_filter_by_lua_skipped;
606+
}
607+
608+
return 0;
609+
}
610+
611+
612+
ngx_int_t
613+
ngx_http_apisix_skip_body_filter_by_lua(ngx_http_request_t *r)
614+
{
615+
ngx_http_apisix_ctx_t *ctx;
616+
617+
ctx = ngx_http_apisix_get_module_ctx(r);
618+
if (ctx == NULL) {
619+
return NGX_ERROR;
620+
}
621+
622+
ctx->body_filter_by_lua_skipped = 1;
623+
return NGX_OK;
624+
}
625+
626+
627+
ngx_int_t
628+
ngx_http_apisix_is_body_filter_by_lua_skipped(ngx_http_request_t *r)
629+
{
630+
ngx_http_apisix_ctx_t *ctx;
631+
632+
ctx = ngx_http_apisix_get_module_ctx(r);
633+
if (ctx != NULL) {
634+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
635+
"apisix body_filter_by_lua %p skipped: %d",
636+
ctx, ctx->body_filter_by_lua_skipped);
637+
638+
return ctx->body_filter_by_lua_skipped;
639+
}
640+
641+
return 0;
642+
}

src/ngx_http_apisix_module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ typedef struct {
3030
unsigned request_buffering:1;
3131
unsigned request_buffering_set:1;
3232
unsigned request_header_set:1;
33+
unsigned header_filter_by_lua_skipped:1;
34+
unsigned body_filter_by_lua_skipped:1;
3335
} ngx_http_apisix_ctx_t;
3436

3537

@@ -50,4 +52,8 @@ ngx_int_t ngx_http_apisix_is_request_buffering(ngx_http_request_t *r, ngx_flag_t
5052

5153
void ngx_http_apisix_mark_request_header_set(ngx_http_request_t *r);
5254

55+
ngx_int_t ngx_http_apisix_is_header_filter_by_lua_skipped(ngx_http_request_t *r);
56+
ngx_int_t ngx_http_apisix_is_body_filter_by_lua_skipped(ngx_http_request_t *r);
57+
58+
5359
#endif /* _NGX_HTTP_APISIX_H_INCLUDED_ */

t/skip_filter.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use t::APISIX_NGINX 'no_plan';
2+
3+
run_tests;
4+
5+
__DATA__
6+
7+
=== TEST 1: skip header_filter_by_lua
8+
--- config
9+
location /t {
10+
access_by_lua_block {
11+
local resp = require("resty.apisix.response")
12+
assert(resp.skip_header_filter_by_lua())
13+
14+
ngx.header["Test"] = "one"
15+
ngx.say("ok")
16+
}
17+
header_filter_by_lua_block {
18+
ngx.header["Test"] = "two"
19+
}
20+
}
21+
--- response_headers
22+
Test: one
23+
24+
25+
26+
=== TEST 2: skip body_filter_by_lua
27+
--- config
28+
location /t {
29+
access_by_lua_block {
30+
local resp = require("resty.apisix.response")
31+
assert(resp.skip_body_filter_by_lua())
32+
33+
ngx.say("ok")
34+
}
35+
body_filter_by_lua_block {
36+
ngx.arg[1] = "no"
37+
ngx.arg[2] = true
38+
}
39+
}
40+
--- response_body
41+
ok

0 commit comments

Comments
 (0)