From 9e1cd4388a50d3ea6eae06472230c06bb257e98a Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Mon, 6 Jan 2025 23:46:19 -0800 Subject: [PATCH 1/3] SSI: prevent fallback from emitting twice --- src/http/ngx_http_request.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index 3cca57cf5..3459d2726 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -2526,6 +2526,7 @@ ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc) if (r != r->main && r->post_subrequest) { rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc); + r->post_subrequest = NULL; } if (rc == NGX_ERROR From b9ab9562d59548178e7207d34bb343b63372682b Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Thu, 6 Mar 2025 09:01:17 -0800 Subject: [PATCH 2/3] SSI: prevent fallback from emitting twice --- src/http/modules/ngx_http_ssi_filter_module.c | 20 ++++++++++++++++++- src/http/modules/ngx_http_ssi_filter_module.h | 2 ++ src/http/ngx_http_request.c | 1 - 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/http/modules/ngx_http_ssi_filter_module.c b/src/http/modules/ngx_http_ssi_filter_module.c index 47068f755..cdb5356d4 100644 --- a/src/http/modules/ngx_http_ssi_filter_module.c +++ b/src/http/modules/ngx_http_ssi_filter_module.c @@ -2232,12 +2232,30 @@ ngx_http_ssi_include(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx, static ngx_int_t ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc) { - ngx_chain_t *out; + ngx_chain_t *out; + ngx_http_ssi_ctx_t *ctx; if (rc == NGX_ERROR || r->connection->error || r->request_output) { return rc; } + ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module); + + if (ctx && ctx->stub_output_done) { + return NGX_OK; + } + + if (ctx == NULL) { + ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t)); + if (ctx == NULL) { + return NGX_ERROR; + } + + ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module); + } + + ctx->stub_output_done = 1; + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ssi stub output: \"%V?%V\"", &r->uri, &r->args); diff --git a/src/http/modules/ngx_http_ssi_filter_module.h b/src/http/modules/ngx_http_ssi_filter_module.h index 419bf977d..eb61040be 100644 --- a/src/http/modules/ngx_http_ssi_filter_module.h +++ b/src/http/modules/ngx_http_ssi_filter_module.h @@ -82,6 +82,8 @@ typedef struct { void *value_buf; ngx_str_t timefmt; ngx_str_t errmsg; + + ngx_uint_t stub_output_done; } ngx_http_ssi_ctx_t; diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index 3459d2726..3cca57cf5 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -2526,7 +2526,6 @@ ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc) if (r != r->main && r->post_subrequest) { rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc); - r->post_subrequest = NULL; } if (rc == NGX_ERROR From 4f519a66f364a7ec40399dae25e411a37ed565c5 Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Sun, 9 Mar 2025 17:56:54 -0700 Subject: [PATCH 3/3] SSI: correct context initialization --- src/http/modules/ngx_http_ssi_filter_module.c | 57 ++++++++++++------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/http/modules/ngx_http_ssi_filter_module.c b/src/http/modules/ngx_http_ssi_filter_module.c index cdb5356d4..82d47d361 100644 --- a/src/http/modules/ngx_http_ssi_filter_module.c +++ b/src/http/modules/ngx_http_ssi_filter_module.c @@ -325,6 +325,39 @@ static ngx_http_variable_t ngx_http_ssi_vars[] = { }; +static ngx_http_ssi_ctx_t * +ngx_http_ssi_create_ctx(ngx_http_request_t *r) +{ + ngx_http_ssi_ctx_t *ctx; + ngx_http_ssi_loc_conf_t *slcf; + + ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t)); + if (ctx == NULL) { + return NULL; + } + + ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module); + + slcf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module); + + ctx->value_len = slcf->value_len; + ctx->last_out = &ctx->out; + + ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING; + ctx->output = 1; + + ctx->params.elts = ctx->params_array; + ctx->params.size = sizeof(ngx_table_elt_t); + ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N; + ctx->params.pool = r->pool; + + ctx->timefmt = ngx_http_ssi_timefmt; + ngx_str_set(&ctx->errmsg, + "[an error occurred while processing the directive]"); + + return ctx; +} + static ngx_int_t ngx_http_ssi_header_filter(ngx_http_request_t *r) @@ -343,29 +376,11 @@ ngx_http_ssi_header_filter(ngx_http_request_t *r) mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module); - ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t)); + ctx = ngx_http_ssi_create_ctx(r); if (ctx == NULL) { return NGX_ERROR; } - ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module); - - - ctx->value_len = slcf->value_len; - ctx->last_out = &ctx->out; - - ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING; - ctx->output = 1; - - ctx->params.elts = ctx->params_array; - ctx->params.size = sizeof(ngx_table_elt_t); - ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N; - ctx->params.pool = r->pool; - - ctx->timefmt = ngx_http_ssi_timefmt; - ngx_str_set(&ctx->errmsg, - "[an error occurred while processing the directive]"); - r->filter_need_in_memory = 1; if (r == r->main) { @@ -2246,12 +2261,10 @@ ngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc) } if (ctx == NULL) { - ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t)); + ctx = ngx_http_ssi_create_ctx(r); if (ctx == NULL) { return NGX_ERROR; } - - ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module); } ctx->stub_output_done = 1;