[PATCH 13 of 14] Modified $content_length to match available request body length

Maxim Dounin mdounin at mdounin.ru
Sat Apr 20 01:00:01 UTC 2024


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1713574647 -10800
#      Sat Apr 20 03:57:27 2024 +0300
# Node ID 4a81de7ba1d061f90960a5cc44d3a08e8c45fc1e
# Parent  b842b7f11593169f0b44cc1b6aa74e631c17c292
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.

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