comparison src/http/ngx_http_variables.c @ 8873:ef6a3a99a81a

Reworked multi headers to use linked lists. Multi headers are now using linked lists instead of arrays. Notably, the following fields were changed: r->headers_in.cookies (renamed to r->headers_in.cookie), r->headers_in.x_forwarded_for, r->headers_out.cache_control, r->headers_out.link, u->headers_in.cache_control u->headers_in.cookies (renamed to u->headers_in.set_cookie). The r->headers_in.cookies and u->headers_in.cookies fields were renamed to r->headers_in.cookie and u->headers_in.set_cookie to match header names. The ngx_http_parse_multi_header_lines() and ngx_http_parse_set_cookie_lines() functions were changed accordingly. With this change, multi headers are now essentially equivalent to normal headers, and following changes will further make them equivalent.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 30 May 2022 21:25:33 +0300
parents 08b3ea81ff5f
children c263f9ffa1fd
comparison
equal deleted inserted replaced
8872:08b3ea81ff5f 8873:ef6a3a99a81a
181 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_headers, 181 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_headers,
182 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 }, 182 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
183 #endif 183 #endif
184 184
185 { ngx_string("http_cookie"), NULL, ngx_http_variable_cookies, 185 { ngx_string("http_cookie"), NULL, ngx_http_variable_cookies,
186 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 }, 186 offsetof(ngx_http_request_t, headers_in.cookie), 0, 0 },
187 187
188 { ngx_string("content_length"), NULL, ngx_http_variable_content_length, 188 { ngx_string("content_length"), NULL, ngx_http_variable_content_length,
189 0, 0, 0 }, 189 0, 0, 0 },
190 190
191 { ngx_string("content_type"), NULL, ngx_http_variable_header, 191 { ngx_string("content_type"), NULL, ngx_http_variable_header,
844 844
845 static ngx_int_t 845 static ngx_int_t
846 ngx_http_variable_headers_internal(ngx_http_request_t *r, 846 ngx_http_variable_headers_internal(ngx_http_request_t *r,
847 ngx_http_variable_value_t *v, uintptr_t data, u_char sep) 847 ngx_http_variable_value_t *v, uintptr_t data, u_char sep)
848 { 848 {
849 size_t len; 849 size_t len;
850 u_char *p, *end; 850 u_char *p;
851 ngx_uint_t i, n; 851 ngx_table_elt_t *h, *th;
852 ngx_array_t *a; 852
853 ngx_table_elt_t **h; 853 h = *(ngx_table_elt_t **) ((char *) r + data);
854
855 a = (ngx_array_t *) ((char *) r + data);
856
857 n = a->nelts;
858 h = a->elts;
859 854
860 len = 0; 855 len = 0;
861 856
862 for (i = 0; i < n; i++) { 857 for (th = h; th; th = th->next) {
863 858
864 if (h[i]->hash == 0) { 859 if (th->hash == 0) {
865 continue; 860 continue;
866 } 861 }
867 862
868 len += h[i]->value.len + 2; 863 len += th->value.len + 2;
869 } 864 }
870 865
871 if (len == 0) { 866 if (len == 0) {
872 v->not_found = 1; 867 v->not_found = 1;
873 return NGX_OK; 868 return NGX_OK;
877 872
878 v->valid = 1; 873 v->valid = 1;
879 v->no_cacheable = 0; 874 v->no_cacheable = 0;
880 v->not_found = 0; 875 v->not_found = 0;
881 876
882 if (n == 1) { 877 if (h->next == NULL) {
883 v->len = (*h)->value.len; 878 v->len = h->value.len;
884 v->data = (*h)->value.data; 879 v->data = h->value.data;
885 880
886 return NGX_OK; 881 return NGX_OK;
887 } 882 }
888 883
889 p = ngx_pnalloc(r->pool, len); 884 p = ngx_pnalloc(r->pool, len);
892 } 887 }
893 888
894 v->len = len; 889 v->len = len;
895 v->data = p; 890 v->data = p;
896 891
897 end = p + len; 892 for (th = h; th; th = th->next) {
898 893
899 for (i = 0; /* void */ ; i++) { 894 if (th->hash == 0) {
900
901 if (h[i]->hash == 0) {
902 continue; 895 continue;
903 } 896 }
904 897
905 p = ngx_copy(p, h[i]->value.data, h[i]->value.len); 898 p = ngx_copy(p, th->value.data, th->value.len);
906 899
907 if (p == end) { 900 if (th->next == NULL) {
908 break; 901 break;
909 } 902 }
910 903
911 *p++ = sep; *p++ = ' '; 904 *p++ = sep; *p++ = ' ';
912 } 905 }
1100 ngx_str_t cookie, s; 1093 ngx_str_t cookie, s;
1101 1094
1102 s.len = name->len - (sizeof("cookie_") - 1); 1095 s.len = name->len - (sizeof("cookie_") - 1);
1103 s.data = name->data + sizeof("cookie_") - 1; 1096 s.data = name->data + sizeof("cookie_") - 1;
1104 1097
1105 if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &s, &cookie) 1098 if (ngx_http_parse_multi_header_lines(r, r->headers_in.cookie, &s, &cookie)
1106 == NGX_DECLINED) 1099 == NULL)
1107 { 1100 {
1108 v->not_found = 1; 1101 v->not_found = 1;
1109 return NGX_OK; 1102 return NGX_OK;
1110 } 1103 }
1111 1104