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