Mercurial > hg > nginx
annotate src/http/modules/ngx_http_map_module.c @ 3790:d8300807c4f3
allow empty value as "map" parameter
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 25 Nov 2010 15:24:29 +0000 |
parents | 19c931be8b98 |
children | 11701c4c0358 |
rev | line source |
---|---|
589 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
13 ngx_uint_t hash_max_size; | |
14 ngx_uint_t hash_bucket_size; | |
15 } ngx_http_map_conf_t; | |
16 | |
17 | |
18 typedef struct { | |
593 | 19 ngx_hash_keys_arrays_t keys; |
589 | 20 |
21 ngx_array_t *values_hash; | |
22 | |
23 ngx_http_variable_value_t *default_value; | |
24 ngx_uint_t hostnames; /* unsigned hostnames:1 */ | |
25 } ngx_http_map_conf_ctx_t; | |
26 | |
27 | |
28 typedef struct { | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
29 ngx_hash_combined_t hash; |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
30 ngx_http_complex_value_t value; |
589 | 31 ngx_http_variable_value_t *default_value; |
32 ngx_uint_t hostnames; /* unsigned hostnames:1 */ | |
33 } ngx_http_map_ctx_t; | |
34 | |
35 | |
36 static int ngx_libc_cdecl ngx_http_map_cmp_dns_wildcards(const void *one, | |
37 const void *two); | |
38 static void *ngx_http_map_create_conf(ngx_conf_t *cf); | |
39 static char *ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
40 static char *ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf); | |
41 | |
42 | |
43 static ngx_command_t ngx_http_map_commands[] = { | |
44 | |
45 { ngx_string("map"), | |
46 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2, | |
47 ngx_http_map_block, | |
48 NGX_HTTP_MAIN_CONF_OFFSET, | |
49 0, | |
50 NULL }, | |
51 | |
52 { ngx_string("map_hash_max_size"), | |
53 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
54 ngx_conf_set_num_slot, | |
55 NGX_HTTP_MAIN_CONF_OFFSET, | |
56 offsetof(ngx_http_map_conf_t, hash_max_size), | |
57 NULL }, | |
58 | |
59 { ngx_string("map_hash_bucket_size"), | |
60 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
61 ngx_conf_set_num_slot, | |
62 NGX_HTTP_MAIN_CONF_OFFSET, | |
63 offsetof(ngx_http_map_conf_t, hash_bucket_size), | |
64 NULL }, | |
65 | |
66 ngx_null_command | |
67 }; | |
68 | |
69 | |
70 static ngx_http_module_t ngx_http_map_module_ctx = { | |
71 NULL, /* preconfiguration */ | |
72 NULL, /* postconfiguration */ | |
73 | |
74 ngx_http_map_create_conf, /* create main configuration */ | |
75 NULL, /* init main configuration */ | |
76 | |
77 NULL, /* create server configuration */ | |
78 NULL, /* merge server configuration */ | |
79 | |
80 NULL, /* create location configuration */ | |
81 NULL /* merge location configuration */ | |
82 }; | |
83 | |
84 | |
85 ngx_module_t ngx_http_map_module = { | |
86 NGX_MODULE_V1, | |
87 &ngx_http_map_module_ctx, /* module context */ | |
88 ngx_http_map_commands, /* module directives */ | |
89 NGX_HTTP_MODULE, /* module type */ | |
90 NULL, /* init master */ | |
91 NULL, /* init module */ | |
92 NULL, /* init process */ | |
93 NULL, /* init thread */ | |
94 NULL, /* exit thread */ | |
95 NULL, /* exit process */ | |
96 NULL, /* exit master */ | |
97 NGX_MODULE_V1_PADDING | |
98 }; | |
99 | |
100 | |
101 static ngx_int_t | |
102 ngx_http_map_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
103 uintptr_t data) | |
104 { | |
105 ngx_http_map_ctx_t *map = (ngx_http_map_ctx_t *) data; | |
106 | |
107 size_t len; | |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
108 ngx_str_t val; |
2136 | 109 ngx_uint_t key; |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
110 ngx_http_variable_value_t *value; |
589 | 111 |
112 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
113 "http map started"); | |
114 | |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
115 if (ngx_http_complex_value(r, &map->value, &val) != NGX_OK) { |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
116 return NGX_ERROR; |
797
36f7b549f481
fix segfault if $server_addr failed
Igor Sysoev <igor@sysoev.ru>
parents:
637
diff
changeset
|
117 } |
36f7b549f481
fix segfault if $server_addr failed
Igor Sysoev <igor@sysoev.ru>
parents:
637
diff
changeset
|
118 |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
119 len = val.len; |
589 | 120 |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
121 if (len && map->hostnames && val.data[len - 1] == '.') { |
589 | 122 len--; |
123 } | |
124 | |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
125 key = ngx_hash_strlow(val.data, val.data, len); |
589 | 126 |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
127 value = ngx_hash_find_combined(&map->hash, key, val.data, len); |
589 | 128 |
129 if (value) { | |
130 *v = *value; | |
131 | |
132 } else { | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
133 *v = *map->default_value; |
589 | 134 } |
135 | |
136 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
137 "http map: \"%v\" \"%v\"", &val, v); |
589 | 138 |
139 return NGX_OK; | |
140 } | |
141 | |
142 | |
143 static void * | |
144 ngx_http_map_create_conf(ngx_conf_t *cf) | |
145 { | |
146 ngx_http_map_conf_t *mcf; | |
147 | |
148 mcf = ngx_palloc(cf->pool, sizeof(ngx_http_map_conf_t)); | |
149 if (mcf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
150 return NULL; |
589 | 151 } |
152 | |
153 mcf->hash_max_size = NGX_CONF_UNSET_UINT; | |
154 mcf->hash_bucket_size = NGX_CONF_UNSET_UINT; | |
155 | |
156 return mcf; | |
157 } | |
158 | |
159 | |
160 static char * | |
161 ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
162 { | |
163 ngx_http_map_conf_t *mcf = conf; | |
164 | |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
165 char *rv; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
166 ngx_str_t *value, name; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
167 ngx_conf_t save; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
168 ngx_pool_t *pool; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
169 ngx_hash_init_t hash; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
170 ngx_http_map_ctx_t *map; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
171 ngx_http_variable_t *var; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
172 ngx_http_map_conf_ctx_t ctx; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
173 ngx_http_compile_complex_value_t ccv; |
589 | 174 |
175 if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) { | |
176 mcf->hash_max_size = 2048; | |
177 } | |
178 | |
179 if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) { | |
180 mcf->hash_bucket_size = ngx_cacheline_size; | |
181 | |
182 } else { | |
183 mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size, | |
184 ngx_cacheline_size); | |
185 } | |
186 | |
187 map = ngx_pcalloc(cf->pool, sizeof(ngx_http_map_ctx_t)); | |
188 if (map == NULL) { | |
189 return NGX_CONF_ERROR; | |
190 } | |
191 | |
192 value = cf->args->elts; | |
193 | |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
194 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
589 | 195 |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
196 ccv.cf = cf; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
197 ccv.value = &value[1]; |
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
198 ccv.complex_value = &map->value; |
589 | 199 |
3789
19c931be8b98
allow expressions in the first "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
200 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
589 | 201 return NGX_CONF_ERROR; |
202 } | |
203 | |
204 name = value[2]; | |
205 name.len--; | |
206 name.data++; | |
207 | |
1565 | 208 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); |
589 | 209 if (var == NULL) { |
210 return NGX_CONF_ERROR; | |
211 } | |
212 | |
637 | 213 var->get_handler = ngx_http_map_variable; |
589 | 214 var->data = (uintptr_t) map; |
215 | |
216 pool = ngx_create_pool(16384, cf->log); | |
217 if (pool == NULL) { | |
218 return NGX_CONF_ERROR; | |
219 } | |
220 | |
593 | 221 ctx.keys.pool = cf->pool; |
222 ctx.keys.temp_pool = pool; | |
589 | 223 |
593 | 224 if (ngx_hash_keys_array_init(&ctx.keys, NGX_HASH_LARGE) != NGX_OK) { |
589 | 225 ngx_destroy_pool(pool); |
226 return NGX_CONF_ERROR; | |
227 } | |
228 | |
593 | 229 ctx.values_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * ctx.keys.hsize); |
589 | 230 if (ctx.values_hash == NULL) { |
231 ngx_destroy_pool(pool); | |
232 return NGX_CONF_ERROR; | |
233 } | |
234 | |
235 ctx.default_value = NULL; | |
236 ctx.hostnames = 0; | |
237 | |
238 save = *cf; | |
239 cf->pool = pool; | |
240 cf->ctx = &ctx; | |
241 cf->handler = ngx_http_map; | |
242 cf->handler_conf = conf; | |
243 | |
244 rv = ngx_conf_parse(cf, NULL); | |
245 | |
246 *cf = save; | |
247 | |
248 if (rv != NGX_CONF_OK) { | |
249 ngx_destroy_pool(pool); | |
250 return rv; | |
251 } | |
252 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
253 map->default_value = ctx.default_value ? ctx.default_value: |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
254 &ngx_http_variable_null_value; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
255 |
589 | 256 hash.key = ngx_hash_key_lc; |
257 hash.max_size = mcf->hash_max_size; | |
258 hash.bucket_size = mcf->hash_bucket_size; | |
259 hash.name = "map_hash"; | |
260 hash.pool = cf->pool; | |
261 | |
593 | 262 if (ctx.keys.keys.nelts) { |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
263 hash.hash = &map->hash.hash; |
589 | 264 hash.temp_pool = NULL; |
265 | |
593 | 266 if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts) |
591 | 267 != NGX_OK) |
268 { | |
593 | 269 ngx_destroy_pool(pool); |
589 | 270 return NGX_CONF_ERROR; |
271 } | |
272 } | |
273 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
274 if (ctx.keys.dns_wc_head.nelts) { |
589 | 275 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
276 ngx_qsort(ctx.keys.dns_wc_head.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
277 (size_t) ctx.keys.dns_wc_head.nelts, |
589 | 278 sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards); |
279 | |
280 hash.hash = NULL; | |
281 hash.temp_pool = pool; | |
282 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
283 if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_head.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
284 ctx.keys.dns_wc_head.nelts) |
589 | 285 != NGX_OK) |
286 { | |
593 | 287 ngx_destroy_pool(pool); |
589 | 288 return NGX_CONF_ERROR; |
289 } | |
290 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
291 map->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
292 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
293 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
294 if (ctx.keys.dns_wc_tail.nelts) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
295 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
296 ngx_qsort(ctx.keys.dns_wc_tail.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
297 (size_t) ctx.keys.dns_wc_tail.nelts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
298 sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
299 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
300 hash.hash = NULL; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
301 hash.temp_pool = pool; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
302 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
303 if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_tail.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
304 ctx.keys.dns_wc_tail.nelts) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
305 != NGX_OK) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
306 { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
307 ngx_destroy_pool(pool); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
308 return NGX_CONF_ERROR; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
309 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
310 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
311 map->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; |
589 | 312 } |
313 | |
314 ngx_destroy_pool(pool); | |
315 | |
316 return rv; | |
317 } | |
318 | |
319 | |
320 static int ngx_libc_cdecl | |
321 ngx_http_map_cmp_dns_wildcards(const void *one, const void *two) | |
322 { | |
323 ngx_hash_key_t *first, *second; | |
324 | |
325 first = (ngx_hash_key_t *) one; | |
326 second = (ngx_hash_key_t *) two; | |
327 | |
3116
98e288c6dac3
If .domain.com, .sub.domain.com, and .domain-some.com were defined,
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
328 return ngx_dns_strcmp(first->key.data, second->key.data); |
589 | 329 } |
330 | |
331 | |
332 static char * | |
333 ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
334 { | |
591 | 335 ngx_int_t rc; |
336 ngx_str_t *value, file; | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
337 ngx_uint_t i, key; |
589 | 338 ngx_http_map_conf_ctx_t *ctx; |
591 | 339 ngx_http_variable_value_t *var, **vp; |
589 | 340 |
341 ctx = cf->ctx; | |
342 | |
343 value = cf->args->elts; | |
344 | |
345 if (cf->args->nelts == 1 | |
346 && ngx_strcmp(value[0].data, "hostnames") == 0) | |
347 { | |
348 ctx->hostnames = 1; | |
349 return NGX_CONF_OK; | |
350 | |
351 } else if (cf->args->nelts != 2) { | |
352 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
353 "invalid number of the map parameters"); | |
354 return NGX_CONF_ERROR; | |
355 } | |
356 | |
357 if (ngx_strcmp(value[0].data, "include") == 0) { | |
358 file = value[1]; | |
359 | |
3642 | 360 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) { |
589 | 361 return NGX_CONF_ERROR; |
362 } | |
363 | |
364 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
365 | |
366 return ngx_conf_parse(cf, &file); | |
367 } | |
368 | |
369 key = 0; | |
370 | |
371 for (i = 0; i < value[1].len; i++) { | |
372 key = ngx_hash(key, value[1].data[i]); | |
373 } | |
374 | |
593 | 375 key %= ctx->keys.hsize; |
589 | 376 |
377 vp = ctx->values_hash[key].elts; | |
378 | |
379 if (vp) { | |
380 for (i = 0; i < ctx->values_hash[key].nelts; i++) { | |
381 if (value[1].len != (size_t) vp[i]->len) { | |
382 continue; | |
383 } | |
384 | |
385 if (ngx_strncmp(value[1].data, vp[i]->data, value[1].len) == 0) { | |
386 var = vp[i]; | |
387 goto found; | |
388 } | |
389 } | |
390 | |
391 } else { | |
392 if (ngx_array_init(&ctx->values_hash[key], cf->pool, 4, | |
393 sizeof(ngx_http_variable_value_t *)) | |
394 != NGX_OK) | |
395 { | |
396 return NGX_CONF_ERROR; | |
397 } | |
398 } | |
399 | |
593 | 400 var = ngx_palloc(ctx->keys.pool, sizeof(ngx_http_variable_value_t)); |
589 | 401 if (var == NULL) { |
402 return NGX_CONF_ERROR; | |
403 } | |
404 | |
405 var->len = value[1].len; | |
593 | 406 var->data = ngx_pstrdup(ctx->keys.pool, &value[1]); |
589 | 407 if (var->data == NULL) { |
408 return NGX_CONF_ERROR; | |
409 } | |
410 | |
411 var->valid = 1; | |
1565 | 412 var->no_cacheable = 0; |
589 | 413 var->not_found = 0; |
414 | |
415 vp = ngx_array_push(&ctx->values_hash[key]); | |
416 if (vp == NULL) { | |
417 return NGX_CONF_ERROR; | |
418 } | |
419 | |
420 *vp = var; | |
421 | |
422 found: | |
423 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
424 if (ngx_strcmp(value[0].data, "default") == 0) { |
589 | 425 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
426 if (ctx->default_value) { |
589 | 427 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
428 "duplicate default map parameter"); |
589 | 429 return NGX_CONF_ERROR; |
430 } | |
431 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
432 ctx->default_value = var; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
433 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
434 return NGX_CONF_OK; |
589 | 435 } |
436 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
437 if (value[0].len && value[0].data[0] == '!') { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
438 value[0].len--; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
439 value[0].data++; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
440 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
441 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
442 rc = ngx_hash_add_key(&ctx->keys, &value[0], var, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
443 (ctx->hostnames) ? NGX_HASH_WILDCARD_KEY : 0); |
589 | 444 |
591 | 445 if (rc == NGX_OK) { |
446 return NGX_CONF_OK; | |
447 } | |
589 | 448 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
449 if (rc == NGX_DECLINED) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
450 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
451 "invalid hostname or wildcard \"%V\"", &value[0]); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
452 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
453 |
591 | 454 if (rc == NGX_BUSY) { |
455 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
456 "conflicting parameter \"%V\"", &value[0]); | |
457 } | |
589 | 458 |
591 | 459 return NGX_CONF_ERROR; |
589 | 460 } |