Mercurial > hg > nginx
comparison src/stream/ngx_stream_map_module.c @ 6609:73543af69f14
Stream: map module.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Wed, 29 Jun 2016 12:46:12 +0300 |
parents | |
children | 8ed51b02f655 |
comparison
equal
deleted
inserted
replaced
6608:eb4293155e87 | 6609:73543af69f14 |
---|---|
1 | |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 typedef struct { | |
14 ngx_uint_t hash_max_size; | |
15 ngx_uint_t hash_bucket_size; | |
16 } ngx_stream_map_conf_t; | |
17 | |
18 | |
19 typedef struct { | |
20 ngx_hash_keys_arrays_t keys; | |
21 | |
22 ngx_array_t *values_hash; | |
23 #if (NGX_PCRE) | |
24 ngx_array_t regexes; | |
25 #endif | |
26 | |
27 ngx_stream_variable_value_t *default_value; | |
28 ngx_conf_t *cf; | |
29 ngx_uint_t hostnames; /* unsigned hostnames:1 */ | |
30 } ngx_stream_map_conf_ctx_t; | |
31 | |
32 | |
33 typedef struct { | |
34 ngx_stream_map_t map; | |
35 ngx_stream_complex_value_t value; | |
36 ngx_stream_variable_value_t *default_value; | |
37 ngx_uint_t hostnames; /* unsigned hostnames:1 */ | |
38 } ngx_stream_map_ctx_t; | |
39 | |
40 | |
41 static int ngx_libc_cdecl ngx_stream_map_cmp_dns_wildcards(const void *one, | |
42 const void *two); | |
43 static void *ngx_stream_map_create_conf(ngx_conf_t *cf); | |
44 static char *ngx_stream_map_block(ngx_conf_t *cf, ngx_command_t *cmd, | |
45 void *conf); | |
46 static char *ngx_stream_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf); | |
47 | |
48 | |
49 static ngx_command_t ngx_stream_map_commands[] = { | |
50 | |
51 { ngx_string("map"), | |
52 NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2, | |
53 ngx_stream_map_block, | |
54 NGX_STREAM_MAIN_CONF_OFFSET, | |
55 0, | |
56 NULL }, | |
57 | |
58 { ngx_string("map_hash_max_size"), | |
59 NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1, | |
60 ngx_conf_set_num_slot, | |
61 NGX_STREAM_MAIN_CONF_OFFSET, | |
62 offsetof(ngx_stream_map_conf_t, hash_max_size), | |
63 NULL }, | |
64 | |
65 { ngx_string("map_hash_bucket_size"), | |
66 NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1, | |
67 ngx_conf_set_num_slot, | |
68 NGX_STREAM_MAIN_CONF_OFFSET, | |
69 offsetof(ngx_stream_map_conf_t, hash_bucket_size), | |
70 NULL }, | |
71 | |
72 ngx_null_command | |
73 }; | |
74 | |
75 | |
76 static ngx_stream_module_t ngx_stream_map_module_ctx = { | |
77 NULL, /* preconfiguration */ | |
78 NULL, /* postconfiguration */ | |
79 | |
80 ngx_stream_map_create_conf, /* create main configuration */ | |
81 NULL, /* init main configuration */ | |
82 | |
83 NULL, /* create server configuration */ | |
84 NULL, /* merge server configuration */ | |
85 }; | |
86 | |
87 | |
88 ngx_module_t ngx_stream_map_module = { | |
89 NGX_MODULE_V1, | |
90 &ngx_stream_map_module_ctx, /* module context */ | |
91 ngx_stream_map_commands, /* module directives */ | |
92 NGX_STREAM_MODULE, /* module type */ | |
93 NULL, /* init master */ | |
94 NULL, /* init module */ | |
95 NULL, /* init process */ | |
96 NULL, /* init thread */ | |
97 NULL, /* exit thread */ | |
98 NULL, /* exit process */ | |
99 NULL, /* exit master */ | |
100 NGX_MODULE_V1_PADDING | |
101 }; | |
102 | |
103 | |
104 static ngx_int_t | |
105 ngx_stream_map_variable(ngx_stream_session_t *s, ngx_stream_variable_value_t *v, | |
106 uintptr_t data) | |
107 { | |
108 ngx_stream_map_ctx_t *map = (ngx_stream_map_ctx_t *) data; | |
109 | |
110 ngx_str_t val, str; | |
111 ngx_stream_complex_value_t *cv; | |
112 ngx_stream_variable_value_t *value; | |
113 | |
114 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
115 "stream map started"); | |
116 | |
117 if (ngx_stream_complex_value(s, &map->value, &val) != NGX_OK) { | |
118 return NGX_ERROR; | |
119 } | |
120 | |
121 if (map->hostnames && val.len > 0 && val.data[val.len - 1] == '.') { | |
122 val.len--; | |
123 } | |
124 | |
125 value = ngx_stream_map_find(s, &map->map, &val); | |
126 | |
127 if (value == NULL) { | |
128 value = map->default_value; | |
129 } | |
130 | |
131 if (!value->valid) { | |
132 cv = (ngx_stream_complex_value_t *) value->data; | |
133 | |
134 if (ngx_stream_complex_value(s, cv, &str) != NGX_OK) { | |
135 return NGX_ERROR; | |
136 } | |
137 | |
138 v->valid = 1; | |
139 v->no_cacheable = 0; | |
140 v->not_found = 0; | |
141 v->len = str.len; | |
142 v->data = str.data; | |
143 | |
144 } else { | |
145 *v = *value; | |
146 } | |
147 | |
148 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
149 "stream map: \"%V\" \"%v\"", &val, v); | |
150 | |
151 return NGX_OK; | |
152 } | |
153 | |
154 | |
155 static void * | |
156 ngx_stream_map_create_conf(ngx_conf_t *cf) | |
157 { | |
158 ngx_stream_map_conf_t *mcf; | |
159 | |
160 mcf = ngx_palloc(cf->pool, sizeof(ngx_stream_map_conf_t)); | |
161 if (mcf == NULL) { | |
162 return NULL; | |
163 } | |
164 | |
165 mcf->hash_max_size = NGX_CONF_UNSET_UINT; | |
166 mcf->hash_bucket_size = NGX_CONF_UNSET_UINT; | |
167 | |
168 return mcf; | |
169 } | |
170 | |
171 | |
172 static char * | |
173 ngx_stream_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
174 { | |
175 ngx_stream_map_conf_t *mcf = conf; | |
176 | |
177 char *rv; | |
178 ngx_str_t *value, name; | |
179 ngx_conf_t save; | |
180 ngx_pool_t *pool; | |
181 ngx_hash_init_t hash; | |
182 ngx_stream_map_ctx_t *map; | |
183 ngx_stream_variable_t *var; | |
184 ngx_stream_map_conf_ctx_t ctx; | |
185 ngx_stream_compile_complex_value_t ccv; | |
186 | |
187 if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) { | |
188 mcf->hash_max_size = 2048; | |
189 } | |
190 | |
191 if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) { | |
192 mcf->hash_bucket_size = ngx_cacheline_size; | |
193 | |
194 } else { | |
195 mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size, | |
196 ngx_cacheline_size); | |
197 } | |
198 | |
199 map = ngx_pcalloc(cf->pool, sizeof(ngx_stream_map_ctx_t)); | |
200 if (map == NULL) { | |
201 return NGX_CONF_ERROR; | |
202 } | |
203 | |
204 value = cf->args->elts; | |
205 | |
206 ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t)); | |
207 | |
208 ccv.cf = cf; | |
209 ccv.value = &value[1]; | |
210 ccv.complex_value = &map->value; | |
211 | |
212 if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) { | |
213 return NGX_CONF_ERROR; | |
214 } | |
215 | |
216 name = value[2]; | |
217 | |
218 if (name.data[0] != '$') { | |
219 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
220 "invalid variable name \"%V\"", &name); | |
221 return NGX_CONF_ERROR; | |
222 } | |
223 | |
224 name.len--; | |
225 name.data++; | |
226 | |
227 var = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE); | |
228 if (var == NULL) { | |
229 return NGX_CONF_ERROR; | |
230 } | |
231 | |
232 var->get_handler = ngx_stream_map_variable; | |
233 var->data = (uintptr_t) map; | |
234 | |
235 pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log); | |
236 if (pool == NULL) { | |
237 return NGX_CONF_ERROR; | |
238 } | |
239 | |
240 ctx.keys.pool = cf->pool; | |
241 ctx.keys.temp_pool = pool; | |
242 | |
243 if (ngx_hash_keys_array_init(&ctx.keys, NGX_HASH_LARGE) != NGX_OK) { | |
244 ngx_destroy_pool(pool); | |
245 return NGX_CONF_ERROR; | |
246 } | |
247 | |
248 ctx.values_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * ctx.keys.hsize); | |
249 if (ctx.values_hash == NULL) { | |
250 ngx_destroy_pool(pool); | |
251 return NGX_CONF_ERROR; | |
252 } | |
253 | |
254 #if (NGX_PCRE) | |
255 if (ngx_array_init(&ctx.regexes, cf->pool, 2, | |
256 sizeof(ngx_stream_map_regex_t)) | |
257 != NGX_OK) | |
258 { | |
259 ngx_destroy_pool(pool); | |
260 return NGX_CONF_ERROR; | |
261 } | |
262 #endif | |
263 | |
264 ctx.default_value = NULL; | |
265 ctx.cf = &save; | |
266 ctx.hostnames = 0; | |
267 | |
268 save = *cf; | |
269 cf->pool = pool; | |
270 cf->ctx = &ctx; | |
271 cf->handler = ngx_stream_map; | |
272 cf->handler_conf = conf; | |
273 | |
274 rv = ngx_conf_parse(cf, NULL); | |
275 | |
276 *cf = save; | |
277 | |
278 if (rv != NGX_CONF_OK) { | |
279 ngx_destroy_pool(pool); | |
280 return rv; | |
281 } | |
282 | |
283 map->default_value = ctx.default_value ? ctx.default_value: | |
284 &ngx_stream_variable_null_value; | |
285 | |
286 map->hostnames = ctx.hostnames; | |
287 | |
288 hash.key = ngx_hash_key_lc; | |
289 hash.max_size = mcf->hash_max_size; | |
290 hash.bucket_size = mcf->hash_bucket_size; | |
291 hash.name = "map_hash"; | |
292 hash.pool = cf->pool; | |
293 | |
294 if (ctx.keys.keys.nelts) { | |
295 hash.hash = &map->map.hash.hash; | |
296 hash.temp_pool = NULL; | |
297 | |
298 if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts) | |
299 != NGX_OK) | |
300 { | |
301 ngx_destroy_pool(pool); | |
302 return NGX_CONF_ERROR; | |
303 } | |
304 } | |
305 | |
306 if (ctx.keys.dns_wc_head.nelts) { | |
307 | |
308 ngx_qsort(ctx.keys.dns_wc_head.elts, | |
309 (size_t) ctx.keys.dns_wc_head.nelts, | |
310 sizeof(ngx_hash_key_t), ngx_stream_map_cmp_dns_wildcards); | |
311 | |
312 hash.hash = NULL; | |
313 hash.temp_pool = pool; | |
314 | |
315 if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_head.elts, | |
316 ctx.keys.dns_wc_head.nelts) | |
317 != NGX_OK) | |
318 { | |
319 ngx_destroy_pool(pool); | |
320 return NGX_CONF_ERROR; | |
321 } | |
322 | |
323 map->map.hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; | |
324 } | |
325 | |
326 if (ctx.keys.dns_wc_tail.nelts) { | |
327 | |
328 ngx_qsort(ctx.keys.dns_wc_tail.elts, | |
329 (size_t) ctx.keys.dns_wc_tail.nelts, | |
330 sizeof(ngx_hash_key_t), ngx_stream_map_cmp_dns_wildcards); | |
331 | |
332 hash.hash = NULL; | |
333 hash.temp_pool = pool; | |
334 | |
335 if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_tail.elts, | |
336 ctx.keys.dns_wc_tail.nelts) | |
337 != NGX_OK) | |
338 { | |
339 ngx_destroy_pool(pool); | |
340 return NGX_CONF_ERROR; | |
341 } | |
342 | |
343 map->map.hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; | |
344 } | |
345 | |
346 #if (NGX_PCRE) | |
347 | |
348 if (ctx.regexes.nelts) { | |
349 map->map.regex = ctx.regexes.elts; | |
350 map->map.nregex = ctx.regexes.nelts; | |
351 } | |
352 | |
353 #endif | |
354 | |
355 ngx_destroy_pool(pool); | |
356 | |
357 return rv; | |
358 } | |
359 | |
360 | |
361 static int ngx_libc_cdecl | |
362 ngx_stream_map_cmp_dns_wildcards(const void *one, const void *two) | |
363 { | |
364 ngx_hash_key_t *first, *second; | |
365 | |
366 first = (ngx_hash_key_t *) one; | |
367 second = (ngx_hash_key_t *) two; | |
368 | |
369 return ngx_dns_strcmp(first->key.data, second->key.data); | |
370 } | |
371 | |
372 | |
373 static char * | |
374 ngx_stream_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
375 { | |
376 u_char *data; | |
377 size_t len; | |
378 ngx_int_t rv; | |
379 ngx_str_t *value, v; | |
380 ngx_uint_t i, key; | |
381 ngx_stream_map_conf_ctx_t *ctx; | |
382 ngx_stream_complex_value_t cv, *cvp; | |
383 ngx_stream_variable_value_t *var, **vp; | |
384 ngx_stream_compile_complex_value_t ccv; | |
385 | |
386 ctx = cf->ctx; | |
387 | |
388 value = cf->args->elts; | |
389 | |
390 if (cf->args->nelts == 1 | |
391 && ngx_strcmp(value[0].data, "hostnames") == 0) | |
392 { | |
393 ctx->hostnames = 1; | |
394 return NGX_CONF_OK; | |
395 | |
396 } else if (cf->args->nelts != 2) { | |
397 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
398 "invalid number of the map parameters"); | |
399 return NGX_CONF_ERROR; | |
400 } | |
401 | |
402 if (ngx_strcmp(value[0].data, "include") == 0) { | |
403 return ngx_conf_include(cf, dummy, conf); | |
404 } | |
405 | |
406 key = 0; | |
407 | |
408 for (i = 0; i < value[1].len; i++) { | |
409 key = ngx_hash(key, value[1].data[i]); | |
410 } | |
411 | |
412 key %= ctx->keys.hsize; | |
413 | |
414 vp = ctx->values_hash[key].elts; | |
415 | |
416 if (vp) { | |
417 for (i = 0; i < ctx->values_hash[key].nelts; i++) { | |
418 | |
419 if (vp[i]->valid) { | |
420 data = vp[i]->data; | |
421 len = vp[i]->len; | |
422 | |
423 } else { | |
424 cvp = (ngx_stream_complex_value_t *) vp[i]->data; | |
425 data = cvp->value.data; | |
426 len = cvp->value.len; | |
427 } | |
428 | |
429 if (value[1].len != len) { | |
430 continue; | |
431 } | |
432 | |
433 if (ngx_strncmp(value[1].data, data, len) == 0) { | |
434 var = vp[i]; | |
435 goto found; | |
436 } | |
437 } | |
438 | |
439 } else { | |
440 if (ngx_array_init(&ctx->values_hash[key], cf->pool, 4, | |
441 sizeof(ngx_stream_variable_value_t *)) | |
442 != NGX_OK) | |
443 { | |
444 return NGX_CONF_ERROR; | |
445 } | |
446 } | |
447 | |
448 var = ngx_palloc(ctx->keys.pool, sizeof(ngx_stream_variable_value_t)); | |
449 if (var == NULL) { | |
450 return NGX_CONF_ERROR; | |
451 } | |
452 | |
453 v.len = value[1].len; | |
454 v.data = ngx_pstrdup(ctx->keys.pool, &value[1]); | |
455 if (v.data == NULL) { | |
456 return NGX_CONF_ERROR; | |
457 } | |
458 | |
459 ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t)); | |
460 | |
461 ccv.cf = ctx->cf; | |
462 ccv.value = &v; | |
463 ccv.complex_value = &cv; | |
464 | |
465 if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) { | |
466 return NGX_CONF_ERROR; | |
467 } | |
468 | |
469 if (cv.lengths != NULL) { | |
470 cvp = ngx_palloc(ctx->keys.pool, sizeof(ngx_stream_complex_value_t)); | |
471 if (cvp == NULL) { | |
472 return NGX_CONF_ERROR; | |
473 } | |
474 | |
475 *cvp = cv; | |
476 | |
477 var->len = 0; | |
478 var->data = (u_char *) cvp; | |
479 var->valid = 0; | |
480 | |
481 } else { | |
482 var->len = v.len; | |
483 var->data = v.data; | |
484 var->valid = 1; | |
485 } | |
486 | |
487 var->no_cacheable = 0; | |
488 var->not_found = 0; | |
489 | |
490 vp = ngx_array_push(&ctx->values_hash[key]); | |
491 if (vp == NULL) { | |
492 return NGX_CONF_ERROR; | |
493 } | |
494 | |
495 *vp = var; | |
496 | |
497 found: | |
498 | |
499 if (ngx_strcmp(value[0].data, "default") == 0) { | |
500 | |
501 if (ctx->default_value) { | |
502 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
503 "duplicate default map parameter"); | |
504 return NGX_CONF_ERROR; | |
505 } | |
506 | |
507 ctx->default_value = var; | |
508 | |
509 return NGX_CONF_OK; | |
510 } | |
511 | |
512 #if (NGX_PCRE) | |
513 | |
514 if (value[0].len && value[0].data[0] == '~') { | |
515 ngx_regex_compile_t rc; | |
516 ngx_stream_map_regex_t *regex; | |
517 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
518 | |
519 regex = ngx_array_push(&ctx->regexes); | |
520 if (regex == NULL) { | |
521 return NGX_CONF_ERROR; | |
522 } | |
523 | |
524 value[0].len--; | |
525 value[0].data++; | |
526 | |
527 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); | |
528 | |
529 if (value[0].data[0] == '*') { | |
530 value[0].len--; | |
531 value[0].data++; | |
532 rc.options = NGX_REGEX_CASELESS; | |
533 } | |
534 | |
535 rc.pattern = value[0]; | |
536 rc.err.len = NGX_MAX_CONF_ERRSTR; | |
537 rc.err.data = errstr; | |
538 | |
539 regex->regex = ngx_stream_regex_compile(ctx->cf, &rc); | |
540 if (regex->regex == NULL) { | |
541 return NGX_CONF_ERROR; | |
542 } | |
543 | |
544 regex->value = var; | |
545 | |
546 return NGX_CONF_OK; | |
547 } | |
548 | |
549 #endif | |
550 | |
551 if (value[0].len && value[0].data[0] == '\\') { | |
552 value[0].len--; | |
553 value[0].data++; | |
554 } | |
555 | |
556 rv = ngx_hash_add_key(&ctx->keys, &value[0], var, | |
557 (ctx->hostnames) ? NGX_HASH_WILDCARD_KEY : 0); | |
558 | |
559 if (rv == NGX_OK) { | |
560 return NGX_CONF_OK; | |
561 } | |
562 | |
563 if (rv == NGX_DECLINED) { | |
564 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
565 "invalid hostname or wildcard \"%V\"", &value[0]); | |
566 } | |
567 | |
568 if (rv == NGX_BUSY) { | |
569 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
570 "conflicting parameter \"%V\"", &value[0]); | |
571 } | |
572 | |
573 return NGX_CONF_ERROR; | |
574 } |