[nginx] Perl: avoid returning 500 if header was already sent.

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


details:   https://hg.nginx.org/nginx/rev/fd9252844ec1
branches:  
changeset: 7530:fd9252844ec1
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Fri Jul 12 15:39:25 2019 +0300
description:
Perl: avoid returning 500 if header was already sent.

Returning NGX_HTTP_INTERNAL_SERVER_ERROR if a perl code died after
sending header will lead to a "header already sent" alert.  To avoid
it, we now check if header was already sent, and return NGX_ERROR
instead if it was.

diffstat:

 src/http/modules/perl/nginx.xs               |  2 ++
 src/http/modules/perl/ngx_http_perl_module.c |  4 ++++
 src/http/modules/perl/ngx_http_perl_module.h |  1 +
 3 files changed, 7 insertions(+), 0 deletions(-)

diffs (37 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
@@ -164,6 +164,8 @@ send_http_header(r, ...)
         }
     }
 
+    ctx->header_sent = 1;
+
     r->disable_not_modified = 1;
 
     rc = ngx_http_send_header(r);
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
@@ -780,6 +780,10 @@ ngx_http_perl_call_handler(pTHX_ ngx_htt
 
         ctx->redirect_uri.len = 0;
 
+        if (ctx->header_sent) {
+            return NGX_ERROR;
+        }
+
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
diff --git a/src/http/modules/perl/ngx_http_perl_module.h b/src/http/modules/perl/ngx_http_perl_module.h
--- a/src/http/modules/perl/ngx_http_perl_module.h
+++ b/src/http/modules/perl/ngx_http_perl_module.h
@@ -34,6 +34,7 @@ typedef struct {
     unsigned                  done:1;
     unsigned                  error:1;
     unsigned                  variable:1;
+    unsigned                  header_sent:1;
 
     ngx_array_t              *variables;  /* array of ngx_http_perl_var_t */
 


More information about the nginx-devel mailing list