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