Skip to content

Commit bceb3ef

Browse files
committed
add ngx error log while get wrong code in ngx.exit
1 parent 8a4850d commit bceb3ef

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/ngx_http_lua_control.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,20 @@ ngx_http_lua_ngx_exit(lua_State *L)
325325
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
326326
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH);
327327

328-
/* rc is useless in bodyfilter phase */
328+
rc = (ngx_int_t) luaL_checkinteger(L, 1);
329+
329330
if (ctx->context & NGX_HTTP_LUA_CONTEXT_BODY_FILTER) {
331+
332+
if (rc != NGX_ERROR && rc < 400) {
333+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to "
334+
"exit with the code smaller than 400 and not "
335+
"ngx.ERROR");
336+
}
337+
330338
ctx->body_exited = 1;
331339
return 0;
332340
}
333341

334-
rc = (ngx_int_t) luaL_checkinteger(L, 1);
335-
336342
if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SSL_CERT
337343
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
338344
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH))
@@ -488,8 +494,14 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
488494
return NGX_ERROR;
489495
}
490496

491-
/* rc is useless in bodyfilter phase */
492497
if (ctx->context & NGX_HTTP_LUA_CONTEXT_BODY_FILTER) {
498+
499+
if (status != NGX_ERROR && status < 400) {
500+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to "
501+
"exit with the code smaller than 400 and not "
502+
"ngx.ERROR");
503+
}
504+
493505
ctx->body_exited = 1;
494506
return NGX_OK;
495507
}

t/082-body-filter.t

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ log_level('debug');
1010

1111
repeat_each(2);
1212

13-
plan tests => repeat_each() * (blocks() * 3 + 8);
13+
plan tests => repeat_each() * (blocks() * 3 + 13);
1414

1515
#no_diff();
1616
no_long_string();
@@ -859,6 +859,8 @@ API disabled in the context of body_filter_by_lua*
859859
111
860860
--- error_code: 200
861861
--- no_error_log
862+
[error]
863+
[alert]
862864
863865
864866
@@ -880,7 +882,8 @@ API disabled in the context of body_filter_by_lua*
880882
--- response_body
881883
111
882884
--- error_code: 200
883-
--- no_error_log
885+
--- error_log
886+
attempt to exit with the code smaller than 400 and not ngx.ERROR
884887
885888
886889
@@ -907,3 +910,5 @@ API disabled in the context of body_filter_by_lua*
907910
222
908911
--- error_code: 200
909912
--- no_error_log
913+
[error]
914+
[alert]

0 commit comments

Comments
 (0)