Mercurial > hg > nginx
changeset 9263:388a801e9bb9
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.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sat, 27 Apr 2024 18:23:52 +0300 |
parents | 106b3832e7ef |
children | f5423ee155fe |
files | src/http/modules/ngx_http_proxy_module.c src/http/ngx_http_request_body.c src/http/ngx_http_variables.c src/http/v2/ngx_http_v2.c src/http/v3/ngx_http_v3_request.c |
diffstat | 5 files changed, 12 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/src/http/modules/ngx_http_proxy_module.c Sat Apr 27 18:23:22 2024 +0300 +++ b/src/http/modules/ngx_http_proxy_module.c Sat Apr 27 18:23:52 2024 +0300 @@ -1342,7 +1342,7 @@ 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;
--- a/src/http/ngx_http_request_body.c Sat Apr 27 18:23:22 2024 +0300 +++ b/src/http/ngx_http_request_body.c Sat Apr 27 18:23:52 2024 +0300 @@ -46,7 +46,7 @@ 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 @@ 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 @@ 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_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 @@ return NGX_OK; } - r->discard_body = 1; - size = r->header_in->last - r->header_in->pos; if (size || r->headers_in.chunked) {
--- a/src/http/ngx_http_variables.c Sat Apr 27 18:23:22 2024 +0300 +++ b/src/http/ngx_http_variables.c Sat Apr 27 18:23:52 2024 +0300 @@ -1196,11 +1196,7 @@ 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);
--- a/src/http/v2/ngx_http_v2.c Sat Apr 27 18:23:22 2024 +0300 +++ b/src/http/v2/ngx_http_v2.c Sat Apr 27 18:23:52 2024 +0300 @@ -1101,7 +1101,7 @@ 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 @@ "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 @@ 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 @@ 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);
--- a/src/http/v3/ngx_http_v3_request.c Sat Apr 27 18:23:22 2024 +0300 +++ b/src/http/v3/ngx_http_v3_request.c Sat Apr 27 18:23:52 2024 +0300 @@ -1292,7 +1292,7 @@ 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);