Mercurial > hg > nginx
annotate src/http/modules/ngx_http_referer_module.c @ 1590:8f8e0a60685f
fix segfault
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 22 Oct 2007 10:15:27 +0000 |
parents | 4c43e25d11ea |
children | e584e946e198 |
rev | line source |
---|---|
577 | 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 | |
595 | 12 #define NGX_HTTP_REFERER_NO_URI_PART ((void *) 4) |
13 | |
1388 | 14 #if (NGX_PCRE) |
15 | |
16 typedef struct { | |
17 ngx_regex_t *regex; | |
18 ngx_str_t name; | |
19 } ngx_http_referer_regex_t; | |
20 | |
21 #else | |
22 | |
23 #define ngx_regex_t void | |
24 | |
25 #endif | |
26 | |
27 | |
577 | 28 typedef struct { |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
29 ngx_hash_combined_t hash; |
577 | 30 |
1388 | 31 #if (NGX_PCRE) |
32 ngx_array_t *regex; | |
33 #endif | |
34 | |
593 | 35 ngx_flag_t no_referer; |
36 ngx_flag_t blocked_referer; | |
577 | 37 |
593 | 38 ngx_hash_keys_arrays_t *keys; |
577 | 39 } ngx_http_referer_conf_t; |
40 | |
41 | |
42 static void * ngx_http_referer_create_conf(ngx_conf_t *cf); | |
43 static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, | |
44 void *child); | |
45 static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, | |
46 void *conf); | |
593 | 47 static char *ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys, |
595 | 48 ngx_str_t *value, ngx_str_t *uri); |
1388 | 49 static char *ngx_http_add_regex_referer(ngx_conf_t *cf, |
50 ngx_http_referer_conf_t *rlcf, ngx_str_t *name, ngx_regex_t *regex); | |
593 | 51 static int ngx_libc_cdecl ngx_http_cmp_referer_wildcards(const void *one, |
52 const void *two); | |
577 | 53 |
54 | |
55 static ngx_command_t ngx_http_referer_commands[] = { | |
56 | |
57 { ngx_string("valid_referers"), | |
58 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
59 ngx_http_valid_referers, | |
60 NGX_HTTP_LOC_CONF_OFFSET, | |
61 0, | |
62 NULL }, | |
63 | |
64 ngx_null_command | |
65 }; | |
66 | |
67 | |
68 static ngx_http_module_t ngx_http_referer_module_ctx = { | |
69 NULL, /* preconfiguration */ | |
70 NULL, /* postconfiguration */ | |
71 | |
72 NULL, /* create main configuration */ | |
73 NULL, /* init main configuration */ | |
74 | |
75 NULL, /* create server configuration */ | |
76 NULL, /* merge server configuration */ | |
77 | |
78 ngx_http_referer_create_conf, /* create location configuration */ | |
79 ngx_http_referer_merge_conf /* merge location configuration */ | |
80 }; | |
81 | |
82 | |
83 ngx_module_t ngx_http_referer_module = { | |
84 NGX_MODULE_V1, | |
85 &ngx_http_referer_module_ctx, /* module context */ | |
86 ngx_http_referer_commands, /* module directives */ | |
87 NGX_HTTP_MODULE, /* module type */ | |
88 NULL, /* init master */ | |
89 NULL, /* init module */ | |
90 NULL, /* init process */ | |
91 NULL, /* init thread */ | |
92 NULL, /* exit thread */ | |
93 NULL, /* exit process */ | |
94 NULL, /* exit master */ | |
95 NGX_MODULE_V1_PADDING | |
96 }; | |
97 | |
98 | |
99 static ngx_int_t | |
100 ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
101 uintptr_t data) | |
102 { | |
1388 | 103 u_char *p, *ref, *last; |
104 size_t len; | |
105 ngx_str_t *uri; | |
106 ngx_uint_t i, key; | |
107 ngx_http_referer_conf_t *rlcf; | |
108 u_char buf[256]; | |
109 #if (NGX_PCRE) | |
110 ngx_int_t n; | |
111 ngx_str_t referer; | |
112 ngx_http_referer_regex_t *regex; | |
113 #endif | |
577 | 114 |
593 | 115 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_referer_module); |
577 | 116 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
117 if (rlcf->hash.hash.buckets == NULL |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
118 && rlcf->hash.wc_head == NULL |
1388 | 119 && rlcf->hash.wc_tail == NULL |
120 #if (NGX_PCRE) | |
121 && rlcf->regex == NULL | |
122 #endif | |
123 ) | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
124 { |
595 | 125 goto valid; |
577 | 126 } |
127 | |
128 if (r->headers_in.referer == NULL) { | |
593 | 129 if (rlcf->no_referer) { |
595 | 130 goto valid; |
131 } | |
577 | 132 |
595 | 133 goto invalid; |
577 | 134 } |
135 | |
136 len = r->headers_in.referer->value.len; | |
137 ref = r->headers_in.referer->value.data; | |
138 | |
139 if (len < sizeof("http://i.ru") - 1 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
637
diff
changeset
|
140 || (ngx_strncasecmp(ref, (u_char *) "http://", 7) != 0)) |
577 | 141 { |
593 | 142 if (rlcf->blocked_referer) { |
595 | 143 goto valid; |
144 } | |
577 | 145 |
595 | 146 goto invalid; |
577 | 147 } |
148 | |
595 | 149 last = ref + len; |
577 | 150 ref += 7; |
595 | 151 i = 0; |
152 key = 0; | |
577 | 153 |
595 | 154 for (p = ref; p < last; p++) { |
593 | 155 if (*p == '/' || *p == ':') { |
156 break; | |
577 | 157 } |
595 | 158 |
159 buf[i] = ngx_tolower(*p); | |
160 key = ngx_hash(key, buf[i++]); | |
161 | |
162 if (i == 256) { | |
163 goto invalid; | |
164 } | |
593 | 165 } |
577 | 166 |
1388 | 167 uri = ngx_hash_find_combined(&rlcf->hash, key, buf, p - ref); |
577 | 168 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
169 if (uri) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
170 goto uri; |
577 | 171 } |
172 | |
1388 | 173 #if (NGX_PCRE) |
174 | |
175 if (rlcf->regex) { | |
176 | |
177 referer.len = len - 7; | |
178 referer.data = ref; | |
179 | |
180 regex = rlcf->regex->elts; | |
181 | |
182 for (i = 0; i < rlcf->regex->nelts; i++) { | |
183 n = ngx_regex_exec(regex[i].regex, &referer, NULL, 0); | |
184 | |
185 if (n == NGX_REGEX_NO_MATCHED) { | |
186 continue; | |
187 } | |
188 | |
189 if (n < 0) { | |
190 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
191 ngx_regex_exec_n | |
192 " failed: %d on \"%V\" using \"%V\"", | |
193 n, &referer, ®ex[i].name); | |
194 return NGX_ERROR; | |
195 } | |
196 | |
197 /* match */ | |
198 | |
199 goto valid; | |
200 } | |
201 } | |
202 | |
203 #endif | |
204 | |
595 | 205 invalid: |
206 | |
577 | 207 *v = ngx_http_variable_true_value; |
208 | |
209 return NGX_OK; | |
595 | 210 |
211 uri: | |
212 | |
213 for ( /* void */ ; p < last; p++) { | |
214 if (*p == '/') { | |
215 break; | |
216 } | |
217 } | |
218 | |
219 len = last - p; | |
220 | |
221 if (uri == NGX_HTTP_REFERER_NO_URI_PART) { | |
222 goto valid; | |
223 } | |
224 | |
225 if (len < uri->len || ngx_strncmp(uri->data, p, uri->len) != 0) { | |
226 goto invalid; | |
227 } | |
228 | |
229 valid: | |
230 | |
231 *v = ngx_http_variable_null_value; | |
232 | |
233 return NGX_OK; | |
577 | 234 } |
235 | |
236 | |
237 static void * | |
238 ngx_http_referer_create_conf(ngx_conf_t *cf) | |
239 { | |
240 ngx_http_referer_conf_t *conf; | |
241 | |
593 | 242 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_referer_conf_t)); |
577 | 243 if (conf == NULL) { |
244 return NGX_CONF_ERROR; | |
245 } | |
246 | |
247 conf->no_referer = NGX_CONF_UNSET; | |
248 conf->blocked_referer = NGX_CONF_UNSET; | |
249 | |
250 return conf; | |
251 } | |
252 | |
253 | |
254 static char * | |
255 ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
256 { | |
257 ngx_http_referer_conf_t *prev = parent; | |
258 ngx_http_referer_conf_t *conf = child; | |
259 | |
593 | 260 ngx_hash_init_t hash; |
261 | |
262 if (conf->keys == NULL) { | |
263 conf->hash = prev->hash; | |
264 | |
577 | 265 ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0); |
266 ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0); | |
593 | 267 |
268 return NGX_CONF_OK; | |
269 } | |
270 | |
603 | 271 if ((conf->no_referer == 1 || conf->blocked_referer == 1) |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
272 && conf->keys->keys.nelts == 0 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
273 && conf->keys->dns_wc_head.nelts == 0 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
274 && conf->keys->dns_wc_tail.nelts == 0) |
603 | 275 { |
276 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
277 "the \"none\" or \"blocked\" referers are specified " | |
278 "in the \"valid_referers\" directive " | |
279 "without any valid referer"); | |
280 return NGX_CONF_ERROR; | |
281 } | |
282 | |
593 | 283 hash.key = ngx_hash_key_lc; |
284 hash.max_size = 2048; /* TODO: referer_hash_max_size; */ | |
285 hash.bucket_size = 64; /* TODO: referer_hash_bucket_size; */ | |
286 hash.name = "referers_hash"; | |
287 hash.pool = cf->pool; | |
288 | |
289 if (conf->keys->keys.nelts) { | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
290 hash.hash = &conf->hash.hash; |
593 | 291 hash.temp_pool = NULL; |
292 | |
293 if (ngx_hash_init(&hash, conf->keys->keys.elts, conf->keys->keys.nelts) | |
294 != NGX_OK) | |
295 { | |
296 return NGX_CONF_ERROR; | |
297 } | |
298 } | |
299 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
300 if (conf->keys->dns_wc_head.nelts) { |
593 | 301 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
302 ngx_qsort(conf->keys->dns_wc_head.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
303 (size_t) conf->keys->dns_wc_head.nelts, |
593 | 304 sizeof(ngx_hash_key_t), |
305 ngx_http_cmp_referer_wildcards); | |
306 | |
307 hash.hash = NULL; | |
308 hash.temp_pool = cf->temp_pool; | |
309 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
310 if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_head.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
311 conf->keys->dns_wc_head.nelts) |
593 | 312 != NGX_OK) |
313 { | |
314 return NGX_CONF_ERROR; | |
315 } | |
316 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
317 conf->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
318 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
319 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
320 if (conf->keys->dns_wc_tail.nelts) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
321 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
322 ngx_qsort(conf->keys->dns_wc_tail.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
323 (size_t) conf->keys->dns_wc_tail.nelts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
324 sizeof(ngx_hash_key_t), |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
325 ngx_http_cmp_referer_wildcards); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
326 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
327 hash.hash = NULL; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
328 hash.temp_pool = cf->temp_pool; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
329 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
330 if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_tail.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
331 conf->keys->dns_wc_tail.nelts) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
332 != NGX_OK) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
333 { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
334 return NGX_CONF_ERROR; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
335 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
336 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
337 conf->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; |
577 | 338 } |
339 | |
340 if (conf->no_referer == NGX_CONF_UNSET) { | |
341 conf->no_referer = 0; | |
342 } | |
343 | |
344 if (conf->blocked_referer == NGX_CONF_UNSET) { | |
345 conf->blocked_referer = 0; | |
346 } | |
347 | |
611 | 348 conf->keys = NULL; |
349 | |
577 | 350 return NGX_CONF_OK; |
351 } | |
352 | |
353 | |
354 static char * | |
355 ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
356 { | |
593 | 357 ngx_http_referer_conf_t *rlcf = conf; |
577 | 358 |
593 | 359 u_char *p; |
595 | 360 ngx_str_t *value, uri, name; |
593 | 361 ngx_uint_t i, n; |
577 | 362 ngx_http_variable_t *var; |
363 ngx_http_server_name_t *sn; | |
364 ngx_http_core_srv_conf_t *cscf; | |
365 | |
366 name.len = sizeof("invalid_referer") - 1; | |
367 name.data = (u_char *) "invalid_referer"; | |
368 | |
583 | 369 var = ngx_http_add_variable(cf, &name, |
1565 | 370 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOHASH); |
577 | 371 if (var == NULL) { |
372 return NGX_CONF_ERROR; | |
373 } | |
374 | |
637 | 375 var->get_handler = ngx_http_referer_variable; |
577 | 376 |
593 | 377 if (rlcf->keys == NULL) { |
378 rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); | |
379 if (rlcf->keys == NULL) { | |
380 return NGX_CONF_ERROR; | |
381 } | |
577 | 382 |
593 | 383 rlcf->keys->pool = cf->pool; |
384 rlcf->keys->temp_pool = cf->pool; | |
385 | |
386 if (ngx_hash_keys_array_init(rlcf->keys, NGX_HASH_SMALL) != NGX_OK) { | |
577 | 387 return NGX_CONF_ERROR; |
388 } | |
389 } | |
390 | |
391 value = cf->args->elts; | |
392 | |
393 for (i = 1; i < cf->args->nelts; i++) { | |
394 if (value[i].len == 0) { | |
395 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
396 "invalid referer \"%V\"", &value[i]); | |
397 return NGX_CONF_ERROR; | |
398 } | |
399 | |
400 if (ngx_strcmp(value[i].data, "none") == 0) { | |
593 | 401 rlcf->no_referer = 1; |
577 | 402 continue; |
403 } | |
404 | |
405 if (ngx_strcmp(value[i].data, "blocked") == 0) { | |
593 | 406 rlcf->blocked_referer = 1; |
577 | 407 continue; |
408 } | |
409 | |
595 | 410 uri.len = 0; |
613 | 411 uri.data = NULL; |
595 | 412 |
577 | 413 if (ngx_strcmp(value[i].data, "server_names") == 0) { |
593 | 414 |
415 cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module); | |
577 | 416 |
593 | 417 sn = cscf->server_names.elts; |
418 for (n = 0; n < cscf->server_names.nelts; n++) { | |
1388 | 419 |
420 #if (NGX_PCRE) | |
421 if (sn[n].regex) { | |
422 | |
423 if (ngx_http_add_regex_referer(cf, rlcf, &sn[n].name, | |
424 sn[n].regex) | |
425 != NGX_OK) | |
426 { | |
427 return NGX_CONF_ERROR; | |
428 } | |
429 | |
430 continue; | |
431 } | |
432 #endif | |
433 | |
595 | 434 if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name, &uri) |
435 != NGX_OK) | |
436 { | |
593 | 437 return NGX_CONF_ERROR; |
438 } | |
439 } | |
577 | 440 |
441 continue; | |
442 } | |
443 | |
1388 | 444 if (value[i].data[0] == '~') { |
445 if (ngx_http_add_regex_referer(cf, rlcf, &value[i], NULL) != NGX_OK) | |
446 { | |
447 return NGX_CONF_ERROR; | |
448 } | |
449 | |
450 continue; | |
451 } | |
452 | |
1242 | 453 p = (u_char *) ngx_strchr(value[i].data, '/'); |
577 | 454 |
593 | 455 if (p) { |
595 | 456 uri.len = (value[i].data + value[i].len) - p; |
457 uri.data = p; | |
593 | 458 value[i].len = p - value[i].data; |
577 | 459 } |
460 | |
595 | 461 if (ngx_http_add_referer(cf, rlcf->keys, &value[i], &uri) != NGX_OK) { |
577 | 462 return NGX_CONF_ERROR; |
463 } | |
464 } | |
465 | |
466 return NGX_CONF_OK; | |
467 } | |
593 | 468 |
469 | |
470 static char * | |
471 ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys, | |
595 | 472 ngx_str_t *value, ngx_str_t *uri) |
593 | 473 { |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
474 ngx_int_t rc; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
475 ngx_str_t *u; |
593 | 476 |
595 | 477 if (uri->len == 0) { |
478 u = NGX_HTTP_REFERER_NO_URI_PART; | |
479 | |
480 } else { | |
481 u = ngx_palloc(cf->pool, sizeof(ngx_str_t)); | |
482 if (u == NULL) { | |
483 return NGX_CONF_ERROR; | |
484 } | |
485 | |
486 *u = *uri; | |
487 } | |
488 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
489 rc = ngx_hash_add_key(keys, value, u, NGX_HASH_WILDCARD_KEY); |
593 | 490 |
491 if (rc == NGX_OK) { | |
492 return NGX_CONF_OK; | |
493 } | |
494 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
495 if (rc == NGX_DECLINED) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
496 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
497 "invalid hostname or wildcard \"%V\"", value); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
498 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
499 |
593 | 500 if (rc == NGX_BUSY) { |
501 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
502 "conflicting parameter \"%V\"", value); | |
503 } | |
504 | |
505 return NGX_CONF_ERROR; | |
506 } | |
507 | |
508 | |
1388 | 509 static char * |
510 ngx_http_add_regex_referer(ngx_conf_t *cf, ngx_http_referer_conf_t *rlcf, | |
511 ngx_str_t *name, ngx_regex_t *regex) | |
512 { | |
513 #if (NGX_PCRE) | |
514 ngx_str_t err; | |
515 ngx_http_referer_regex_t *rr; | |
516 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
517 | |
518 if (rlcf->regex == NULL) { | |
519 rlcf->regex = ngx_array_create(cf->pool, 2, | |
520 sizeof(ngx_http_referer_regex_t)); | |
521 if (rlcf->regex == NULL) { | |
522 return NGX_CONF_ERROR; | |
523 } | |
524 } | |
525 | |
526 rr = ngx_array_push(rlcf->regex); | |
527 if (rr == NULL) { | |
528 return NGX_CONF_ERROR; | |
529 } | |
530 | |
531 if (regex) { | |
532 rr->regex = regex; | |
533 rr->name = *name; | |
534 | |
535 return NGX_CONF_OK; | |
536 } | |
537 | |
538 err.len = NGX_MAX_CONF_ERRSTR; | |
539 err.data = errstr; | |
540 | |
541 name->len--; | |
542 name->data++; | |
543 | |
544 rr->regex = ngx_regex_compile(name, NGX_REGEX_CASELESS, cf->pool, &err); | |
545 | |
546 if (rr->regex == NULL) { | |
547 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data); | |
548 return NGX_CONF_ERROR; | |
549 } | |
550 | |
551 rr->name = *name; | |
552 | |
553 return NGX_CONF_OK; | |
554 | |
555 #else | |
556 | |
557 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
558 "the using of the regex \"%V\" requires PCRE library", | |
559 name); | |
560 | |
561 return NGX_CONF_ERROR; | |
562 | |
563 #endif | |
564 } | |
565 | |
566 | |
593 | 567 static int ngx_libc_cdecl |
568 ngx_http_cmp_referer_wildcards(const void *one, const void *two) | |
569 { | |
570 ngx_hash_key_t *first, *second; | |
571 | |
572 first = (ngx_hash_key_t *) one; | |
573 second = (ngx_hash_key_t *) two; | |
574 | |
575 return ngx_strcmp(first->key.data, second->key.data); | |
576 } |