Skip to content
Merged
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
73 changes: 33 additions & 40 deletions src/ngx_http_lua_precontentby.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,27 @@ ngx_http_lua_precontent_handler(ngx_http_request_t *r)
return rc;
}

if (rc == NGX_OK) {
if (r->header_sent
|| (r->headers_out.status != 0 && ctx->out != NULL))
{
dd("header already sent");

/* response header was already generated in precontent_by_lua*,
* so it is no longer safe to proceed to later phases
* which may generate responses again */

if (!ctx->eof) {
dd("eof not yet sent");

rc = ngx_http_lua_send_chain_link(r, ctx, NULL
/* indicate last_buf */);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}
}
if (rc == NGX_OK
&& (r->header_sent
|| (r->headers_out.status != 0 && ctx->out != NULL)))
{
dd("header already sent");

/* response header was already generated in precontent_by_lua*,
* so it is no longer safe to proceed to later phases
* which may generate responses again */

if (!ctx->eof) {
dd("eof not yet sent");

return NGX_HTTP_OK;
rc = ngx_http_lua_send_chain_link(r, ctx, NULL
/* indicate last_buf */);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}
}

return NGX_OK;
return NGX_HTTP_OK;
}

return NGX_DECLINED;
Expand Down Expand Up @@ -373,32 +370,28 @@ ngx_http_lua_precontent_by_chunk(lua_State *L, ngx_http_request_t *r)
}
}

#if 1
if (rc == NGX_OK) {
if (r->header_sent || (r->headers_out.status != 0 && ctx->out != NULL))
{
dd("header already sent");
if (rc == NGX_OK
&& (r->header_sent
|| (r->headers_out.status != 0 && ctx->out != NULL)))
{
dd("header already sent");

/* response header was already generated in precontent_by_lua*,
* so it is no longer safe to proceed to later phases
* which may generate responses again */
/* response header was already generated in precontent_by_lua*,
* so it is no longer safe to proceed to later phases
* which may generate responses again */

if (!ctx->eof) {
dd("eof not yet sent");
if (!ctx->eof) {
dd("eof not yet sent");

rc = ngx_http_lua_send_chain_link(r, ctx, NULL
/* indicate last_buf */);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}
rc = ngx_http_lua_send_chain_link(r, ctx, NULL
/* indicate last_buf */);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}

return NGX_HTTP_OK;
}

return NGX_OK;
return NGX_HTTP_OK;
}
#endif

return NGX_DECLINED;
}
Expand Down
63 changes: 62 additions & 1 deletion t/189-precontent.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ our $StapScript = $t::StapThread::StapScript;

repeat_each(2);

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

#log_level("info");
#no_long_string();
Expand Down Expand Up @@ -361,3 +361,64 @@ content phase executed
precontent_by_lua_block executed
--- no_error_log
[error]



=== TEST 16: no postpone continues to later precontent handlers
--- http_config
precontent_by_lua_no_postpone on;
--- config
location /lua {
precontent_by_lua_block {
ngx.log(ngx.INFO, "precontent_by_lua_block executed")
}
try_files /not-found @fallback;
content_by_lua_block {
ngx.say("original location")
}
}

location @fallback {
content_by_lua_block {
ngx.say("fallback location")
}
}
--- request
GET /lua
--- response_body
fallback location
--- error_log
precontent_by_lua_block executed
--- no_error_log
[error]



=== TEST 17: no postpone continues after yielding
--- http_config
precontent_by_lua_no_postpone on;
--- config
location /lua {
precontent_by_lua_block {
ngx.sleep(0.001)
ngx.log(ngx.INFO, "precontent_by_lua_block resumed")
}
try_files /not-found @fallback;
content_by_lua_block {
ngx.say("original location")
}
}

location @fallback {
content_by_lua_block {
ngx.say("fallback location")
}
}
--- request
GET /lua
--- response_body
fallback location
--- error_log
precontent_by_lua_block resumed
--- no_error_log
[error]
Loading