From d751192d482440f2bf90b68a0430b29f365e1ea0 Mon Sep 17 00:00:00 2001 From: oxpa Date: Sat, 22 Apr 2023 17:54:16 +0100 Subject: [PATCH] Set headers in the tail of a list rather than in the head It seems that ngx_http_variable_headers_internal function in nginx/src/http/ngx_http_variables.c relies on the fact that the last header values is stored in the last element of a list. Therefore we should avoid changing a random element in the head of a list of headers and either change the last element or add a new element to the list. --- src/ngx_http_lua_headers_out.c | 62 +++++++++++----------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/src/ngx_http_lua_headers_out.c b/src/ngx_http_lua_headers_out.c index c51146a3fc..1424b379eb 100644 --- a/src/ngx_http_lua_headers_out.c +++ b/src/ngx_http_lua_headers_out.c @@ -319,62 +319,40 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, headers = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset); - if (hv->no_override) { - for (h = *headers; h; h = h->next) { - if (!h->hash) { - h->value = *value; - h->hash = hv->hash; - return NGX_OK; - } - } - - goto create; - } - - /* override old values (if any) */ - - if (*headers) { - for (h = (*headers)->next; h; h = h->next) { + for (h = *headers; h; h = h->next) { + if (!hv->no_override) { h->hash = 0; h->value.len = 0; } + } - h = *headers; - + if (h->hash == 0) { + // update the last element if possible h->value = *value; - - if (value->len == 0) { - h->hash = 0; - - } else { + if (value->len != 0) { h->hash = hv->hash; } - return NGX_OK; - } - -create: - - for (ph = headers; *ph; ph = &(*ph)->next) { /* void */ } + } else { + ho = ngx_list_push(&r->headers_out.headers); + if (ho == NULL) { + return NGX_ERROR; + } - ho = ngx_list_push(&r->headers_out.headers); - if (ho == NULL) { - return NGX_ERROR; - } + ho->value = *value; - ho->value = *value; + if (value->len == 0) { + ho->hash = 0; - if (value->len == 0) { - ho->hash = 0; + } else { + ho->hash = hv->hash; + } - } else { - ho->hash = hv->hash; + ho->key = hv->key; + ho->next = NULL; + h->next = ho; } - ho->key = hv->key; - ho->next = NULL; - *ph = ho; - return NGX_OK; #else ngx_array_t *pa;