HTTP: force chunked encoding only when emitting trailers. Please note that this changes API and modules that produce trailers must set r->expect_trailers, otherwise trailers won't be emitted. Requested by Maxim Dounin. Change-Id: Ic85917fd84e093c02473fe16a1d629d5f22316e1 Signed-off-by: Piotr Sikora <piotrsikora@google.com> Reviewed-on: https://nginx-review.googlesource.com/2802 Reviewed-by: Lizan Zhou <zlizan@google.com>
diff --git a/src/http/modules/ngx_http_chunked_filter_module.c b/src/http/modules/ngx_http_chunked_filter_module.c index 41ca4cb..ffa1dd2 100644 --- a/src/http/modules/ngx_http_chunked_filter_module.c +++ b/src/http/modules/ngx_http_chunked_filter_module.c
@@ -72,7 +72,9 @@ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); - if (clcf->chunked_transfer_encoding && r->trailers_ok) { + if (clcf->chunked_transfer_encoding + && r->allow_trailers && r->expect_trailers) + { ngx_http_clear_content_length(r); r->chunked = 1; @@ -205,7 +207,7 @@ b->pos += 2; } - if (r->trailers_ok) { + if (r->allow_trailers && r->expect_trailers) { tl->next = ngx_http_chunked_get_trailers(r); if (tl->next != NULL) {
diff --git a/src/http/modules/ngx_http_headers_filter_module.c b/src/http/modules/ngx_http_headers_filter_module.c index df5d8bd..baeb44c 100644 --- a/src/http/modules/ngx_http_headers_filter_module.c +++ b/src/http/modules/ngx_http_headers_filter_module.c
@@ -224,7 +224,7 @@ } } - if (conf->trailers && r->trailers_ok) { + if (conf->trailers && r->allow_trailers) { len = 0; h = conf->trailers->elts; @@ -280,6 +280,8 @@ t->value.len = len; t->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash( ngx_hash('t', 'r'), 'a'), 'i'), 'l'), 'e'), 'r'); + + r->expect_trailers = 1; } return ngx_http_next_header_filter(r);
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index 4cbfb84..d1012ad 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c
@@ -1774,10 +1774,12 @@ if (h->value.len == sizeof("trailers") - 1 && ngx_memcmp(h->value.data, "trailers", sizeof("trailers") - 1) == 0) { - r->trailers_ok = 1; + r->allow_trailers = 1; return NGX_OK; } +#if (NGX_HTTP_V2) + if (r->http_version >= NGX_HTTP_VERSION_20) { ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "client sent HTTP/2 request with invalid value \"%V\" " @@ -1787,6 +1789,8 @@ return NGX_ERROR; } +#endif + if (h->value.len < sizeof("trailers") - 1) { return NGX_OK; } @@ -1801,7 +1805,7 @@ p += sizeof("trailers") - 1; if (p == h->value.data + h->value.len || *p == ',' || *p == ' ') { - r->trailers_ok = 1; + r->allow_trailers = 1; return NGX_OK; } }
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h index 150ecdd..7dbed9d 100644 --- a/src/http/ngx_http_request.h +++ b/src/http/ngx_http_request.h
@@ -513,7 +513,8 @@ unsigned pipeline:1; unsigned chunked:1; unsigned header_only:1; - unsigned trailers_ok:1; + unsigned allow_trailers:1; + unsigned expect_trailers:1; unsigned keepalive:1; unsigned lingering_close:1; unsigned discard_body:1;
diff --git a/src/http/v2/ngx_http_v2_filter_module.c b/src/http/v2/ngx_http_v2_filter_module.c index adf6acd..2432918 100644 --- a/src/http/v2/ngx_http_v2_filter_module.c +++ b/src/http/v2/ngx_http_v2_filter_module.c
@@ -1064,7 +1064,7 @@ size -= rest; } - if (cl->buf->last_buf && r->trailers_ok) { + if (cl->buf->last_buf && r->allow_trailers && r->expect_trailers) { trailers = ngx_http_v2_create_trailers_frame(r); if (trailers) { cl->buf->last_buf = 0;