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