Welcome! Log In Create A New Profile

Advanced

[nginx] Combining unknown headers during variables lookup (ticket #1316).

Sergey Kandaurov
May 30, 2022 06:34PM
details: https://hg.nginx.org/nginx/rev/08b3ea81ff5f
branches:
changeset: 8023:08b3ea81ff5f
user: Maxim Dounin <mdounin@mdounin.ru>
date: Mon May 30 21:25:32 2022 +0300
description:
Combining unknown headers during variables lookup (ticket #1316).

Previously, $http_*, $sent_http_*, $sent_trailer_*, $upstream_http_*,
and $upstream_trailer_* variables returned only the first header (with
a few specially handled exceptions: $http_cookie, $http_x_forwarded_for,
$sent_http_cache_control, $sent_http_link).

With this change, all headers are returned, combined together. For
example, $http_foo variable will be "a, b" if there are "Foo: a" and
"Foo: b" headers in the request.

Note that $upstream_http_set_cookie will also return all "Set-Cookie"
headers (ticket #1843), though this might not be what one want, since
the "Set-Cookie" header does not follow the list syntax (see RFC 7230,
section 3.2.2).

diffstat:

src/http/ngx_http_upstream.c | 4 +-
src/http/ngx_http_variables.c | 86 ++++++++++++++++++++++++++++++++++--------
src/http/ngx_http_variables.h | 5 +-
3 files changed, 74 insertions(+), 21 deletions(-)

diffs (179 lines):

diff -r 8b7a96fdd54c -r 08b3ea81ff5f src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c Mon May 30 21:25:30 2022 +0300
+++ b/src/http/ngx_http_upstream.c Mon May 30 21:25:32 2022 +0300
@@ -5703,7 +5703,7 @@ ngx_http_upstream_header_variable(ngx_ht
return NGX_OK;
}

- return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
+ return ngx_http_variable_unknown_header(r, v, (ngx_str_t *) data,
&r->upstream->headers_in.headers.part,
sizeof("upstream_http_") - 1);
}
@@ -5718,7 +5718,7 @@ ngx_http_upstream_trailer_variable(ngx_h
return NGX_OK;
}

- return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
+ return ngx_http_variable_unknown_header(r, v, (ngx_str_t *) data,
&r->upstream->headers_in.trailers.part,
sizeof("upstream_trailer_") - 1);
}
diff -r 8b7a96fdd54c -r 08b3ea81ff5f src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c Mon May 30 21:25:30 2022 +0300
+++ b/src/http/ngx_http_variables.c Mon May 30 21:25:32 2022 +0300
@@ -919,7 +919,7 @@ static ngx_int_t
ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
- return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
+ return ngx_http_variable_unknown_header(r, v, (ngx_str_t *) data,
&r->headers_in.headers.part,
sizeof("http_") - 1);
}
@@ -929,7 +929,7 @@ static ngx_int_t
ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
- return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
+ return ngx_http_variable_unknown_header(r, v, (ngx_str_t *) data,
&r->headers_out.headers.part,
sizeof("sent_http_") - 1);
}
@@ -939,19 +939,26 @@ static ngx_int_t
ngx_http_variable_unknown_trailer_out(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
- return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
+ return ngx_http_variable_unknown_header(r, v, (ngx_str_t *) data,
&r->headers_out.trailers.part,
sizeof("sent_trailer_") - 1);
}


ngx_int_t
-ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var,
+ngx_http_variable_unknown_header(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, ngx_str_t *var,
ngx_list_part_t *part, size_t prefix)
{
- u_char ch;
+ u_char *p, ch;
+ size_t len;
ngx_uint_t i, n;
- ngx_table_elt_t *header;
+ ngx_table_elt_t *header, *h, **ph;
+
+ ph = &h;
+#if (NGX_SUPPRESS_WARN)
+ len = 0;
+#endif

header = part->elts;

@@ -971,7 +978,11 @@ ngx_http_variable_unknown_header(ngx_htt
continue;
}

- for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) {
+ if (header[i].key.len != var->len - prefix) {
+ continue;
+ }
+
+ for (n = 0; n < var->len - prefix; n++) {
ch = header[i].key.data[n];

if (ch >= 'A' && ch <= 'Z') {
@@ -986,18 +997,59 @@ ngx_http_variable_unknown_header(ngx_htt
}
}

- if (n + prefix == var->len && n == header[i].key.len) {
- v->len = header[i].value.len;
- v->valid = 1;
- v->no_cacheable = 0;
- v->not_found = 0;
- v->data = header[i].value.data;
-
- return NGX_OK;
+ if (n != var->len - prefix) {
+ continue;
}
+
+ len += header[i].value.len + 2;
+
+ *ph = &header[i];
+ ph = &header[i].next;
}

- v->not_found = 1;
+ *ph = NULL;
+
+ if (h == NULL) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ len -= 2;
+
+ if (h->next == NULL) {
+
+ v->len = h->value.len;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = h->value.data;
+
+ return NGX_OK;
+ }
+
+ p = ngx_pnalloc(r->pool, len);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ v->len = len;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ for ( ;; ) {
+
+ p = ngx_copy(p, h->value.data, h->value.len);
+
+ if (h->next == NULL) {
+ break;
+ }
+
+ *p++ = ','; *p++ = ' ';
+
+ h = h->next;
+ }

return NGX_OK;
}
@@ -1879,7 +1931,7 @@ ngx_http_variable_sent_location(ngx_http

ngx_str_set(&name, "sent_http_location");

- return ngx_http_variable_unknown_header(v, &name,
+ return ngx_http_variable_unknown_header(r, v, &name,
&r->headers_out.headers.part,
sizeof("sent_http_") - 1);
}
diff -r 8b7a96fdd54c -r 08b3ea81ff5f src/http/ngx_http_variables.h
--- a/src/http/ngx_http_variables.h Mon May 30 21:25:30 2022 +0300
+++ b/src/http/ngx_http_variables.h Mon May 30 21:25:32 2022 +0300
@@ -57,8 +57,9 @@ ngx_http_variable_value_t *ngx_http_get_
ngx_http_variable_value_t *ngx_http_get_variable(ngx_http_request_t *r,
ngx_str_t *name, ngx_uint_t key);

-ngx_int_t ngx_http_variable_unknown_header(ngx_http_variable_value_t *v,
- ngx_str_t *var, ngx_list_part_t *part, size_t prefix);
+ngx_int_t ngx_http_variable_unknown_header(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, ngx_str_t *var, ngx_list_part_t *part,
+ size_t prefix);


#if (NGX_PCRE)
_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-leave@nginx.org
Subject Author Views Posted

[nginx] Combining unknown headers during variables lookup (ticket #1316).

Sergey Kandaurov 122 May 30, 2022 06:34PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 56
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready