[nginx] Perl: additional ctx->header_sent checks.

Maxim Dounin mdounin at mdounin.ru
Fri Jul 12 14:54:02 UTC 2019


details:   https://hg.nginx.org/nginx/rev/ede052c67512
branches:  
changeset: 7531:ede052c67512
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Fri Jul 12 15:39:25 2019 +0300
description:
Perl: additional ctx->header_sent checks.

As we now have ctx->header_sent flag, it is further used to prevent
duplicate $r->send_http_header() calls, prevent output before sending
header, and $r->internal_redirect() after sending header.

Further, $r->send_http_header() protected from calls after
$r->internal_redirect().

diffstat:

 src/http/modules/perl/nginx.xs               |  24 ++++++++++++++++++++++++
 src/http/modules/perl/ngx_http_perl_module.c |   1 +
 2 files changed, 25 insertions(+), 0 deletions(-)

diffs (73 lines):

diff --git a/src/http/modules/perl/nginx.xs b/src/http/modules/perl/nginx.xs
--- a/src/http/modules/perl/nginx.xs
+++ b/src/http/modules/perl/nginx.xs
@@ -141,6 +141,14 @@ send_http_header(r, ...)
         croak("send_http_header(): cannot be used in variable handler");
     }
 
+    if (ctx->header_sent) {
+        croak("send_http_header(): header already sent");
+    }
+
+    if (ctx->redirect_uri.len) {
+        croak("send_http_header(): cannot be used with internal_redirect()");
+    }
+
     if (r->headers_out.status == 0) {
         r->headers_out.status = NGX_HTTP_OK;
     }
@@ -666,6 +674,10 @@ print(r, ...)
         croak("print(): cannot be used in variable handler");
     }
 
+    if (!ctx->header_sent) {
+        croak("print(): header not sent");
+    }
+
     if (items == 2) {
 
         /*
@@ -780,6 +792,10 @@ sendfile(r, filename, offset = -1, bytes
         croak("sendfile(): cannot be used in variable handler");
     }
 
+    if (!ctx->header_sent) {
+        croak("sendfile(): header not sent");
+    }
+
     filename = SvPV_nolen(ST(1));
 
     if (filename == NULL) {
@@ -886,6 +902,10 @@ flush(r)
         croak("flush(): cannot be used in variable handler");
     }
 
+    if (!ctx->header_sent) {
+        croak("flush(): header not sent");
+    }
+
     b = ngx_calloc_buf(r->pool);
     if (b == NULL) {
         ctx->error = 1;
@@ -921,6 +941,10 @@ internal_redirect(r, uri)
         croak("internal_redirect(): cannot be used in variable handler");
     }
 
+    if (ctx->header_sent) {
+        croak("internal_redirect(): header already sent");
+    }
+
     uri = ST(1);
 
     if (ngx_http_perl_sv2str(aTHX_ r, &ctx->redirect_uri, uri) != NGX_OK) {
diff --git a/src/http/modules/perl/ngx_http_perl_module.c b/src/http/modules/perl/ngx_http_perl_module.c
--- a/src/http/modules/perl/ngx_http_perl_module.c
+++ b/src/http/modules/perl/ngx_http_perl_module.c
@@ -394,6 +394,7 @@ ngx_http_perl_ssi(ngx_http_request_t *r,
     pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
 
     ctx->ssi = ssi_ctx;
+    ctx->header_sent = 1;
 
     handler = params[NGX_HTTP_PERL_SSI_SUB];
     handler->data[handler->len] = '\0';


More information about the nginx-devel mailing list