138
|
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 {
|
140
|
19 ngx_http_hash_conf_t hash;
|
138
|
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
|
|
118 len = vv->len;
|
|
119
|
|
120 if (len && map->hostnames && vv->data[len - 1] == '.') {
|
|
121 len--;
|
|
122 }
|
|
123
|
|
124 if (len == 0) {
|
|
125 *v = *map->default_value;
|
|
126 return NGX_OK;
|
|
127 }
|
|
128
|
|
129 name = ngx_palloc(r->pool, len);
|
|
130 if (name == NULL) {
|
|
131 return NGX_ERROR;
|
|
132 }
|
|
133
|
|
134 key = 0;
|
|
135 for (i = 0; i < len; i++) {
|
|
136 name[i] = ngx_tolower(vv->data[i]);
|
|
137 key = ngx_hash(key, name[i]);
|
|
138 }
|
|
139
|
|
140 value = NULL;
|
|
141
|
|
142 if (map->hash.buckets) {
|
|
143 value = ngx_hash_find(&map->hash, key, name, len);
|
|
144 }
|
|
145
|
|
146 if (value) {
|
|
147 *v = *value;
|
|
148
|
|
149 } else {
|
|
150 if (map->dns_wildcards && map->dns_wildcards->hash.buckets) {
|
|
151 value = ngx_hash_find_wildcard(map->dns_wildcards, name, len);
|
|
152 if (value) {
|
|
153 *v = *value;
|
|
154
|
|
155 } else {
|
|
156 *v = *map->default_value;
|
|
157 }
|
|
158
|
|
159 } else {
|
|
160 *v = *map->default_value;
|
|
161 }
|
|
162 }
|
|
163
|
|
164 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
165 "http map: \"%V\" \"%V\"", vv, v);
|
|
166
|
|
167 return NGX_OK;
|
|
168 }
|
|
169
|
|
170
|
|
171 static void *
|
|
172 ngx_http_map_create_conf(ngx_conf_t *cf)
|
|
173 {
|
|
174 ngx_http_map_conf_t *mcf;
|
|
175
|
|
176 mcf = ngx_palloc(cf->pool, sizeof(ngx_http_map_conf_t));
|
|
177 if (mcf == NULL) {
|
|
178 return NGX_CONF_ERROR;
|
|
179 }
|
|
180
|
|
181 mcf->hash_max_size = NGX_CONF_UNSET_UINT;
|
|
182 mcf->hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
183
|
|
184 return mcf;
|
|
185 }
|
|
186
|
|
187
|
|
188 static char *
|
|
189 ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
190 {
|
|
191 ngx_http_map_conf_t *mcf = conf;
|
|
192
|
|
193 char *rv;
|
|
194 ngx_str_t *value, name;
|
|
195 ngx_conf_t save;
|
|
196 ngx_pool_t *pool;
|
|
197 ngx_hash_init_t hash;
|
|
198 ngx_http_map_ctx_t *map;
|
|
199 ngx_http_variable_t *var;
|
|
200 ngx_http_map_conf_ctx_t ctx;
|
|
201
|
|
202 if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) {
|
|
203 mcf->hash_max_size = 2048;
|
|
204 }
|
|
205
|
|
206 if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) {
|
|
207 mcf->hash_bucket_size = ngx_cacheline_size;
|
|
208
|
|
209 } else {
|
|
210 mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size,
|
|
211 ngx_cacheline_size);
|
|
212 }
|
|
213
|
|
214 map = ngx_pcalloc(cf->pool, sizeof(ngx_http_map_ctx_t));
|
|
215 if (map == NULL) {
|
|
216 return NGX_CONF_ERROR;
|
|
217 }
|
|
218
|
|
219 value = cf->args->elts;
|
|
220
|
|
221 name = value[1];
|
|
222 name.len--;
|
|
223 name.data++;
|
|
224
|
|
225 map->index = ngx_http_get_variable_index(cf, &name);
|
|
226
|
|
227 if (map->index == NGX_ERROR) {
|
|
228 return NGX_CONF_ERROR;
|
|
229 }
|
|
230
|
|
231 name = value[2];
|
|
232 name.len--;
|
|
233 name.data++;
|
|
234
|
|
235 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGABLE);
|
|
236 if (var == NULL) {
|
|
237 return NGX_CONF_ERROR;
|
|
238 }
|
|
239
|
|
240 var->handler = ngx_http_map_variable;
|
|
241 var->data = (uintptr_t) map;
|
|
242
|
|
243 pool = ngx_create_pool(16384, cf->log);
|
|
244 if (pool == NULL) {
|
|
245 return NGX_CONF_ERROR;
|
|
246 }
|
|
247
|
140
|
248 if (ngx_array_init(&ctx.hash.keys, pool, 16384, sizeof(ngx_hash_key_t))
|
138
|
249 != NGX_OK)
|
|
250 {
|
|
251 ngx_destroy_pool(pool);
|
|
252 return NGX_CONF_ERROR;
|
|
253 }
|
|
254
|
140
|
255 if (ngx_array_init(&ctx.hash.dns_wildcards, pool, 16384,
|
|
256 sizeof(ngx_hash_key_t))
|
138
|
257 != NGX_OK)
|
|
258 {
|
|
259 ngx_destroy_pool(pool);
|
|
260 return NGX_CONF_ERROR;
|
|
261 }
|
|
262
|
140
|
263 ctx.hash.keys_hash = ngx_pcalloc(pool,
|
|
264 sizeof(ngx_array_t) * NGX_HTTP_CONFIG_HASH);
|
|
265 if (ctx.hash.keys_hash == NULL) {
|
138
|
266 ngx_destroy_pool(pool);
|
|
267 return NGX_CONF_ERROR;
|
|
268 }
|
|
269
|
140
|
270 ctx.hash.dns_hash = ngx_pcalloc(pool,
|
|
271 sizeof(ngx_array_t) * NGX_HTTP_CONFIG_HASH);
|
|
272 if (ctx.hash.dns_hash == NULL) {
|
138
|
273 ngx_destroy_pool(pool);
|
|
274 return NGX_CONF_ERROR;
|
|
275 }
|
|
276
|
|
277 ctx.values_hash = ngx_pcalloc(pool,
|
140
|
278 sizeof(ngx_array_t) * NGX_HTTP_CONFIG_HASH);
|
138
|
279 if (ctx.values_hash == NULL) {
|
|
280 ngx_destroy_pool(pool);
|
|
281 return NGX_CONF_ERROR;
|
|
282 }
|
|
283
|
140
|
284 ctx.hash.pool = cf->pool;
|
|
285 ctx.hash.temp_pool = pool;
|
138
|
286 ctx.default_value = NULL;
|
|
287 ctx.hostnames = 0;
|
|
288
|
|
289 save = *cf;
|
|
290 cf->pool = pool;
|
|
291 cf->ctx = &ctx;
|
|
292 cf->handler = ngx_http_map;
|
|
293 cf->handler_conf = conf;
|
|
294
|
|
295 rv = ngx_conf_parse(cf, NULL);
|
|
296
|
|
297 *cf = save;
|
|
298
|
|
299 if (rv != NGX_CONF_OK) {
|
|
300 ngx_destroy_pool(pool);
|
|
301
|
|
302 return rv;
|
|
303 }
|
|
304
|
|
305 hash.key = ngx_hash_key_lc;
|
|
306 hash.max_size = mcf->hash_max_size;
|
|
307 hash.bucket_size = mcf->hash_bucket_size;
|
|
308 hash.name = "map_hash";
|
|
309 hash.pool = cf->pool;
|
|
310
|
140
|
311 if (ctx.hash.keys.nelts) {
|
138
|
312 hash.hash = &map->hash;
|
|
313 hash.temp_pool = NULL;
|
|
314
|
140
|
315 if (ngx_hash_init(&hash, ctx.hash.keys.elts, ctx.hash.keys.nelts)
|
|
316 != NGX_OK)
|
|
317 {
|
138
|
318 return NGX_CONF_ERROR;
|
|
319 }
|
|
320 }
|
|
321
|
|
322 map->default_value = ctx.default_value ? ctx.default_value:
|
|
323 &ngx_http_variable_null_value;
|
|
324
|
140
|
325 if (ctx.hash.dns_wildcards.nelts) {
|
138
|
326
|
140
|
327 ngx_qsort(ctx.hash.dns_wildcards.elts,
|
|
328 (size_t) ctx.hash.dns_wildcards.nelts,
|
138
|
329 sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards);
|
|
330
|
|
331 hash.hash = NULL;
|
|
332 hash.temp_pool = pool;
|
|
333
|
140
|
334 if (ngx_hash_wildcard_init(&hash, ctx.hash.dns_wildcards.elts,
|
|
335 ctx.hash.dns_wildcards.nelts)
|
138
|
336 != NGX_OK)
|
|
337 {
|
|
338 return NGX_CONF_ERROR;
|
|
339 }
|
|
340
|
|
341 map->dns_wildcards = (ngx_hash_wildcard_t *) hash.hash;
|
|
342 }
|
|
343
|
|
344 ngx_destroy_pool(pool);
|
|
345
|
|
346 return rv;
|
|
347 }
|
|
348
|
|
349
|
|
350 static int ngx_libc_cdecl
|
|
351 ngx_http_map_cmp_dns_wildcards(const void *one, const void *two)
|
|
352 {
|
|
353 ngx_hash_key_t *first, *second;
|
|
354
|
|
355 first = (ngx_hash_key_t *) one;
|
|
356 second = (ngx_hash_key_t *) two;
|
|
357
|
|
358 return ngx_strcmp(first->key.data, second->key.data);
|
|
359 }
|
|
360
|
|
361
|
|
362 static char *
|
|
363 ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
|
364 {
|
140
|
365 u_char ch;
|
|
366 ngx_int_t rc;
|
|
367 ngx_str_t *value, file;
|
|
368 ngx_uint_t i, key, flags;
|
138
|
369 ngx_http_map_conf_ctx_t *ctx;
|
140
|
370 ngx_http_variable_value_t *var, **vp;
|
138
|
371
|
|
372 ctx = cf->ctx;
|
|
373
|
|
374 value = cf->args->elts;
|
|
375
|
|
376 if (cf->args->nelts == 1
|
|
377 && ngx_strcmp(value[0].data, "hostnames") == 0)
|
|
378 {
|
|
379 ctx->hostnames = 1;
|
|
380 return NGX_CONF_OK;
|
|
381
|
|
382 } else if (cf->args->nelts != 2) {
|
|
383 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
384 "invalid number of the map parameters");
|
|
385 return NGX_CONF_ERROR;
|
|
386
|
|
387 } else if (value[0].len == 0) {
|
|
388 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
389 "invalid first parameter");
|
|
390 return NGX_CONF_ERROR;
|
|
391 }
|
|
392
|
|
393 if (ngx_strcmp(value[0].data, "include") == 0) {
|
|
394 file = value[1];
|
|
395
|
|
396 if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR){
|
|
397 return NGX_CONF_ERROR;
|
|
398 }
|
|
399
|
|
400 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
401
|
|
402 return ngx_conf_parse(cf, &file);
|
|
403 }
|
|
404
|
|
405 key = 0;
|
|
406
|
|
407 for (i = 0; i < value[1].len; i++) {
|
|
408 key = ngx_hash(key, value[1].data[i]);
|
|
409 }
|
|
410
|
140
|
411 key %= NGX_HTTP_CONFIG_HASH;
|
138
|
412
|
|
413 vp = ctx->values_hash[key].elts;
|
|
414
|
|
415 if (vp) {
|
|
416 for (i = 0; i < ctx->values_hash[key].nelts; i++) {
|
|
417 if (value[1].len != (size_t) vp[i]->len) {
|
|
418 continue;
|
|
419 }
|
|
420
|
|
421 if (ngx_strncmp(value[1].data, vp[i]->data, value[1].len) == 0) {
|
|
422 var = vp[i];
|
|
423 goto found;
|
|
424 }
|
|
425 }
|
|
426
|
|
427 } else {
|
|
428 if (ngx_array_init(&ctx->values_hash[key], cf->pool, 4,
|
|
429 sizeof(ngx_http_variable_value_t *))
|
|
430 != NGX_OK)
|
|
431 {
|
|
432 return NGX_CONF_ERROR;
|
|
433 }
|
|
434 }
|
|
435
|
140
|
436 var = ngx_palloc(ctx->hash.pool, sizeof(ngx_http_variable_value_t));
|
138
|
437 if (var == NULL) {
|
|
438 return NGX_CONF_ERROR;
|
|
439 }
|
|
440
|
|
441 var->len = value[1].len;
|
140
|
442 var->data = ngx_pstrdup(ctx->hash.pool, &value[1]);
|
138
|
443 if (var->data == NULL) {
|
|
444 return NGX_CONF_ERROR;
|
|
445 }
|
|
446
|
|
447 var->valid = 1;
|
|
448 var->no_cachable = 0;
|
|
449 var->not_found = 0;
|
|
450
|
|
451 vp = ngx_array_push(&ctx->values_hash[key]);
|
|
452 if (vp == NULL) {
|
|
453 return NGX_CONF_ERROR;
|
|
454 }
|
|
455
|
|
456 *vp = var;
|
|
457
|
|
458 found:
|
|
459
|
140
|
460 ch = value[0].data[0];
|
138
|
461
|
140
|
462 if ((ch != '*' && ch != '.') || ctx->hostnames == 0) {
|
138
|
463
|
140
|
464 if (ngx_strcmp(value[0].data, "default") == 0) {
|
138
|
465
|
140
|
466 if (ctx->default_value) {
|
|
467 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
468 "duplicate default map parameter");
|
138
|
469 return NGX_CONF_ERROR;
|
|
470 }
|
|
471
|
140
|
472 ctx->default_value = var;
|
138
|
473
|
140
|
474 return NGX_CONF_OK;
|
|
475 }
|
138
|
476
|
140
|
477 if (value[0].len && ch == '!') {
|
|
478 value[0].len--;
|
|
479 value[0].data++;
|
|
480 }
|
138
|
481
|
140
|
482 flags = 0;
|
138
|
483
|
|
484 } else {
|
|
485
|
140
|
486 if ((ch == '*' && (value[0].len < 3 || value[0].data[1] != '.'))
|
|
487 || (ch == '.' && value[0].len < 2))
|
|
488 {
|
138
|
489 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
490 "invalid DNS wildcard \"%V\"", &value[0]);
|
140
|
491
|
138
|
492 return NGX_CONF_ERROR;
|
|
493 }
|
|
494
|
140
|
495 flags = NGX_HTTP_WILDCARD_HASH;
|
138
|
496 }
|
|
497
|
140
|
498 rc = ngx_http_config_add_hash(&ctx->hash, &value[0], var, flags);
|
138
|
499
|
140
|
500 if (rc == NGX_OK) {
|
|
501 return NGX_CONF_OK;
|
|
502 }
|
138
|
503
|
140
|
504 if (rc == NGX_BUSY) {
|
|
505 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
506 "conflicting parameter \"%V\"", &value[0]);
|
|
507 }
|
138
|
508
|
140
|
509 return NGX_CONF_ERROR;
|
138
|
510 }
|