# HG changeset patch # User Maxim Dounin # Date 1714231402 -10800 # Node ID 106b3832e7efe262785c837db8e5c3dbfd2a659a # Parent f798ecafec05bda00fd69b3172a6b3f06cc1b1dd 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) {