Skip to content

Commit b75aa93

Browse files
committed
feature: added new config directive "access_by_lua_no_postpone". thanks Delta Yeh for the patch in openresty#580.
1 parent 7c28017 commit b75aa93

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

README.markdown

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ Directives
10451045
* [lua_ssl_verify_depth](#lua_ssl_verify_depth)
10461046
* [lua_http10_buffering](#lua_http10_buffering)
10471047
* [rewrite_by_lua_no_postpone](#rewrite_by_lua_no_postpone)
1048+
* [access_by_lua_no_postpone](#access_by_lua_no_postpone)
10481049
* [lua_transform_underscores_in_response_headers](#lua_transform_underscores_in_response_headers)
10491050
* [lua_check_client_abort](#lua_check_client_abort)
10501051
* [lua_max_pending_timers](#lua_max_pending_timers)
@@ -1832,6 +1833,10 @@ As with other access phase handlers, [access_by_lua](#access_by_lua) will *not*
18321833

18331834
Note that when calling `ngx.exit(ngx.OK)` within a [access_by_lua](#access_by_lua) handler, the nginx request processing control flow will still continue to the content handler. To terminate the current request from within a [access_by_lua](#access_by_lua) handler, calling [ngx.exit](#ngxexit) with status >= 200 (`ngx.HTTP_OK`) and status < 300 (`ngx.HTTP_SPECIAL_RESPONSE`) for successful quits and `ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)` (or its friends) for failures.
18341835

1836+
Starting from the `v0.9.20` release, you can use the [access_by_lua_no_postpone](#access_by_lua_no_postpone)
1837+
directive to control when to run this handler inside the "access" request-processing phase
1838+
of NGINX.
1839+
18351840
[Back to TOC](#directives)
18361841

18371842
access_by_lua_block
@@ -2521,12 +2526,27 @@ rewrite_by_lua_no_postpone
25212526

25222527
**context:** *http*
25232528

2524-
Controls whether or not to disable postponing [rewrite_by_lua](#rewrite_by_lua) and [rewrite_by_lua_file](#rewrite_by_lua_file) directives to run at the end of the `rewrite` request-processing phase. By default, this directive is turned off and the Lua code is postponed to run at the end of the `rewrite` phase.
2529+
Controls whether or not to disable postponing [rewrite_by_lua](#rewrite_by_lua)* directives to run at the end of the `rewrite` request-processing phase. By default, this directive is turned off and the Lua code is postponed to run at the end of the `rewrite` phase.
25252530

25262531
This directive was first introduced in the `v0.5.0rc29` release.
25272532

25282533
[Back to TOC](#directives)
25292534

2535+
access_by_lua_no_postpone
2536+
-------------------------
2537+
2538+
**syntax:** *access_by_lua_no_postpone on|off*
2539+
2540+
**default:** *access_by_lua_no_postpone off*
2541+
2542+
**context:** *http*
2543+
2544+
Controls whether or not to disable postponing [access_by_lua](#access_by_lua)* directives to run at the end of the `access` request-processing phase. By default, this directive is turned off and the Lua code is postponed to run at the end of the `access` phase.
2545+
2546+
This directive was first introduced in the `v0.9.20` release.
2547+
2548+
[Back to TOC](#directives)
2549+
25302550
lua_transform_underscores_in_response_headers
25312551
---------------------------------------------
25322552

doc/HttpLuaModule.wiki

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,10 @@ As with other access phase handlers, [[#access_by_lua|access_by_lua]] will ''not
15141514
15151515
Note that when calling <code>ngx.exit(ngx.OK)</code> within a [[#access_by_lua|access_by_lua]] handler, the nginx request processing control flow will still continue to the content handler. To terminate the current request from within a [[#access_by_lua|access_by_lua]] handler, calling [[#ngx.exit|ngx.exit]] with status >= 200 (<code>ngx.HTTP_OK</code>) and status < 300 (<code>ngx.HTTP_SPECIAL_RESPONSE</code>) for successful quits and <code>ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)</code> (or its friends) for failures.
15161516
1517+
Starting from the <code>v0.9.20</code> release, you can use the [[#access_by_lua_no_postpone|access_by_lua_no_postpone]]
1518+
directive to control when to run this handler inside the "access" request-processing phase
1519+
of NGINX.
1520+
15171521
== access_by_lua_block ==
15181522
15191523
'''syntax:''' ''access_by_lua_block { lua-script }''
@@ -2108,10 +2112,22 @@ This directive was first introduced in the <code>v0.5.0rc19</code> release.
21082112
21092113
'''context:''' ''http''
21102114
2111-
Controls whether or not to disable postponing [[#rewrite_by_lua|rewrite_by_lua]] and [[#rewrite_by_lua_file|rewrite_by_lua_file]] directives to run at the end of the <code>rewrite</code> request-processing phase. By default, this directive is turned off and the Lua code is postponed to run at the end of the <code>rewrite</code> phase.
2115+
Controls whether or not to disable postponing [[#rewrite_by_lua|rewrite_by_lua]]* directives to run at the end of the <code>rewrite</code> request-processing phase. By default, this directive is turned off and the Lua code is postponed to run at the end of the <code>rewrite</code> phase.
21122116
21132117
This directive was first introduced in the <code>v0.5.0rc29</code> release.
21142118
2119+
== access_by_lua_no_postpone ==
2120+
2121+
'''syntax:''' ''access_by_lua_no_postpone on|off''
2122+
2123+
'''default:''' ''access_by_lua_no_postpone off''
2124+
2125+
'''context:''' ''http''
2126+
2127+
Controls whether or not to disable postponing [[#access_by_lua|access_by_lua]]* directives to run at the end of the <code>access</code> request-processing phase. By default, this directive is turned off and the Lua code is postponed to run at the end of the <code>access</code> phase.
2128+
2129+
This directive was first introduced in the <code>v0.9.20</code> release.
2130+
21152131
== lua_transform_underscores_in_response_headers ==
21162132
21172133
'''syntax:''' ''lua_transform_underscores_in_response_headers on|off''

src/ngx_http_lua_module.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ static ngx_command_t ngx_http_lua_cmds[] = {
313313
0,
314314
(void *) ngx_http_lua_access_handler_file },
315315

316+
{ ngx_string("access_by_lua_no_postpone"),
317+
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
318+
ngx_conf_set_flag_slot,
319+
NGX_HTTP_MAIN_CONF_OFFSET,
320+
offsetof(ngx_http_lua_main_conf_t, postponed_to_access_phase_end),
321+
NULL },
322+
316323
/* content_by_lua_file rel/or/abs/path/to/script */
317324
{ ngx_string("content_by_lua_file"),
318325
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
@@ -569,6 +576,10 @@ ngx_http_lua_init(ngx_conf_t *cf)
569576
lmcf->postponed_to_rewrite_phase_end = 0;
570577
}
571578

579+
if (lmcf->postponed_to_access_phase_end == NGX_CONF_UNSET) {
580+
lmcf->postponed_to_access_phase_end = 0;
581+
}
582+
572583
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
573584

574585
if (lmcf->requires_rewrite) {
@@ -719,6 +730,7 @@ ngx_http_lua_create_main_conf(ngx_conf_t *cf)
719730
lmcf->regex_match_limit = NGX_CONF_UNSET;
720731
#endif
721732
lmcf->postponed_to_rewrite_phase_end = NGX_CONF_UNSET;
733+
lmcf->postponed_to_access_phase_end = NGX_CONF_UNSET;
722734

723735
dd("nginx Lua module main config structure initialized!");
724736

t/076-no-postpone.t

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;
99
repeat_each(2);
1010
#repeat_each(1);
1111

12-
plan tests => repeat_each() * (blocks() * 3);
12+
plan tests => repeat_each() * (blocks() * 3 + 1);
1313

1414
#no_diff();
1515
#no_long_string();
@@ -82,3 +82,65 @@ no foo: 1
8282
--- no_error_log
8383
[error]
8484
85+
86+
87+
=== TEST 4: access no postpone on
88+
--- http_config
89+
access_by_lua_no_postpone on;
90+
--- config
91+
location /t {
92+
access_by_lua '
93+
ngx.redirect("http://www.taobao.com/foo")
94+
ngx.say("hi")
95+
';
96+
content_by_lua 'return';
97+
deny all;
98+
}
99+
--- request
100+
GET /t
101+
--- response_headers
102+
Location: http://www.taobao.com/foo
103+
--- response_body_like: 302 Found
104+
--- error_code: 302
105+
--- no_error_log
106+
[error]
107+
108+
109+
110+
=== TEST 5: access no postpone explicitly off
111+
--- http_config
112+
access_by_lua_no_postpone off;
113+
--- config
114+
location /t {
115+
access_by_lua '
116+
ngx.redirect("http://www.taobao.com/foo")
117+
ngx.say("hi")
118+
';
119+
content_by_lua 'return';
120+
deny all;
121+
}
122+
--- request
123+
GET /t
124+
--- response_body_like: 403 Forbidden
125+
--- error_code: 403
126+
--- error_log
127+
access forbidden by rule
128+
129+
130+
131+
=== TEST 6: access no postpone off by default
132+
--- config
133+
location /t {
134+
access_by_lua '
135+
ngx.redirect("http://www.taobao.com/foo")
136+
ngx.say("hi")
137+
';
138+
content_by_lua 'return';
139+
deny all;
140+
}
141+
--- request
142+
GET /t
143+
--- response_body_like: 403 Forbidden
144+
--- error_code: 403
145+
--- error_log
146+
access forbidden by rule

0 commit comments

Comments
 (0)