Skip to content

Commit c0b3de3

Browse files
committed
bugfix: change return values to NGX_DECLINED in precontent_by_lua
NGX_HTTP_PRECONTENT_PHASE uses ngx_http_core_generic_phase. Previous implementations were ported from access_by_lua*, and the handler would return NGX_OK at the end. If "preaccess_by_lua_no_postpone on;" is configured, subsequent handlers in the same phase would be skipped. Therefore, it should return NGX_DECLINED instead.
1 parent 583f0f4 commit c0b3de3

2 files changed

Lines changed: 94 additions & 38 deletions

File tree

src/ngx_http_lua_precontentby.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,27 @@ ngx_http_lua_precontent_handler(ngx_http_request_t *r)
9797
return rc;
9898
}
9999

100-
if (rc == NGX_OK) {
101-
if (r->header_sent
102-
|| (r->headers_out.status != 0 && ctx->out != NULL))
103-
{
104-
dd("header already sent");
105-
106-
/* response header was already generated in precontent_by_lua*,
107-
* so it is no longer safe to proceed to later phases
108-
* which may generate responses again */
109-
110-
if (!ctx->eof) {
111-
dd("eof not yet sent");
112-
113-
rc = ngx_http_lua_send_chain_link(r, ctx, NULL
114-
/* indicate last_buf */);
115-
if (rc == NGX_ERROR || rc > NGX_OK) {
116-
return rc;
117-
}
118-
}
100+
if (rc == NGX_OK
101+
&& (r->header_sent
102+
|| (r->headers_out.status != 0 && ctx->out != NULL)))
103+
{
104+
dd("header already sent");
105+
106+
/* response header was already generated in precontent_by_lua*,
107+
* so it is no longer safe to proceed to later phases
108+
* which may generate responses again */
109+
110+
if (!ctx->eof) {
111+
dd("eof not yet sent");
119112

120-
return NGX_HTTP_OK;
113+
rc = ngx_http_lua_send_chain_link(r, ctx, NULL
114+
/* indicate last_buf */);
115+
if (rc == NGX_ERROR || rc > NGX_OK) {
116+
return rc;
117+
}
121118
}
122119

123-
return NGX_OK;
120+
return NGX_HTTP_OK;
124121
}
125122

126123
return NGX_DECLINED;
@@ -374,29 +371,27 @@ ngx_http_lua_precontent_by_chunk(lua_State *L, ngx_http_request_t *r)
374371
}
375372

376373
#if 1
377-
if (rc == NGX_OK) {
378-
if (r->header_sent || (r->headers_out.status != 0 && ctx->out != NULL))
379-
{
380-
dd("header already sent");
374+
if (rc == NGX_OK
375+
&& (r->header_sent
376+
|| (r->headers_out.status != 0 && ctx->out != NULL)))
377+
{
378+
dd("header already sent");
381379

382-
/* response header was already generated in precontent_by_lua*,
383-
* so it is no longer safe to proceed to later phases
384-
* which may generate responses again */
380+
/* response header was already generated in precontent_by_lua*,
381+
* so it is no longer safe to proceed to later phases
382+
* which may generate responses again */
385383

386-
if (!ctx->eof) {
387-
dd("eof not yet sent");
384+
if (!ctx->eof) {
385+
dd("eof not yet sent");
388386

389-
rc = ngx_http_lua_send_chain_link(r, ctx, NULL
390-
/* indicate last_buf */);
391-
if (rc == NGX_ERROR || rc > NGX_OK) {
392-
return rc;
393-
}
387+
rc = ngx_http_lua_send_chain_link(r, ctx, NULL
388+
/* indicate last_buf */);
389+
if (rc == NGX_ERROR || rc > NGX_OK) {
390+
return rc;
394391
}
395-
396-
return NGX_HTTP_OK;
397392
}
398393

399-
return NGX_OK;
394+
return NGX_HTTP_OK;
400395
}
401396
#endif
402397

t/189-precontent.t

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,64 @@ content phase executed
361361
precontent_by_lua_block executed
362362
--- no_error_log
363363
[error]
364+
365+
366+
367+
=== TEST 16: no postpone continues to later precontent handlers
368+
--- http_config
369+
precontent_by_lua_no_postpone on;
370+
--- config
371+
location /lua {
372+
precontent_by_lua_block {
373+
ngx.log(ngx.INFO, "precontent_by_lua_block executed")
374+
}
375+
try_files /not-found @fallback;
376+
content_by_lua_block {
377+
ngx.say("original location")
378+
}
379+
}
380+
381+
location @fallback {
382+
content_by_lua_block {
383+
ngx.say("fallback location")
384+
}
385+
}
386+
--- request
387+
GET /lua
388+
--- response_body
389+
fallback location
390+
--- error_log
391+
precontent_by_lua_block executed
392+
--- no_error_log
393+
[error]
394+
395+
396+
397+
=== TEST 17: no postpone continues after yielding
398+
--- http_config
399+
precontent_by_lua_no_postpone on;
400+
--- config
401+
location /lua {
402+
precontent_by_lua_block {
403+
ngx.sleep(0.001)
404+
ngx.log(ngx.INFO, "precontent_by_lua_block resumed")
405+
}
406+
try_files /not-found @fallback;
407+
content_by_lua_block {
408+
ngx.say("original location")
409+
}
410+
}
411+
412+
location @fallback {
413+
content_by_lua_block {
414+
ngx.say("fallback location")
415+
}
416+
}
417+
--- request
418+
GET /lua
419+
--- response_body
420+
fallback location
421+
--- error_log
422+
precontent_by_lua_block resumed
423+
--- no_error_log
424+
[error]

0 commit comments

Comments
 (0)