changeset 9262:106b3832e7ef

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.
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 27 Apr 2024 18:23:22 +0300
parents f798ecafec05
children 388a801e9bb9
files src/http/ngx_http_variables.c
diffstat 1 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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) {