Mercurial > hg > nginx
comparison src/http/modules/ngx_http_map_module.c @ 3872:83cd1910329c
allow regex as "map" parameter
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 16 Mar 2011 15:32:31 +0000 |
parents | 164a7f8df979 |
children | cb2432ed17fd |
comparison
equal
deleted
inserted
replaced
3871:8ccd6ed1d4bb | 3872:83cd1910329c |
---|---|
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 ngx_array_t var_values; |
23 #if (NGX_PCRE) | |
24 ngx_array_t regexes; | |
25 #endif | |
23 | 26 |
24 ngx_http_variable_value_t *default_value; | 27 ngx_http_variable_value_t *default_value; |
25 ngx_conf_t *cf; | 28 ngx_conf_t *cf; |
26 ngx_uint_t hostnames; /* unsigned hostnames:1 */ | 29 ngx_uint_t hostnames; /* unsigned hostnames:1 */ |
27 } ngx_http_map_conf_ctx_t; | 30 } ngx_http_map_conf_ctx_t; |
28 | 31 |
29 | 32 |
30 typedef struct { | 33 typedef struct { |
31 ngx_hash_combined_t hash; | 34 ngx_http_map_t map; |
32 ngx_http_complex_value_t value; | 35 ngx_http_complex_value_t value; |
33 ngx_http_variable_value_t *default_value; | 36 ngx_http_variable_value_t *default_value; |
34 ngx_uint_t hostnames; /* unsigned hostnames:1 */ | 37 ngx_uint_t hostnames; /* unsigned hostnames:1 */ |
35 } ngx_http_map_ctx_t; | 38 } ngx_http_map_ctx_t; |
36 | 39 |
124 len--; | 127 len--; |
125 } | 128 } |
126 | 129 |
127 key = ngx_hash_strlow(val.data, val.data, len); | 130 key = ngx_hash_strlow(val.data, val.data, len); |
128 | 131 |
129 value = ngx_hash_find_combined(&map->hash, key, val.data, len); | 132 value = ngx_http_map_find(r, &map->map, key, val.data, len, &val); |
130 | 133 |
131 if (value == NULL) { | 134 if (value == NULL) { |
132 value = map->default_value; | 135 value = map->default_value; |
133 } | 136 } |
134 | 137 |
247 { | 250 { |
248 ngx_destroy_pool(pool); | 251 ngx_destroy_pool(pool); |
249 return NGX_CONF_ERROR; | 252 return NGX_CONF_ERROR; |
250 } | 253 } |
251 | 254 |
255 #if (NGX_PCRE) | |
256 if (ngx_array_init(&ctx.regexes, cf->pool, 2, sizeof(ngx_http_map_regex_t)) | |
257 != NGX_OK) | |
258 { | |
259 ngx_destroy_pool(pool); | |
260 return NGX_CONF_ERROR; | |
261 } | |
262 #endif | |
263 | |
252 ctx.default_value = NULL; | 264 ctx.default_value = NULL; |
253 ctx.cf = &save; | 265 ctx.cf = &save; |
254 ctx.hostnames = 0; | 266 ctx.hostnames = 0; |
255 | 267 |
256 save = *cf; | 268 save = *cf; |
276 hash.bucket_size = mcf->hash_bucket_size; | 288 hash.bucket_size = mcf->hash_bucket_size; |
277 hash.name = "map_hash"; | 289 hash.name = "map_hash"; |
278 hash.pool = cf->pool; | 290 hash.pool = cf->pool; |
279 | 291 |
280 if (ctx.keys.keys.nelts) { | 292 if (ctx.keys.keys.nelts) { |
281 hash.hash = &map->hash.hash; | 293 hash.hash = &map->map.hash.hash; |
282 hash.temp_pool = NULL; | 294 hash.temp_pool = NULL; |
283 | 295 |
284 if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts) | 296 if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts) |
285 != NGX_OK) | 297 != NGX_OK) |
286 { | 298 { |
304 { | 316 { |
305 ngx_destroy_pool(pool); | 317 ngx_destroy_pool(pool); |
306 return NGX_CONF_ERROR; | 318 return NGX_CONF_ERROR; |
307 } | 319 } |
308 | 320 |
309 map->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; | 321 map->map.hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; |
310 } | 322 } |
311 | 323 |
312 if (ctx.keys.dns_wc_tail.nelts) { | 324 if (ctx.keys.dns_wc_tail.nelts) { |
313 | 325 |
314 ngx_qsort(ctx.keys.dns_wc_tail.elts, | 326 ngx_qsort(ctx.keys.dns_wc_tail.elts, |
324 { | 336 { |
325 ngx_destroy_pool(pool); | 337 ngx_destroy_pool(pool); |
326 return NGX_CONF_ERROR; | 338 return NGX_CONF_ERROR; |
327 } | 339 } |
328 | 340 |
329 map->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; | 341 map->map.hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; |
330 } | 342 } |
343 | |
344 #if (NGX_PCRE) | |
345 | |
346 if (ctx.regexes.nelts) { | |
347 map->map.regex = ctx.regexes.elts; | |
348 map->map.nregex = ctx.regexes.nelts; | |
349 } | |
350 | |
351 #endif | |
331 | 352 |
332 ngx_destroy_pool(pool); | 353 ngx_destroy_pool(pool); |
333 | 354 |
334 return rv; | 355 return rv; |
335 } | 356 } |
489 ctx->default_value = var; | 510 ctx->default_value = var; |
490 | 511 |
491 return NGX_CONF_OK; | 512 return NGX_CONF_OK; |
492 } | 513 } |
493 | 514 |
515 #if (NGX_PCRE) | |
516 | |
517 if (value[0].len && value[0].data[0] == '~') { | |
518 ngx_regex_compile_t rc; | |
519 ngx_http_map_regex_t *regex; | |
520 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
521 | |
522 regex = ngx_array_push(&ctx->regexes); | |
523 if (regex == NULL) { | |
524 return NGX_CONF_ERROR; | |
525 } | |
526 | |
527 value[0].len--; | |
528 value[0].data++; | |
529 | |
530 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); | |
531 | |
532 if (value[0].data[0] == '*') { | |
533 value[0].len--; | |
534 value[0].data++; | |
535 rc.options = NGX_REGEX_CASELESS; | |
536 } | |
537 | |
538 rc.pattern = value[0]; | |
539 rc.err.len = NGX_MAX_CONF_ERRSTR; | |
540 rc.err.data = errstr; | |
541 | |
542 regex->regex = ngx_http_regex_compile(ctx->cf, &rc); | |
543 if (regex->regex == NULL) { | |
544 return NGX_CONF_ERROR; | |
545 } | |
546 | |
547 regex->value = var; | |
548 | |
549 return NGX_CONF_OK; | |
550 } | |
551 | |
552 #endif | |
553 | |
494 if (value[0].len && value[0].data[0] == '\\') { | 554 if (value[0].len && value[0].data[0] == '\\') { |
495 value[0].len--; | 555 value[0].len--; |
496 value[0].data++; | 556 value[0].data++; |
497 } | 557 } |
498 | 558 |