[nginx] Modified $content_length to match available request body...

Maxim Dounin mdounin at mdounin.ru
Sat Apr 27 15:56:48 UTC 2024


details:   http://freenginx.org/hg/nginx/rev/106b3832e7ef
branches:  
changeset: 9262:106b3832e7ef
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Sat Apr 27 18:23:22 2024 +0300
description:
Modified $content_length to match available request body length.

As long as the request body was discarded or there was an error during
reading, it now follows r->headers_in.content_length_n and reflects the
fact that no request body is available, similarly to how Content-Length
as used by the proxy module does.

This makes complex processing of various error pages safer, notably when
using fastcgi_pass, uwsgi_pass, or grpc_pass, where the $content_length
variable is used to set length.

diffstat:

 src/http/ngx_http_variables.c |  13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)

diffs (39 lines):

diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -1184,17 +1184,24 @@ ngx_http_variable_content_length(ngx_htt
 {
     u_char  *p;
 
-    if (r->headers_in.content_length) {
+    if (r->reading_body && r->headers_in.content_length) {
         v->len = r->headers_in.content_length->value.len;
         v->data = r->headers_in.content_length->value.data;
         v->valid = 1;
-        v->no_cacheable = 0;
+        v->no_cacheable = 1;
         v->not_found = 0;
 
     } else if (r->reading_body) {
         v->not_found = 1;
         v->no_cacheable = 1;
 
+    } else if (r->discard_body) {
+        v->len = 1;
+        v->data = (u_char *) "0";
+        v->valid = 1;
+        v->no_cacheable = 0;
+        v->not_found = 0;
+
     } else if (r->headers_in.content_length_n >= 0) {
         p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
         if (p == NULL) {
@@ -1204,7 +1211,7 @@ ngx_http_variable_content_length(ngx_htt
         v->len = ngx_sprintf(p, "%O", r->headers_in.content_length_n) - p;
         v->data = p;
         v->valid = 1;
-        v->no_cacheable = 0;
+        v->no_cacheable = 1;
         v->not_found = 0;
 
     } else if (r->headers_in.chunked) {



More information about the nginx-devel mailing list