[nginx] Variables: generic prefix variables.
Dmitry Volyntsev
xeioex at nginx.com
Tue Jan 31 18:22:23 UTC 2017
details: http://hg.nginx.org/nginx/rev/d2b2ff157da5
branches:
changeset: 6899:d2b2ff157da5
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Jan 31 21:19:58 2017 +0300
description:
Variables: generic prefix variables.
diffstat:
src/http/modules/ngx_http_rewrite_module.c | 13 +-
src/http/ngx_http_core_module.h | 3 +-
src/http/ngx_http_upstream.c | 14 +-
src/http/ngx_http_upstream.h | 5 -
src/http/ngx_http_variables.c | 250 ++++++++++++++--------------
src/http/ngx_http_variables.h | 2 +
src/stream/ngx_stream.h | 3 +-
src/stream/ngx_stream_variables.c | 146 ++++++++++++++--
src/stream/ngx_stream_variables.h | 2 +
9 files changed, 271 insertions(+), 167 deletions(-)
diffs (747 lines):
diff -r 25203fc377fb -r d2b2ff157da5 src/http/modules/ngx_http_rewrite_module.c
--- a/src/http/modules/ngx_http_rewrite_module.c Tue Jan 31 12:09:40 2017 +0300
+++ b/src/http/modules/ngx_http_rewrite_module.c Tue Jan 31 21:19:58 2017 +0300
@@ -917,7 +917,8 @@ ngx_http_rewrite_set(ngx_conf_t *cf, ngx
value[1].len--;
value[1].data++;
- v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
+ v = ngx_http_add_variable(cf, &value[1],
+ NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_WEAK);
if (v == NULL) {
return NGX_CONF_ERROR;
}
@@ -927,15 +928,7 @@ ngx_http_rewrite_set(ngx_conf_t *cf, ngx
return NGX_CONF_ERROR;
}
- if (v->get_handler == NULL
- && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0
- && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0
- && ngx_strncasecmp(value[1].data, (u_char *) "upstream_http_", 14) != 0
- && ngx_strncasecmp(value[1].data, (u_char *) "cookie_", 7) != 0
- && ngx_strncasecmp(value[1].data, (u_char *) "upstream_cookie_", 16)
- != 0
- && ngx_strncasecmp(value[1].data, (u_char *) "arg_", 4) != 0)
- {
+ if (v->get_handler == NULL) {
v->get_handler = ngx_http_rewrite_var;
v->data = index;
}
diff -r 25203fc377fb -r d2b2ff157da5 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h Tue Jan 31 12:09:40 2017 +0300
+++ b/src/http/ngx_http_core_module.h Tue Jan 31 21:19:58 2017 +0300
@@ -158,7 +158,8 @@ typedef struct {
ngx_hash_t variables_hash;
- ngx_array_t variables; /* ngx_http_variable_t */
+ ngx_array_t variables; /* ngx_http_variable_t */
+ ngx_array_t prefix_variables; /* ngx_http_variable_t */
ngx_uint_t ncaptures;
ngx_uint_t server_names_hash_max_size;
diff -r 25203fc377fb -r d2b2ff157da5 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c Tue Jan 31 12:09:40 2017 +0300
+++ b/src/http/ngx_http_upstream.c Tue Jan 31 21:19:58 2017 +0300
@@ -162,6 +162,10 @@ static ngx_int_t ngx_http_upstream_respo
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_upstream_response_length_variable(
ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_upstream_cookie_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -413,6 +417,12 @@ static ngx_http_variable_t ngx_http_ups
#endif
+ { ngx_string("upstream_http_"), NULL, ngx_http_upstream_header_variable,
+ 0, NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_PREFIX, 0 },
+
+ { ngx_string("upstream_cookie_"), NULL, ngx_http_upstream_cookie_variable,
+ 0, NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_PREFIX, 0 },
+
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
@@ -5391,7 +5401,7 @@ ngx_http_upstream_response_length_variab
}
-ngx_int_t
+static ngx_int_t
ngx_http_upstream_header_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
@@ -5406,7 +5416,7 @@ ngx_http_upstream_header_variable(ngx_ht
}
-ngx_int_t
+static ngx_int_t
ngx_http_upstream_cookie_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
diff -r 25203fc377fb -r d2b2ff157da5 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h Tue Jan 31 12:09:40 2017 +0300
+++ b/src/http/ngx_http_upstream.h Tue Jan 31 21:19:58 2017 +0300
@@ -402,11 +402,6 @@ typedef struct {
} ngx_http_upstream_param_t;
-ngx_int_t ngx_http_upstream_cookie_variable(ngx_http_request_t *r,
- ngx_http_variable_value_t *v, uintptr_t data);
-ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
- ngx_http_variable_value_t *v, uintptr_t data);
-
ngx_int_t ngx_http_upstream_create(ngx_http_request_t *r);
void ngx_http_upstream_init(ngx_http_request_t *r);
ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
diff -r 25203fc377fb -r d2b2ff157da5 src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c Tue Jan 31 12:09:40 2017 +0300
+++ b/src/http/ngx_http_variables.c Tue Jan 31 21:19:58 2017 +0300
@@ -11,6 +11,9 @@
#include <nginx.h>
+static ngx_http_variable_t *ngx_http_add_prefix_variable(ngx_conf_t *cf,
+ ngx_str_t *name, ngx_uint_t flags);
+
static ngx_int_t ngx_http_variable_request(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
#if 0
@@ -356,6 +359,18 @@ static ngx_http_variable_t ngx_http_cor
3, NGX_HTTP_VAR_NOCACHEABLE, 0 },
#endif
+ { ngx_string("http_"), NULL, ngx_http_variable_unknown_header_in,
+ 0, NGX_HTTP_VAR_PREFIX, 0 },
+
+ { ngx_string("sent_http_"), NULL, ngx_http_variable_unknown_header_out,
+ 0, NGX_HTTP_VAR_PREFIX, 0 },
+
+ { ngx_string("cookie_"), NULL, ngx_http_variable_cookie,
+ 0, NGX_HTTP_VAR_PREFIX, 0 },
+
+ { ngx_string("arg_"), NULL, ngx_http_variable_argument,
+ 0, NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_PREFIX, 0 },
+
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
@@ -384,6 +399,10 @@ ngx_http_add_variable(ngx_conf_t *cf, ng
return NULL;
}
+ if (flags & NGX_HTTP_VAR_PREFIX) {
+ return ngx_http_add_prefix_variable(cf, name, flags);
+ }
+
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
key = cmcf->variables_keys->keys.elts;
@@ -402,6 +421,8 @@ ngx_http_add_variable(ngx_conf_t *cf, ng
return NULL;
}
+ v->flags &= flags | ~NGX_HTTP_VAR_WEAK;
+
return v;
}
@@ -440,6 +461,59 @@ ngx_http_add_variable(ngx_conf_t *cf, ng
}
+static ngx_http_variable_t *
+ngx_http_add_prefix_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
+{
+ ngx_uint_t i;
+ ngx_http_variable_t *v;
+ ngx_http_core_main_conf_t *cmcf;
+
+ cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
+
+ v = cmcf->prefix_variables.elts;
+ for (i = 0; i < cmcf->prefix_variables.nelts; i++) {
+ if (name->len != v[i].name.len
+ || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
+ {
+ continue;
+ }
+
+ v = &v[i];
+
+ if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "the duplicate \"%V\" variable", name);
+ return NULL;
+ }
+
+ v->flags &= flags | ~NGX_HTTP_VAR_WEAK;
+
+ return v;
+ }
+
+ v = ngx_array_push(&cmcf->prefix_variables);
+ if (v == NULL) {
+ return NULL;
+ }
+
+ v->name.len = name->len;
+ v->name.data = ngx_pnalloc(cf->pool, name->len);
+ if (v->name.data == NULL) {
+ return NULL;
+ }
+
+ ngx_strlow(v->name.data, name->data, name->len);
+
+ v->set_handler = NULL;
+ v->get_handler = NULL;
+ v->data = 0;
+ v->flags = flags;
+ v->index = 0;
+
+ return v;
+}
+
+
ngx_int_t
ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)
{
@@ -573,6 +647,8 @@ ngx_http_get_flushed_variable(ngx_http_r
ngx_http_variable_value_t *
ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key)
{
+ size_t len;
+ ngx_uint_t i, n;
ngx_http_variable_t *v;
ngx_http_variable_value_t *vv;
ngx_http_core_main_conf_t *cmcf;
@@ -610,64 +686,22 @@ ngx_http_get_variable(ngx_http_request_t
return NULL;
}
- if (name->len >= 5 && ngx_strncmp(name->data, "http_", 5) == 0) {
-
- if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name)
- == NGX_OK)
+ len = 0;
+
+ v = cmcf->prefix_variables.elts;
+ n = cmcf->prefix_variables.nelts;
+
+ for (i = 0; i < cmcf->prefix_variables.nelts; i++) {
+ if (name->len >= v[i].name.len && name->len > len
+ && ngx_strncmp(name->data, v[i].name.data, v[i].name.len) == 0)
{
- return vv;
- }
-
- return NULL;
- }
-
- if (name->len >= 10 && ngx_strncmp(name->data, "sent_http_", 10) == 0) {
-
- if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name)
- == NGX_OK)
- {
- return vv;
- }
-
- return NULL;
- }
-
- if (name->len >= 14 && ngx_strncmp(name->data, "upstream_http_", 14) == 0) {
-
- if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name)
- == NGX_OK)
- {
- return vv;
+ len = v[i].name.len;
+ n = i;
}
-
- return NULL;
- }
-
- if (name->len >= 7 && ngx_strncmp(name->data, "cookie_", 7) == 0) {
-
- if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) {
- return vv;
- }
-
- return NULL;
}
- if (name->len >= 16
- && ngx_strncmp(name->data, "upstream_cookie_", 16) == 0)
- {
-
- if (ngx_http_upstream_cookie_variable(r, vv, (uintptr_t) name)
- == NGX_OK)
- {
- return vv;
- }
-
- return NULL;
- }
-
- if (name->len >= 4 && ngx_strncmp(name->data, "arg_", 4) == 0) {
-
- if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) {
+ if (n != cmcf->prefix_variables.nelts) {
+ if (v[n].get_handler(r, vv, (uintptr_t) name) == NGX_OK) {
return vv;
}
@@ -2502,7 +2536,6 @@ ngx_http_regex_exec(ngx_http_request_t *
ngx_int_t
ngx_http_variables_add_core_vars(ngx_conf_t *cf)
{
- ngx_int_t rc;
ngx_http_variable_t *cv, *v;
ngx_http_core_main_conf_t *cmcf;
@@ -2523,27 +2556,20 @@ ngx_http_variables_add_core_vars(ngx_con
return NGX_ERROR;
}
+ if (ngx_array_init(&cmcf->prefix_variables, cf->pool, 8,
+ sizeof(ngx_http_variable_t))
+ != NGX_OK)
+ {
+ return NGX_ERROR;
+ }
+
for (cv = ngx_http_core_variables; cv->name.len; cv++) {
- v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));
+ v = ngx_http_add_variable(cf, &cv->name, cv->flags);
if (v == NULL) {
return NGX_ERROR;
}
*v = *cv;
-
- rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,
- NGX_HASH_READONLY_KEY);
-
- if (rc == NGX_OK) {
- continue;
- }
-
- if (rc == NGX_BUSY) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "conflicting variable name \"%V\"", &v->name);
- }
-
- return NGX_ERROR;
}
return NGX_OK;
@@ -2553,10 +2579,11 @@ ngx_http_variables_add_core_vars(ngx_con
ngx_int_t
ngx_http_variables_init_vars(ngx_conf_t *cf)
{
+ size_t len;
ngx_uint_t i, n;
ngx_hash_key_t *key;
ngx_hash_init_t hash;
- ngx_http_variable_t *v, *av;
+ ngx_http_variable_t *v, *av, *pv;
ngx_http_core_main_conf_t *cmcf;
/* set the handlers for the indexed http variables */
@@ -2564,6 +2591,7 @@ ngx_http_variables_init_vars(ngx_conf_t
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
v = cmcf->variables.elts;
+ pv = cmcf->prefix_variables.elts;
key = cmcf->variables_keys->keys.elts;
for (i = 0; i < cmcf->variables.nelts; i++) {
@@ -2584,7 +2612,9 @@ ngx_http_variables_init_vars(ngx_conf_t
av->index = i;
- if (av->get_handler == NULL) {
+ if (av->get_handler == NULL
+ || (av->flags & NGX_HTTP_VAR_WEAK))
+ {
break;
}
@@ -2592,68 +2622,34 @@ ngx_http_variables_init_vars(ngx_conf_t
}
}
- if (v[i].name.len >= 5
- && ngx_strncmp(v[i].name.data, "http_", 5) == 0)
- {
- v[i].get_handler = ngx_http_variable_unknown_header_in;
- v[i].data = (uintptr_t) &v[i].name;
-
- continue;
- }
-
- if (v[i].name.len >= 10
- && ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0)
- {
- v[i].get_handler = ngx_http_variable_unknown_header_out;
- v[i].data = (uintptr_t) &v[i].name;
-
- continue;
- }
-
- if (v[i].name.len >= 14
- && ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0)
- {
- v[i].get_handler = ngx_http_upstream_header_variable;
- v[i].data = (uintptr_t) &v[i].name;
- v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;
-
- continue;
+ len = 0;
+ av = NULL;
+
+ for (n = 0; n < cmcf->prefix_variables.nelts; n++) {
+ if (v[i].name.len >= pv[n].name.len && v[i].name.len > len
+ && ngx_strncmp(v[i].name.data, pv[n].name.data, pv[n].name.len)
+ == 0)
+ {
+ av = &pv[n];
+ len = pv[n].name.len;
+ }
}
- if (v[i].name.len >= 7
- && ngx_strncmp(v[i].name.data, "cookie_", 7) == 0)
- {
- v[i].get_handler = ngx_http_variable_cookie;
- v[i].data = (uintptr_t) &v[i].name;
-
- continue;
- }
-
- if (v[i].name.len >= 16
- && ngx_strncmp(v[i].name.data, "upstream_cookie_", 16) == 0)
- {
- v[i].get_handler = ngx_http_upstream_cookie_variable;
+ if (av) {
+ v[i].get_handler = av->get_handler;
v[i].data = (uintptr_t) &v[i].name;
- v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;
-
- continue;
+ v[i].flags = av->flags;
+
+ goto next;
}
- if (v[i].name.len >= 4
- && ngx_strncmp(v[i].name.data, "arg_", 4) == 0)
- {
- v[i].get_handler = ngx_http_variable_argument;
- v[i].data = (uintptr_t) &v[i].name;
- v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;
-
- continue;
+ if (v[i].get_handler == NULL) {
+ ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+ "unknown \"%V\" variable", &v[i].name);
+
+ return NGX_ERROR;
}
- ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
- "unknown \"%V\" variable", &v[i].name);
-
- return NGX_ERROR;
-
next:
continue;
}
diff -r 25203fc377fb -r d2b2ff157da5 src/http/ngx_http_variables.h
--- a/src/http/ngx_http_variables.h Tue Jan 31 12:09:40 2017 +0300
+++ b/src/http/ngx_http_variables.h Tue Jan 31 21:19:58 2017 +0300
@@ -30,6 +30,8 @@ typedef ngx_int_t (*ngx_http_get_variabl
#define NGX_HTTP_VAR_NOCACHEABLE 2
#define NGX_HTTP_VAR_INDEXED 4
#define NGX_HTTP_VAR_NOHASH 8
+#define NGX_HTTP_VAR_WEAK 16
+#define NGX_HTTP_VAR_PREFIX 32
struct ngx_http_variable_s {
diff -r 25203fc377fb -r d2b2ff157da5 src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h Tue Jan 31 12:09:40 2017 +0300
+++ b/src/stream/ngx_stream.h Tue Jan 31 21:19:58 2017 +0300
@@ -153,7 +153,8 @@ typedef struct {
ngx_hash_t variables_hash;
- ngx_array_t variables; /* ngx_stream_variable_t */
+ ngx_array_t variables; /* ngx_stream_variable_t */
+ ngx_array_t prefix_variables; /* ngx_stream_variable_t */
ngx_uint_t ncaptures;
ngx_uint_t variables_hash_max_size;
diff -r 25203fc377fb -r d2b2ff157da5 src/stream/ngx_stream_variables.c
--- a/src/stream/ngx_stream_variables.c Tue Jan 31 12:09:40 2017 +0300
+++ b/src/stream/ngx_stream_variables.c Tue Jan 31 21:19:58 2017 +0300
@@ -10,6 +10,8 @@
#include <ngx_stream.h>
#include <nginx.h>
+static ngx_stream_variable_t *ngx_stream_add_prefix_variable(ngx_conf_t *cf,
+ ngx_str_t *name, ngx_uint_t flags);
static ngx_int_t ngx_stream_variable_binary_remote_addr(
ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
@@ -137,6 +139,10 @@ ngx_stream_add_variable(ngx_conf_t *cf,
return NULL;
}
+ if (flags & NGX_STREAM_VAR_PREFIX) {
+ return ngx_stream_add_prefix_variable(cf, name, flags);
+ }
+
cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module);
key = cmcf->variables_keys->keys.elts;
@@ -155,6 +161,8 @@ ngx_stream_add_variable(ngx_conf_t *cf,
return NULL;
}
+ v->flags &= flags | ~NGX_STREAM_VAR_WEAK;
+
return v;
}
@@ -193,6 +201,60 @@ ngx_stream_add_variable(ngx_conf_t *cf,
}
+static ngx_stream_variable_t *
+ngx_stream_add_prefix_variable(ngx_conf_t *cf, ngx_str_t *name,
+ ngx_uint_t flags)
+{
+ ngx_uint_t i;
+ ngx_stream_variable_t *v;
+ ngx_stream_core_main_conf_t *cmcf;
+
+ cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module);
+
+ v = cmcf->prefix_variables.elts;
+ for (i = 0; i < cmcf->prefix_variables.nelts; i++) {
+ if (name->len != v[i].name.len
+ || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
+ {
+ continue;
+ }
+
+ v = &v[i];
+
+ if (!(v->flags & NGX_STREAM_VAR_CHANGEABLE)) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "the duplicate \"%V\" variable", name);
+ return NULL;
+ }
+
+ v->flags &= flags | ~NGX_STREAM_VAR_WEAK;
+
+ return v;
+ }
+
+ v = ngx_array_push(&cmcf->prefix_variables);
+ if (v == NULL) {
+ return NULL;
+ }
+
+ v->name.len = name->len;
+ v->name.data = ngx_pnalloc(cf->pool, name->len);
+ if (v->name.data == NULL) {
+ return NULL;
+ }
+
+ ngx_strlow(v->name.data, name->data, name->len);
+
+ v->set_handler = NULL;
+ v->get_handler = NULL;
+ v->data = 0;
+ v->flags = flags;
+ v->index = 0;
+
+ return v;
+}
+
+
ngx_int_t
ngx_stream_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)
{
@@ -327,6 +389,8 @@ ngx_stream_variable_value_t *
ngx_stream_get_variable(ngx_stream_session_t *s, ngx_str_t *name,
ngx_uint_t key)
{
+ size_t len;
+ ngx_uint_t i, n;
ngx_stream_variable_t *v;
ngx_stream_variable_value_t *vv;
ngx_stream_core_main_conf_t *cmcf;
@@ -365,6 +429,28 @@ ngx_stream_get_variable(ngx_stream_sessi
return NULL;
}
+ len = 0;
+
+ v = cmcf->prefix_variables.elts;
+ n = cmcf->prefix_variables.nelts;
+
+ for (i = 0; i < cmcf->prefix_variables.nelts; i++) {
+ if (name->len >= v[i].name.len && name->len > len
+ && ngx_strncmp(name->data, v[i].name.data, v[i].name.len) == 0)
+ {
+ len = v[i].name.len;
+ n = i;
+ }
+ }
+
+ if (n != cmcf->prefix_variables.nelts) {
+ if (v[n].get_handler(s, vv, (uintptr_t) name) == NGX_OK) {
+ return vv;
+ }
+
+ return NULL;
+ }
+
vv->not_found = 1;
return vv;
@@ -1000,7 +1086,6 @@ ngx_stream_regex_exec(ngx_stream_session
ngx_int_t
ngx_stream_variables_add_core_vars(ngx_conf_t *cf)
{
- ngx_int_t rc;
ngx_stream_variable_t *cv, *v;
ngx_stream_core_main_conf_t *cmcf;
@@ -1021,27 +1106,20 @@ ngx_stream_variables_add_core_vars(ngx_c
return NGX_ERROR;
}
+ if (ngx_array_init(&cmcf->prefix_variables, cf->pool, 8,
+ sizeof(ngx_stream_variable_t))
+ != NGX_OK)
+ {
+ return NGX_ERROR;
+ }
+
for (cv = ngx_stream_core_variables; cv->name.len; cv++) {
- v = ngx_palloc(cf->pool, sizeof(ngx_stream_variable_t));
+ v = ngx_stream_add_variable(cf, &cv->name, cv->flags);
if (v == NULL) {
return NGX_ERROR;
}
*v = *cv;
-
- rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,
- NGX_HASH_READONLY_KEY);
-
- if (rc == NGX_OK) {
- continue;
- }
-
- if (rc == NGX_BUSY) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "conflicting variable name \"%V\"", &v->name);
- }
-
- return NGX_ERROR;
}
return NGX_OK;
@@ -1051,10 +1129,11 @@ ngx_stream_variables_add_core_vars(ngx_c
ngx_int_t
ngx_stream_variables_init_vars(ngx_conf_t *cf)
{
+ size_t len;
ngx_uint_t i, n;
ngx_hash_key_t *key;
ngx_hash_init_t hash;
- ngx_stream_variable_t *v, *av;
+ ngx_stream_variable_t *v, *av, *pv;
ngx_stream_core_main_conf_t *cmcf;
/* set the handlers for the indexed stream variables */
@@ -1062,6 +1141,7 @@ ngx_stream_variables_init_vars(ngx_conf_
cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module);
v = cmcf->variables.elts;
+ pv = cmcf->prefix_variables.elts;
key = cmcf->variables_keys->keys.elts;
for (i = 0; i < cmcf->variables.nelts; i++) {
@@ -1082,7 +1162,9 @@ ngx_stream_variables_init_vars(ngx_conf_
av->index = i;
- if (av->get_handler == NULL) {
+ if (av->get_handler == NULL
+ || (av->flags & NGX_STREAM_VAR_WEAK))
+ {
break;
}
@@ -1090,10 +1172,32 @@ ngx_stream_variables_init_vars(ngx_conf_
}
}
- ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
- "unknown \"%V\" variable", &v[i].name);
+ len = 0;
+ av = NULL;
- return NGX_ERROR;
+ for (n = 0; n < cmcf->prefix_variables.nelts; n++) {
+ if (v[i].name.len >= pv[n].name.len && v[i].name.len > len
+ && ngx_strncmp(v[i].name.data, pv[n].name.data, pv[n].name.len)
+ == 0)
+ {
+ av = &pv[n];
+ len = pv[n].name.len;
+ }
+ }
+
+ if (av) {
+ v[i].get_handler = av->get_handler;
+ v[i].data = (uintptr_t) &v[i].name;
+ v[i].flags = av->flags;
+
+ goto next;
+ }
+
+ if (v[i].get_handler == NULL) {
+ ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+ "unknown \"%V\" variable", &v[i].name);
+ return NGX_ERROR;
+ }
next:
continue;
diff -r 25203fc377fb -r d2b2ff157da5 src/stream/ngx_stream_variables.h
--- a/src/stream/ngx_stream_variables.h Tue Jan 31 12:09:40 2017 +0300
+++ b/src/stream/ngx_stream_variables.h Tue Jan 31 21:19:58 2017 +0300
@@ -30,6 +30,8 @@ typedef ngx_int_t (*ngx_stream_get_varia
#define NGX_STREAM_VAR_NOCACHEABLE 2
#define NGX_STREAM_VAR_INDEXED 4
#define NGX_STREAM_VAR_NOHASH 8
+#define NGX_STREAM_VAR_WEAK 16
+#define NGX_STREAM_VAR_PREFIX 32
struct ngx_stream_variable_s {
More information about the nginx-devel
mailing list