[nginx] Request body: discarded body now treated as no body.
Maxim Dounin
mdounin at mdounin.ru
Sat Apr 27 15:56:48 UTC 2024
details: http://freenginx.org/hg/nginx/rev/388a801e9bb9
branches:
changeset: 9263:388a801e9bb9
user: Maxim Dounin <mdounin at mdounin.ru>
date: Sat Apr 27 18:23:52 2024 +0300
description:
Request body: discarded body now treated as no body.
Notably, proxying of such requests now uses no Content-Length instead
of "Content-Length: 0", and the $content_length variable is empty (instead
of "0").
This might be beneficial from correctness point of view, since requests
with discarded body, such as during processing of error pages, do not pretend
there is a zero-length body, but instead do not contain body at all. For
example, this might be important for PUT requests, where a zero-length
body could be incorrectly interpreted as a real request body.
This also slightly simplifies the code.
diffstat:
src/http/modules/ngx_http_proxy_module.c | 2 +-
src/http/ngx_http_request_body.c | 22 +++++-----------------
src/http/ngx_http_variables.c | 6 +-----
src/http/v2/ngx_http_v2.c | 8 ++++----
src/http/v3/ngx_http_v3_request.c | 2 +-
5 files changed, 12 insertions(+), 28 deletions(-)
diffs (148 lines):
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -1342,7 +1342,7 @@ ngx_http_proxy_create_request(ngx_http_r
ctx->internal_chunked = 1;
} else if (r->discard_body) {
- ctx->internal_body_length = 0;
+ ctx->internal_body_length = -1;
} else {
ctx->internal_body_length = r->headers_in.content_length_n;
diff --git a/src/http/ngx_http_request_body.c b/src/http/ngx_http_request_body.c
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -46,7 +46,7 @@ ngx_http_read_client_request_body(ngx_ht
r->request_body_no_buffering = 0;
if (r->request_body && r->request_body->no_buffering) {
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
if (r->reading_body) {
@@ -244,7 +244,7 @@ done:
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
r->lingering_close = 1;
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
r->main->count--;
@@ -319,7 +319,7 @@ ngx_http_read_client_request_body_handle
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
r->lingering_close = 1;
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
r->read_event_handler = ngx_http_block_reading;
@@ -679,27 +679,17 @@ ngx_http_discard_request_body(ngx_http_r
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http set discard body");
+ r->discard_body = 1;
+
#if (NGX_HTTP_V2)
if (r->stream) {
r->stream->skip_data = 1;
-
- if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) {
- r->headers_in.content_length_n = 0;
- r->discard_body = 1;
- }
-
return NGX_OK;
}
#endif
#if (NGX_HTTP_V3)
if (r->http_version == NGX_HTTP_VERSION_30) {
-
- if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) {
- r->headers_in.content_length_n = 0;
- r->discard_body = 1;
- }
-
return NGX_OK;
}
#endif
@@ -718,8 +708,6 @@ ngx_http_discard_request_body(ngx_http_r
return NGX_OK;
}
- r->discard_body = 1;
-
size = r->header_in->last - r->header_in->pos;
if (size || r->headers_in.chunked) {
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
@@ -1196,11 +1196,7 @@ ngx_http_variable_content_length(ngx_htt
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;
+ v->not_found = 1;
} else if (r->headers_in.content_length_n >= 0) {
p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
diff --git a/src/http/v2/ngx_http_v2.c b/src/http/v2/ngx_http_v2.c
--- a/src/http/v2/ngx_http_v2.c
+++ b/src/http/v2/ngx_http_v2.c
@@ -1101,7 +1101,7 @@ ngx_http_v2_state_read_data(ngx_http_v2_
if (rc != NGX_OK && rc != NGX_AGAIN) {
stream->skip_data = 1;
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
ngx_http_finalize_request(r, rc);
@@ -3765,7 +3765,7 @@ ngx_http_v2_run_request(ngx_http_request
"client prematurely closed stream");
r->stream->skip_data = 1;
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
goto failed;
@@ -4199,7 +4199,7 @@ ngx_http_v2_read_client_request_body_han
if (rc != NGX_OK && rc != NGX_AGAIN) {
r->stream->skip_data = 1;
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
ngx_http_finalize_request(r, rc);
@@ -4264,7 +4264,7 @@ ngx_http_v2_read_client_request_body_han
error:
stream->skip_data = 1;
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
diff --git a/src/http/v3/ngx_http_v3_request.c b/src/http/v3/ngx_http_v3_request.c
--- a/src/http/v3/ngx_http_v3_request.c
+++ b/src/http/v3/ngx_http_v3_request.c
@@ -1292,7 +1292,7 @@ ngx_http_v3_read_client_request_body_han
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
- r->headers_in.content_length_n = 0;
+ r->discard_body = 1;
r->request_body->bufs = NULL;
ngx_http_finalize_request(r, rc);
More information about the nginx-devel
mailing list