comparison src/http/modules/ngx_http_map_module.c @ 3792:164a7f8df979

allow variable as "map" value
author Igor Sysoev <igor@sysoev.ru>
date Fri, 26 Nov 2010 12:25:51 +0000
parents 11701c4c0358
children 83cd1910329c
comparison
equal deleted inserted replaced
3791:11701c4c0358 3792:164a7f8df979
17 17
18 typedef struct { 18 typedef struct {
19 ngx_hash_keys_arrays_t keys; 19 ngx_hash_keys_arrays_t keys;
20 20
21 ngx_array_t *values_hash; 21 ngx_array_t *values_hash;
22 ngx_array_t var_values;
22 23
23 ngx_http_variable_value_t *default_value; 24 ngx_http_variable_value_t *default_value;
25 ngx_conf_t *cf;
24 ngx_uint_t hostnames; /* unsigned hostnames:1 */ 26 ngx_uint_t hostnames; /* unsigned hostnames:1 */
25 } ngx_http_map_conf_ctx_t; 27 } ngx_http_map_conf_ctx_t;
26 28
27 29
28 typedef struct { 30 typedef struct {
124 126
125 key = ngx_hash_strlow(val.data, val.data, len); 127 key = ngx_hash_strlow(val.data, val.data, len);
126 128
127 value = ngx_hash_find_combined(&map->hash, key, val.data, len); 129 value = ngx_hash_find_combined(&map->hash, key, val.data, len);
128 130
129 if (value) { 131 if (value == NULL) {
130 *v = *value; 132 value = map->default_value;
131 133 }
132 } else { 134
133 *v = *map->default_value; 135 if (!value->valid) {
134 } 136 value = ngx_http_get_flushed_variable(r, (ngx_uint_t) value->data);
137
138 if (value == NULL || value->not_found) {
139 value = &ngx_http_variable_null_value;
140 }
141 }
142
143 *v = *value;
135 144
136 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 145 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
137 "http map: \"%v\" \"%v\"", &val, v); 146 "http map: \"%v\" \"%v\"", &val, v);
138 147
139 return NGX_OK; 148 return NGX_OK;
230 if (ctx.values_hash == NULL) { 239 if (ctx.values_hash == NULL) {
231 ngx_destroy_pool(pool); 240 ngx_destroy_pool(pool);
232 return NGX_CONF_ERROR; 241 return NGX_CONF_ERROR;
233 } 242 }
234 243
244 if (ngx_array_init(&ctx.var_values, cf->pool, 2,
245 sizeof(ngx_http_variable_value_t))
246 != NGX_OK)
247 {
248 ngx_destroy_pool(pool);
249 return NGX_CONF_ERROR;
250 }
251
235 ctx.default_value = NULL; 252 ctx.default_value = NULL;
253 ctx.cf = &save;
236 ctx.hostnames = 0; 254 ctx.hostnames = 0;
237 255
238 save = *cf; 256 save = *cf;
239 cf->pool = pool; 257 cf->pool = pool;
240 cf->ctx = &ctx; 258 cf->ctx = &ctx;
330 348
331 349
332 static char * 350 static char *
333 ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) 351 ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
334 { 352 {
335 ngx_int_t rc; 353 ngx_int_t rc, index;
336 ngx_str_t *value, file; 354 ngx_str_t *value, file, name;
337 ngx_uint_t i, key; 355 ngx_uint_t i, key;
338 ngx_http_map_conf_ctx_t *ctx; 356 ngx_http_map_conf_ctx_t *ctx;
339 ngx_http_variable_value_t *var, **vp; 357 ngx_http_variable_value_t *var, **vp;
340 358
341 ctx = cf->ctx; 359 ctx = cf->ctx;
364 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); 382 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
365 383
366 return ngx_conf_parse(cf, &file); 384 return ngx_conf_parse(cf, &file);
367 } 385 }
368 386
387 if (value[1].data[0] == '$') {
388 name = value[1];
389 name.len--;
390 name.data++;
391
392 index = ngx_http_get_variable_index(ctx->cf, &name);
393 if (index == NGX_ERROR) {
394 return NGX_CONF_ERROR;
395 }
396
397 var = ctx->var_values.elts;
398
399 for (i = 0; i < ctx->var_values.nelts; i++) {
400 if (index == (ngx_int_t) var[i].data) {
401 goto found;
402 }
403 }
404
405 var = ngx_palloc(ctx->keys.pool, sizeof(ngx_http_variable_value_t));
406 if (var == NULL) {
407 return NGX_CONF_ERROR;
408 }
409
410 var->valid = 0;
411 var->no_cacheable = 0;
412 var->not_found = 0;
413 var->len = 0;
414 var->data = (u_char *) index;
415
416 vp = ngx_array_push(&ctx->var_values);
417 if (vp == NULL) {
418 return NGX_CONF_ERROR;
419 }
420
421 *vp = var;
422
423 goto found;
424 }
425
369 key = 0; 426 key = 0;
370 427
371 for (i = 0; i < value[1].len; i++) { 428 for (i = 0; i < value[1].len; i++) {
372 key = ngx_hash(key, value[1].data[i]); 429 key = ngx_hash(key, value[1].data[i]);
373 } 430 }