Skip to content

feature: allow use of ngx.exit() in the context of body_filter_by_lua #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5460,7 +5460,7 @@ ngx.exit

**syntax:** *ngx.exit(status)*

**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua**
**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua**

When `status >= 200` (i.e., `ngx.HTTP_OK` and above), it will interrupt the execution of the current request and return status code to Nginx.

Expand Down Expand Up @@ -5505,7 +5505,7 @@ Note that while this method accepts all [HTTP status constants](#http-status-con

Also note that this method call terminates the processing of the current request and that it is recommended that a coding style that combines this method call with the `return` statement, i.e., `return ngx.exit(...)` be used to reinforce the fact that the request processing is being terminated.

When being used in the contexts of [header_filter_by_lua*](#header_filter_by_lua), [balancer_by_lua*](#balancer_by_lua_block), and
When being used in the contexts of [header_filter_by_lua*](#header_filter_by_lua), [body_filter_by_lua*](#body_filter_by_lua_block), [balancer_by_lua*](#balancer_by_lua_block), and
[ssl_session_store_by_lua*](#ssl_session_store_by_lua_block), `ngx.exit()` is
an asynchronous operation and will return immediately. This behavior may change in future and it is recommended that users always use `return` in combination as suggested above.

Expand Down
4 changes: 2 additions & 2 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -4579,7 +4579,7 @@ Since <code>v0.8.3</code> this function returns <code>1</code> on success, or re

'''syntax:''' ''ngx.exit(status)''

'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''

When <code>status >= 200</code> (i.e., <code>ngx.HTTP_OK</code> and above), it will interrupt the execution of the current request and return status code to Nginx.

Expand Down Expand Up @@ -4621,7 +4621,7 @@ Note that while this method accepts all [[#HTTP status constants|HTTP status con

Also note that this method call terminates the processing of the current request and that it is recommended that a coding style that combines this method call with the <code>return</code> statement, i.e., <code>return ngx.exit(...)</code> be used to reinforce the fact that the request processing is being terminated.

When being used in the contexts of [[#header_filter_by_lua|header_filter_by_lua*]], [[#balancer_by_lua_block|balancer_by_lua*]], and
When being used in the contexts of [[#header_filter_by_lua|header_filter_by_lua*]], [[#body_filter_by_lua_block|body_filter_by_lua*]], [[#balancer_by_lua_block|balancer_by_lua*]], and
[[#ssl_session_store_by_lua_block|ssl_session_store_by_lua*]], <code>ngx.exit()</code> is
an asynchronous operation and will return immediately. This behavior may change in future and it is recommended that users always use <code>return</code> in combination as suggested above.

Expand Down
42 changes: 22 additions & 20 deletions src/ngx_http_lua_bodyfilterby.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ ngx_int_t
ngx_http_lua_body_filter_by_chunk(lua_State *L, ngx_http_request_t *r,
ngx_chain_t *in)
{
ngx_int_t rc;
u_char *err_msg;
size_t len;
ngx_int_t rc;
u_char *err_msg;
size_t len;
#if (NGX_PCRE)
ngx_pool_t *old_pool;
ngx_pool_t *old_pool;
#endif
ngx_http_lua_ctx_t *ctx;

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);

dd("initialize nginx context in Lua VM, code chunk at stack top sp = 1");
ngx_http_lua_body_filter_by_lua_env(L, r, in);
Expand All @@ -110,6 +113,8 @@ ngx_http_lua_body_filter_by_chunk(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_pcre_malloc_done(old_pool);
#endif

dd("rc == %d", (int) rc);

if (rc != 0) {

/* error occurred */
Expand All @@ -136,7 +141,10 @@ ngx_http_lua_body_filter_by_chunk(lua_State *L, ngx_http_request_t *r,

lua_settop(L, 0);

if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
if (ctx->body_filter_aborted
|| rc == NGX_ERROR
|| rc >= NGX_HTTP_SPECIAL_RESPONSE)
{
return NGX_ERROR;
}

Expand Down Expand Up @@ -166,15 +174,9 @@ ngx_http_lua_body_filter_inline(ngx_http_request_t *r, ngx_chain_t *in)
return NGX_ERROR;
}

rc = ngx_http_lua_body_filter_by_chunk(L, r, in);

dd("body filter by chunk returns %d", (int) rc);

if (rc != NGX_OK) {
return NGX_ERROR;
}
dd("calling body filter by chunk");

return NGX_OK;
return ngx_http_lua_body_filter_by_chunk(L, r, in);
}


Expand Down Expand Up @@ -216,13 +218,7 @@ ngx_http_lua_body_filter_file(ngx_http_request_t *r, ngx_chain_t *in)
/* make sure we have a valid code chunk */
ngx_http_lua_assert(lua_isfunction(L, -1));

rc = ngx_http_lua_body_filter_by_chunk(L, r, in);

if (rc != NGX_OK) {
return NGX_ERROR;
}

return NGX_OK;
return ngx_http_lua_body_filter_by_chunk(L, r, in);
}


Expand Down Expand Up @@ -262,6 +258,12 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
}
}

if (ctx->body_filter_aborted) {
/* maybe there is a bug in the output body filter caller, other
* outputs will be intercepted here */
return NGX_ERROR;
}

if (ctx->seen_last_in_filter) {
for (/* void */; in; in = in->next) {
dd("mark the buf as consumed: %d", (int) ngx_buf_size(in->buf));
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ typedef struct ngx_http_lua_ctx_s {

unsigned exited:1;

unsigned body_filter_aborted:1;

unsigned eof:1; /* 1: last_buf has been sent;
0: last_buf not sent yet */

Expand Down
14 changes: 14 additions & 0 deletions src/ngx_http_lua_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
| NGX_HTTP_LUA_CONTEXT_CONTENT
| NGX_HTTP_LUA_CONTEXT_TIMER
| NGX_HTTP_LUA_CONTEXT_HEADER_FILTER
| NGX_HTTP_LUA_CONTEXT_BODY_FILTER
| NGX_HTTP_LUA_CONTEXT_BALANCER
| NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
Expand All @@ -375,6 +376,19 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
return NGX_ERROR;
}

if (ctx->context & NGX_HTTP_LUA_CONTEXT_BODY_FILTER) {

if (status != NGX_ERROR && status != NGX_HTTP_CLOSE) {
*errlen = ngx_snprintf(err, *errlen, "attempt to exit with the "
"code not 444 or ngx.ERROR")
- err;
return NGX_ERROR;
}

ctx->body_filter_aborted = 1;
return NGX_DONE;
}

if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH))
Expand Down
45 changes: 44 additions & 1 deletion t/082-body-filter.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ log_level('debug');

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3 + 11);
plan tests => repeat_each() * (blocks() * 3 + 8);

#no_diff();
no_long_string();
Expand Down Expand Up @@ -838,3 +838,46 @@ GET /lua
--- ignore_response
--- error_log
API disabled in the context of body_filter_by_lua*



=== TEST 27: exit 444
--- config
location = /t {
content_by_lua_block {
ngx.say("111")
ngx.say("222")
ngx.say("333")
}

body_filter_by_lua_block {
ngx.exit(444)
}
}
--- request
GET /t
--- ignore_response
--- no_error_log
[error]
[alert]



=== TEST 28: exit OK
--- config
location = /t {
content_by_lua_block {
ngx.say("111")
ngx.say("222")
ngx.say("333")
}

body_filter_by_lua_block {
ngx.exit(200)
}
}
--- request
GET /t
--- ignore_response
--- error_log
attempt to exit with the code not 444 or ngx.ERROR