2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_uint_t hash_max_size;
|
|
|
|
ngx_uint_t hash_bucket_size;
|
|
|
|
} ngx_http_map_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_hash_keys_arrays_t keys;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
ngx_array_t *values_hash;
|
2010-11-26 20:25:51 +08:00
|
|
|
ngx_array_t var_values;
|
2011-03-16 23:32:31 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
ngx_array_t regexes;
|
|
|
|
#endif
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
ngx_http_variable_value_t *default_value;
|
2010-11-26 20:25:51 +08:00
|
|
|
ngx_conf_t *cf;
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_uint_t hostnames; /* unsigned hostnames:1 */
|
|
|
|
} ngx_http_map_conf_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2011-03-16 23:32:31 +08:00
|
|
|
ngx_http_map_t map;
|
2010-11-25 23:22:43 +08:00
|
|
|
ngx_http_complex_value_t value;
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_http_variable_value_t *default_value;
|
|
|
|
ngx_uint_t hostnames; /* unsigned hostnames:1 */
|
|
|
|
} ngx_http_map_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
static int ngx_libc_cdecl ngx_http_map_cmp_dns_wildcards(const void *one,
|
|
|
|
const void *two);
|
|
|
|
static void *ngx_http_map_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
|
|
static char *ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_map_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("map"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
|
|
|
|
ngx_http_map_block,
|
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("map_hash_max_size"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_map_conf_t, hash_max_size),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("map_hash_bucket_size"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_map_conf_t, hash_bucket_size),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_map_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
NULL, /* postconfiguration */
|
|
|
|
|
|
|
|
ngx_http_map_create_conf, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
NULL, /* create location configuration */
|
|
|
|
NULL /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_map_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_http_map_module_ctx, /* module context */
|
|
|
|
ngx_http_map_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
|
|
|
NULL, /* init master */
|
|
|
|
NULL, /* init module */
|
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_map_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
|
|
uintptr_t data)
|
|
|
|
{
|
|
|
|
ngx_http_map_ctx_t *map = (ngx_http_map_ctx_t *) data;
|
|
|
|
|
|
|
|
size_t len;
|
2010-11-25 23:22:43 +08:00
|
|
|
ngx_str_t val;
|
|
|
|
ngx_http_variable_value_t *value;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http map started");
|
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
if (ngx_http_complex_value(r, &map->value, &val) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
2006-10-21 03:07:50 +08:00
|
|
|
}
|
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
len = val.len;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
if (len && map->hostnames && val.data[len - 1] == '.') {
|
2005-12-16 23:07:08 +08:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2011-05-30 20:36:17 +08:00
|
|
|
value = ngx_http_map_find(r, &map->map, &val);
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2010-11-26 20:25:51 +08:00
|
|
|
if (value == NULL) {
|
|
|
|
value = map->default_value;
|
|
|
|
}
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2010-11-26 20:25:51 +08:00
|
|
|
if (!value->valid) {
|
|
|
|
value = ngx_http_get_flushed_variable(r, (ngx_uint_t) value->data);
|
|
|
|
|
|
|
|
if (value == NULL || value->not_found) {
|
|
|
|
value = &ngx_http_variable_null_value;
|
|
|
|
}
|
2005-12-16 23:07:08 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 20:25:51 +08:00
|
|
|
*v = *value;
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
2010-11-25 23:22:43 +08:00
|
|
|
"http map: \"%v\" \"%v\"", &val, v);
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_http_map_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_map_conf_t *mcf;
|
|
|
|
|
|
|
|
mcf = ngx_palloc(cf->pool, sizeof(ngx_http_map_conf_t));
|
|
|
|
if (mcf == NULL) {
|
2009-06-03 00:09:44 +08:00
|
|
|
return NULL;
|
2005-12-16 23:07:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mcf->hash_max_size = NGX_CONF_UNSET_UINT;
|
|
|
|
mcf->hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
|
|
|
|
|
|
return mcf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
ngx_http_map_conf_t *mcf = conf;
|
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
char *rv;
|
|
|
|
ngx_str_t *value, name;
|
|
|
|
ngx_conf_t save;
|
|
|
|
ngx_pool_t *pool;
|
|
|
|
ngx_hash_init_t hash;
|
|
|
|
ngx_http_map_ctx_t *map;
|
|
|
|
ngx_http_variable_t *var;
|
|
|
|
ngx_http_map_conf_ctx_t ctx;
|
|
|
|
ngx_http_compile_complex_value_t ccv;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) {
|
|
|
|
mcf->hash_max_size = 2048;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) {
|
|
|
|
mcf->hash_bucket_size = ngx_cacheline_size;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size,
|
|
|
|
ngx_cacheline_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
map = ngx_pcalloc(cf->pool, sizeof(ngx_http_map_ctx_t));
|
|
|
|
if (map == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
ccv.cf = cf;
|
|
|
|
ccv.value = &value[1];
|
|
|
|
ccv.complex_value = &map->value;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2010-11-25 23:22:43 +08:00
|
|
|
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
2005-12-16 23:07:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = value[2];
|
|
|
|
name.len--;
|
|
|
|
name.data++;
|
|
|
|
|
2007-10-15 02:56:15 +08:00
|
|
|
var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
2005-12-16 23:07:08 +08:00
|
|
|
if (var == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-04-19 23:30:56 +08:00
|
|
|
var->get_handler = ngx_http_map_variable;
|
2005-12-16 23:07:08 +08:00
|
|
|
var->data = (uintptr_t) map;
|
|
|
|
|
|
|
|
pool = ngx_create_pool(16384, cf->log);
|
|
|
|
if (pool == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ctx.keys.pool = cf->pool;
|
|
|
|
ctx.keys.temp_pool = pool;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (ngx_hash_keys_array_init(&ctx.keys, NGX_HASH_LARGE) != NGX_OK) {
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ctx.values_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * ctx.keys.hsize);
|
2005-12-16 23:07:08 +08:00
|
|
|
if (ctx.values_hash == NULL) {
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-11-26 20:25:51 +08:00
|
|
|
if (ngx_array_init(&ctx.var_values, cf->pool, 2,
|
|
|
|
sizeof(ngx_http_variable_value_t))
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:32:31 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
if (ngx_array_init(&ctx.regexes, cf->pool, 2, sizeof(ngx_http_map_regex_t))
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
ctx.default_value = NULL;
|
2010-11-26 20:25:51 +08:00
|
|
|
ctx.cf = &save;
|
2005-12-16 23:07:08 +08:00
|
|
|
ctx.hostnames = 0;
|
|
|
|
|
|
|
|
save = *cf;
|
|
|
|
cf->pool = pool;
|
|
|
|
cf->ctx = &ctx;
|
|
|
|
cf->handler = ngx_http_map;
|
|
|
|
cf->handler_conf = conf;
|
|
|
|
|
|
|
|
rv = ngx_conf_parse(cf, NULL);
|
|
|
|
|
|
|
|
*cf = save;
|
|
|
|
|
|
|
|
if (rv != NGX_CONF_OK) {
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
map->default_value = ctx.default_value ? ctx.default_value:
|
|
|
|
&ngx_http_variable_null_value;
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
hash.key = ngx_hash_key_lc;
|
|
|
|
hash.max_size = mcf->hash_max_size;
|
|
|
|
hash.bucket_size = mcf->hash_bucket_size;
|
|
|
|
hash.name = "map_hash";
|
|
|
|
hash.pool = cf->pool;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (ctx.keys.keys.nelts) {
|
2011-03-16 23:32:31 +08:00
|
|
|
hash.hash = &map->map.hash.hash;
|
2005-12-16 23:07:08 +08:00
|
|
|
hash.temp_pool = NULL;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts)
|
2005-12-19 00:02:44 +08:00
|
|
|
!= NGX_OK)
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_destroy_pool(pool);
|
2005-12-16 23:07:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (ctx.keys.dns_wc_head.nelts) {
|
|
|
|
|
|
|
|
ngx_qsort(ctx.keys.dns_wc_head.elts,
|
|
|
|
(size_t) ctx.keys.dns_wc_head.nelts,
|
|
|
|
sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards);
|
|
|
|
|
|
|
|
hash.hash = NULL;
|
|
|
|
hash.temp_pool = pool;
|
|
|
|
|
|
|
|
if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_head.elts,
|
|
|
|
ctx.keys.dns_wc_head.nelts)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2011-03-16 23:32:31 +08:00
|
|
|
map->map.hash.wc_head = (ngx_hash_wildcard_t *) hash.hash;
|
2007-06-12 03:49:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.keys.dns_wc_tail.nelts) {
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
ngx_qsort(ctx.keys.dns_wc_tail.elts,
|
|
|
|
(size_t) ctx.keys.dns_wc_tail.nelts,
|
2005-12-16 23:07:08 +08:00
|
|
|
sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards);
|
|
|
|
|
|
|
|
hash.hash = NULL;
|
|
|
|
hash.temp_pool = pool;
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_tail.elts,
|
|
|
|
ctx.keys.dns_wc_tail.nelts)
|
2005-12-16 23:07:08 +08:00
|
|
|
!= NGX_OK)
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_destroy_pool(pool);
|
2005-12-16 23:07:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:32:31 +08:00
|
|
|
map->map.hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash;
|
2005-12-16 23:07:08 +08:00
|
|
|
}
|
|
|
|
|
2011-03-16 23:32:31 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
|
|
|
|
if (ctx.regexes.nelts) {
|
|
|
|
map->map.regex = ctx.regexes.elts;
|
|
|
|
map->map.nregex = ctx.regexes.nelts;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ngx_libc_cdecl
|
|
|
|
ngx_http_map_cmp_dns_wildcards(const void *one, const void *two)
|
|
|
|
{
|
|
|
|
ngx_hash_key_t *first, *second;
|
|
|
|
|
|
|
|
first = (ngx_hash_key_t *) one;
|
|
|
|
second = (ngx_hash_key_t *) two;
|
|
|
|
|
2009-09-12 17:28:37 +08:00
|
|
|
return ngx_dns_strcmp(first->key.data, second->key.data);
|
2005-12-16 23:07:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
|
|
|
{
|
2010-11-26 20:25:51 +08:00
|
|
|
ngx_int_t rc, index;
|
|
|
|
ngx_str_t *value, file, name;
|
2007-06-12 03:49:22 +08:00
|
|
|
ngx_uint_t i, key;
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_http_map_conf_ctx_t *ctx;
|
2005-12-19 00:02:44 +08:00
|
|
|
ngx_http_variable_value_t *var, **vp;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
ctx = cf->ctx;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
if (cf->args->nelts == 1
|
|
|
|
&& ngx_strcmp(value[0].data, "hostnames") == 0)
|
|
|
|
{
|
|
|
|
ctx->hostnames = 1;
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
|
|
|
|
} else if (cf->args->nelts != 2) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid number of the map parameters");
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[0].data, "include") == 0) {
|
|
|
|
file = value[1];
|
|
|
|
|
2010-06-24 00:34:54 +08:00
|
|
|
if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) {
|
2005-12-16 23:07:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
|
|
|
|
|
|
return ngx_conf_parse(cf, &file);
|
|
|
|
}
|
|
|
|
|
2010-11-26 20:25:51 +08:00
|
|
|
if (value[1].data[0] == '$') {
|
|
|
|
name = value[1];
|
|
|
|
name.len--;
|
|
|
|
name.data++;
|
|
|
|
|
|
|
|
index = ngx_http_get_variable_index(ctx->cf, &name);
|
|
|
|
if (index == NGX_ERROR) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
var = ctx->var_values.elts;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->var_values.nelts; i++) {
|
|
|
|
if (index == (ngx_int_t) var[i].data) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var = ngx_palloc(ctx->keys.pool, sizeof(ngx_http_variable_value_t));
|
|
|
|
if (var == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->valid = 0;
|
|
|
|
var->no_cacheable = 0;
|
|
|
|
var->not_found = 0;
|
|
|
|
var->len = 0;
|
|
|
|
var->data = (u_char *) index;
|
|
|
|
|
|
|
|
vp = ngx_array_push(&ctx->var_values);
|
|
|
|
if (vp == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = var;
|
|
|
|
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
key = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < value[1].len; i++) {
|
|
|
|
key = ngx_hash(key, value[1].data[i]);
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
key %= ctx->keys.hsize;
|
2005-12-16 23:07:08 +08:00
|
|
|
|
|
|
|
vp = ctx->values_hash[key].elts;
|
|
|
|
|
|
|
|
if (vp) {
|
|
|
|
for (i = 0; i < ctx->values_hash[key].nelts; i++) {
|
|
|
|
if (value[1].len != (size_t) vp[i]->len) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strncmp(value[1].data, vp[i]->data, value[1].len) == 0) {
|
|
|
|
var = vp[i];
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ngx_array_init(&ctx->values_hash[key], cf->pool, 4,
|
|
|
|
sizeof(ngx_http_variable_value_t *))
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
var = ngx_palloc(ctx->keys.pool, sizeof(ngx_http_variable_value_t));
|
2005-12-16 23:07:08 +08:00
|
|
|
if (var == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->len = value[1].len;
|
2005-12-27 01:07:48 +08:00
|
|
|
var->data = ngx_pstrdup(ctx->keys.pool, &value[1]);
|
2005-12-16 23:07:08 +08:00
|
|
|
if (var->data == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->valid = 1;
|
2007-10-15 02:56:15 +08:00
|
|
|
var->no_cacheable = 0;
|
2005-12-16 23:07:08 +08:00
|
|
|
var->not_found = 0;
|
|
|
|
|
|
|
|
vp = ngx_array_push(&ctx->values_hash[key]);
|
|
|
|
if (vp == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = var;
|
|
|
|
|
|
|
|
found:
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (ngx_strcmp(value[0].data, "default") == 0) {
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (ctx->default_value) {
|
2005-12-19 00:02:44 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2007-06-12 03:49:22 +08:00
|
|
|
"duplicate default map parameter");
|
2005-12-16 23:07:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
ctx->default_value = var;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
2005-12-16 23:07:08 +08:00
|
|
|
}
|
|
|
|
|
2011-03-16 23:32:31 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
|
|
|
|
if (value[0].len && value[0].data[0] == '~') {
|
|
|
|
ngx_regex_compile_t rc;
|
|
|
|
ngx_http_map_regex_t *regex;
|
|
|
|
u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
|
|
|
|
|
|
regex = ngx_array_push(&ctx->regexes);
|
|
|
|
if (regex == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[0].len--;
|
|
|
|
value[0].data++;
|
|
|
|
|
|
|
|
ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
|
|
|
2011-05-30 22:24:17 +08:00
|
|
|
if (value[0].data[0] == '*') {
|
|
|
|
value[0].len--;
|
|
|
|
value[0].data++;
|
|
|
|
rc.options = NGX_REGEX_CASELESS;
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:32:31 +08:00
|
|
|
rc.pattern = value[0];
|
|
|
|
rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
|
|
rc.err.data = errstr;
|
|
|
|
|
|
|
|
regex->regex = ngx_http_regex_compile(ctx->cf, &rc);
|
|
|
|
if (regex->regex == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
regex->value = var;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-11-25 23:36:24 +08:00
|
|
|
if (value[0].len && value[0].data[0] == '\\') {
|
2007-06-12 03:49:22 +08:00
|
|
|
value[0].len--;
|
|
|
|
value[0].data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_hash_add_key(&ctx->keys, &value[0], var,
|
|
|
|
(ctx->hostnames) ? NGX_HASH_WILDCARD_KEY : 0);
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2005-12-19 00:02:44 +08:00
|
|
|
if (rc == NGX_OK) {
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (rc == NGX_DECLINED) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid hostname or wildcard \"%V\"", &value[0]);
|
|
|
|
}
|
|
|
|
|
2005-12-19 00:02:44 +08:00
|
|
|
if (rc == NGX_BUSY) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"conflicting parameter \"%V\"", &value[0]);
|
|
|
|
}
|
2005-12-16 23:07:08 +08:00
|
|
|
|
2005-12-19 00:02:44 +08:00
|
|
|
return NGX_CONF_ERROR;
|
2005-12-16 23:07:08 +08:00
|
|
|
}
|