aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDmitry Volyntsev <xeioex@nginx.com>2017-01-31 21:19:58 +0300
committerDmitry Volyntsev <xeioex@nginx.com>2017-01-31 21:19:58 +0300
commit897eaa9215a8f5832fb7bffe5016a22562b90d0a (patch)
treeb93a5f4c0f037d26cdec1c984c29e656069a1878 /src
parent7e080678d61d946891c035d79b73715f1cc57682 (diff)
downloadnginx-897eaa9215a8f5832fb7bffe5016a22562b90d0a.tar.gz
nginx-897eaa9215a8f5832fb7bffe5016a22562b90d0a.zip
Variables: generic prefix variables.
Diffstat (limited to 'src')
-rw-r--r--src/http/modules/ngx_http_rewrite_module.c13
-rw-r--r--src/http/ngx_http_core_module.h3
-rw-r--r--src/http/ngx_http_upstream.c14
-rw-r--r--src/http/ngx_http_upstream.h5
-rw-r--r--src/http/ngx_http_variables.c240
-rw-r--r--src/http/ngx_http_variables.h2
-rw-r--r--src/stream/ngx_stream.h3
-rw-r--r--src/stream/ngx_stream_variables.c146
-rw-r--r--src/stream/ngx_stream_variables.h2
9 files changed, 266 insertions, 162 deletions
diff --git a/src/http/modules/ngx_http_rewrite_module.c b/src/http/modules/ngx_http_rewrite_module.c
index 6b2444cbd..a6f1fc820 100644
--- a/src/http/modules/ngx_http_rewrite_module.c
+++ b/src/http/modules/ngx_http_rewrite_module.c
@@ -917,7 +917,8 @@ ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
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_command_t *cmd, void *conf)
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 --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index 70e693a6b..5018da09a 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -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 --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 74a7c64b0..c59519416 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -162,6 +162,10 @@ static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
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_upstream_vars[] = {
#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_variable(ngx_http_request_t *r,
}
-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_http_request_t *r,
}
-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 --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index 86e2ee223..7f601ee0b 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -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 --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
index 62006ba93..6138819dd 100644
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -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_core_variables[] = {
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, ngx_str_t *name, ngx_uint_t flags)
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, ngx_str_t *name, ngx_uint_t flags)
return NULL;
}
+ v->flags &= flags | ~NGX_HTTP_VAR_WEAK;
+
return v;
}
@@ -440,6 +461,59 @@ ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
}
+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_request_t *r, ngx_uint_t index)
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 *r, ngx_str_t *name, ngx_uint_t key)
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)
- {
- 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;
- }
-
- 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;
- }
+ len = 0;
- if (name->len >= 16
- && ngx_strncmp(name->data, "upstream_cookie_", 16) == 0)
- {
+ v = cmcf->prefix_variables.elts;
+ n = cmcf->prefix_variables.nelts;
- if (ngx_http_upstream_cookie_variable(r, vv, (uintptr_t) name)
- == NGX_OK)
+ 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;
+ len = v[i].name.len;
+ n = i;
}
-
- 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 *r, ngx_http_regex_t *re, ngx_str_t *s)
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_conf_t *cf)
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_conf_t *cf)
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 *cf)
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 *cf)
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 *cf)
}
}
- 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;
- }
-
- 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;
+ len = 0;
+ av = NULL;
- continue;
+ 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 >= 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;
+ v[i].flags = av->flags;
- continue;
+ 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;
+ if (v[i].get_handler == NULL) {
+ ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+ "unknown \"%V\" variable", &v[i].name);
- continue;
+ return NGX_ERROR;
}
- ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
- "unknown \"%V\" variable", &v[i].name);
-
- return NGX_ERROR;
-
next:
continue;
}
diff --git a/src/http/ngx_http_variables.h b/src/http/ngx_http_variables.h
index 829fab31e..df337defc 100644
--- a/src/http/ngx_http_variables.h
+++ b/src/http/ngx_http_variables.h
@@ -30,6 +30,8 @@ typedef ngx_int_t (*ngx_http_get_variable_pt) (ngx_http_request_t *r,
#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 --git a/src/stream/ngx_stream.h b/src/stream/ngx_stream.h
index 9e4169c38..814e3b99a 100644
--- a/src/stream/ngx_stream.h
+++ b/src/stream/ngx_stream.h
@@ -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 --git a/src/stream/ngx_stream_variables.c b/src/stream/ngx_stream_variables.c
index 9dc93ee57..5d15f3a6d 100644
--- a/src/stream/ngx_stream_variables.c
+++ b/src/stream/ngx_stream_variables.c
@@ -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, ngx_str_t *name, ngx_uint_t flags)
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, ngx_str_t *name, ngx_uint_t flags)
return NULL;
}
+ v->flags &= flags | ~NGX_STREAM_VAR_WEAK;
+
return v;
}
@@ -193,6 +201,60 @@ ngx_stream_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
}
+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_session_t *s, ngx_str_t *name,
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_t *s, ngx_stream_regex_t *re,
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_conf_t *cf)
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_conf_t *cf)
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_t *cf)
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_t *cf)
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_t *cf)
}
}
- 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 --git a/src/stream/ngx_stream_variables.h b/src/stream/ngx_stream_variables.h
index e4151e224..815511154 100644
--- a/src/stream/ngx_stream_variables.h
+++ b/src/stream/ngx_stream_variables.h
@@ -30,6 +30,8 @@ typedef ngx_int_t (*ngx_stream_get_variable_pt) (ngx_stream_session_t *s,
#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 {