2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
#define NGX_HTTP_REFERER_NO_URI_PART ((void *) 4)
|
|
|
|
|
2007-12-27 21:15:43 +08:00
|
|
|
#if !(NGX_PCRE)
|
2007-08-13 04:06:43 +08:00
|
|
|
|
|
|
|
#define ngx_regex_t void
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
typedef struct {
|
2007-06-12 03:49:22 +08:00
|
|
|
ngx_hash_combined_t hash;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2007-08-13 04:06:43 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
ngx_array_t *regex;
|
|
|
|
#endif
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_flag_t no_referer;
|
|
|
|
ngx_flag_t blocked_referer;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_hash_keys_arrays_t *keys;
|
2005-11-15 21:30:52 +08:00
|
|
|
} ngx_http_referer_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
static void * ngx_http_referer_create_conf(ngx_conf_t *cf);
|
|
|
|
static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent,
|
|
|
|
void *child);
|
|
|
|
static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
2005-12-27 01:07:48 +08:00
|
|
|
static char *ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys,
|
2005-12-28 22:23:52 +08:00
|
|
|
ngx_str_t *value, ngx_str_t *uri);
|
2007-08-13 04:06:43 +08:00
|
|
|
static char *ngx_http_add_regex_referer(ngx_conf_t *cf,
|
|
|
|
ngx_http_referer_conf_t *rlcf, ngx_str_t *name, ngx_regex_t *regex);
|
2005-12-27 01:07:48 +08:00
|
|
|
static int ngx_libc_cdecl ngx_http_cmp_referer_wildcards(const void *one,
|
|
|
|
const void *two);
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_referer_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("valid_referers"),
|
|
|
|
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
|
|
ngx_http_valid_referers,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_referer_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
NULL, /* postconfiguration */
|
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
ngx_http_referer_create_conf, /* create location configuration */
|
|
|
|
ngx_http_referer_merge_conf /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_referer_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_http_referer_module_ctx, /* module context */
|
|
|
|
ngx_http_referer_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
|
|
|
NULL, /* init master */
|
|
|
|
NULL, /* init module */
|
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
|
|
uintptr_t data)
|
|
|
|
{
|
2007-08-13 04:06:43 +08:00
|
|
|
u_char *p, *ref, *last;
|
|
|
|
size_t len;
|
|
|
|
ngx_str_t *uri;
|
|
|
|
ngx_uint_t i, key;
|
|
|
|
ngx_http_referer_conf_t *rlcf;
|
|
|
|
u_char buf[256];
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_referer_module);
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (rlcf->hash.hash.buckets == NULL
|
|
|
|
&& rlcf->hash.wc_head == NULL
|
2007-08-13 04:06:43 +08:00
|
|
|
&& rlcf->hash.wc_tail == NULL
|
|
|
|
#if (NGX_PCRE)
|
|
|
|
&& rlcf->regex == NULL
|
|
|
|
#endif
|
|
|
|
)
|
2007-06-12 03:49:22 +08:00
|
|
|
{
|
2005-12-28 22:23:52 +08:00
|
|
|
goto valid;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r->headers_in.referer == NULL) {
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->no_referer) {
|
2005-12-28 22:23:52 +08:00
|
|
|
goto valid;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
2005-12-28 22:23:52 +08:00
|
|
|
|
|
|
|
goto invalid;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
len = r->headers_in.referer->value.len;
|
|
|
|
ref = r->headers_in.referer->value.data;
|
|
|
|
|
2010-03-03 18:21:12 +08:00
|
|
|
if (len >= sizeof("http://i.ru") - 1) {
|
|
|
|
last = ref + len;
|
|
|
|
|
|
|
|
if (ngx_strncasecmp(ref, (u_char *) "http://", 7) == 0) {
|
|
|
|
ref += 7;
|
|
|
|
goto valid_scheme;
|
|
|
|
|
|
|
|
} else if (ngx_strncasecmp(ref, (u_char *) "https://", 8) == 0) {
|
|
|
|
ref += 8;
|
|
|
|
goto valid_scheme;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
2010-03-03 18:21:12 +08:00
|
|
|
}
|
2005-12-28 22:23:52 +08:00
|
|
|
|
2010-03-03 18:21:12 +08:00
|
|
|
if (rlcf->blocked_referer) {
|
|
|
|
goto valid;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 18:21:12 +08:00
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
valid_scheme:
|
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
i = 0;
|
|
|
|
key = 0;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
for (p = ref; p < last; p++) {
|
2005-12-27 01:07:48 +08:00
|
|
|
if (*p == '/' || *p == ':') {
|
|
|
|
break;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
2005-12-28 22:23:52 +08:00
|
|
|
|
|
|
|
buf[i] = ngx_tolower(*p);
|
|
|
|
key = ngx_hash(key, buf[i++]);
|
|
|
|
|
|
|
|
if (i == 256) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
2005-12-27 01:07:48 +08:00
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2007-08-13 04:06:43 +08:00
|
|
|
uri = ngx_hash_find_combined(&rlcf->hash, key, buf, p - ref);
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (uri) {
|
|
|
|
goto uri;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
2007-08-13 04:06:43 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
|
|
|
|
if (rlcf->regex) {
|
2007-12-27 21:15:43 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t referer;
|
2007-08-13 04:06:43 +08:00
|
|
|
|
|
|
|
referer.len = len - 7;
|
|
|
|
referer.data = ref;
|
|
|
|
|
2007-12-27 21:15:43 +08:00
|
|
|
rc = ngx_regex_exec_array(rlcf->regex, &referer, r->connection->log);
|
2007-08-13 04:06:43 +08:00
|
|
|
|
2007-12-27 21:15:43 +08:00
|
|
|
if (rc == NGX_OK) {
|
2007-08-13 04:06:43 +08:00
|
|
|
goto valid;
|
|
|
|
}
|
2007-12-27 21:15:43 +08:00
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NGX_DECLINED */
|
2007-08-13 04:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
invalid:
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
*v = ngx_http_variable_true_value;
|
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
return NGX_OK;
|
|
|
|
|
|
|
|
uri:
|
|
|
|
|
|
|
|
for ( /* void */ ; p < last; p++) {
|
|
|
|
if (*p == '/') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = last - p;
|
|
|
|
|
|
|
|
if (uri == NGX_HTTP_REFERER_NO_URI_PART) {
|
|
|
|
goto valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < uri->len || ngx_strncmp(uri->data, p, uri->len) != 0) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid:
|
|
|
|
|
|
|
|
*v = ngx_http_variable_null_value;
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_http_referer_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_referer_conf_t *conf;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_referer_conf_t));
|
2005-11-15 21:30:52 +08:00
|
|
|
if (conf == NULL) {
|
2009-06-03 00:09:44 +08:00
|
|
|
return NULL;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
2007-12-27 20:13:11 +08:00
|
|
|
#if (NGX_PCRE)
|
|
|
|
conf->regex = NGX_CONF_UNSET_PTR;
|
|
|
|
#endif
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
conf->no_referer = NGX_CONF_UNSET;
|
|
|
|
conf->blocked_referer = NGX_CONF_UNSET;
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_http_referer_conf_t *prev = parent;
|
|
|
|
ngx_http_referer_conf_t *conf = child;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_hash_init_t hash;
|
|
|
|
|
|
|
|
if (conf->keys == NULL) {
|
|
|
|
conf->hash = prev->hash;
|
|
|
|
|
2007-12-28 21:15:11 +08:00
|
|
|
#if (NGX_PCRE)
|
2007-12-27 20:13:11 +08:00
|
|
|
ngx_conf_merge_ptr_value(conf->regex, prev->regex, NULL);
|
2007-12-28 21:15:11 +08:00
|
|
|
#endif
|
2005-11-15 21:30:52 +08:00
|
|
|
ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0);
|
|
|
|
ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0);
|
2005-12-27 01:07:48 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
2006-01-25 00:08:27 +08:00
|
|
|
if ((conf->no_referer == 1 || conf->blocked_referer == 1)
|
2007-06-12 03:49:22 +08:00
|
|
|
&& conf->keys->keys.nelts == 0
|
|
|
|
&& conf->keys->dns_wc_head.nelts == 0
|
|
|
|
&& conf->keys->dns_wc_tail.nelts == 0)
|
2006-01-25 00:08:27 +08:00
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"the \"none\" or \"blocked\" referers are specified "
|
|
|
|
"in the \"valid_referers\" directive "
|
|
|
|
"without any valid referer");
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
hash.key = ngx_hash_key_lc;
|
|
|
|
hash.max_size = 2048; /* TODO: referer_hash_max_size; */
|
|
|
|
hash.bucket_size = 64; /* TODO: referer_hash_bucket_size; */
|
|
|
|
hash.name = "referers_hash";
|
|
|
|
hash.pool = cf->pool;
|
|
|
|
|
|
|
|
if (conf->keys->keys.nelts) {
|
2007-06-12 03:49:22 +08:00
|
|
|
hash.hash = &conf->hash.hash;
|
2005-12-27 01:07:48 +08:00
|
|
|
hash.temp_pool = NULL;
|
|
|
|
|
|
|
|
if (ngx_hash_init(&hash, conf->keys->keys.elts, conf->keys->keys.nelts)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (conf->keys->dns_wc_head.nelts) {
|
2005-12-27 01:07:48 +08:00
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
ngx_qsort(conf->keys->dns_wc_head.elts,
|
|
|
|
(size_t) conf->keys->dns_wc_head.nelts,
|
2005-12-27 01:07:48 +08:00
|
|
|
sizeof(ngx_hash_key_t),
|
|
|
|
ngx_http_cmp_referer_wildcards);
|
|
|
|
|
|
|
|
hash.hash = NULL;
|
|
|
|
hash.temp_pool = cf->temp_pool;
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_head.elts,
|
|
|
|
conf->keys->dns_wc_head.nelts)
|
2005-12-27 01:07:48 +08:00
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
conf->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf->keys->dns_wc_tail.nelts) {
|
|
|
|
|
|
|
|
ngx_qsort(conf->keys->dns_wc_tail.elts,
|
|
|
|
(size_t) conf->keys->dns_wc_tail.nelts,
|
|
|
|
sizeof(ngx_hash_key_t),
|
|
|
|
ngx_http_cmp_referer_wildcards);
|
|
|
|
|
|
|
|
hash.hash = NULL;
|
|
|
|
hash.temp_pool = cf->temp_pool;
|
|
|
|
|
|
|
|
if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_tail.elts,
|
|
|
|
conf->keys->dns_wc_tail.nelts)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
conf->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
2007-12-28 21:15:11 +08:00
|
|
|
#if (NGX_PCRE)
|
2007-12-27 20:13:11 +08:00
|
|
|
ngx_conf_merge_ptr_value(conf->regex, prev->regex, NULL);
|
2007-12-28 21:15:11 +08:00
|
|
|
#endif
|
2007-12-27 20:13:11 +08:00
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
if (conf->no_referer == NGX_CONF_UNSET) {
|
|
|
|
conf->no_referer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf->blocked_referer == NGX_CONF_UNSET) {
|
|
|
|
conf->blocked_referer = 0;
|
|
|
|
}
|
|
|
|
|
2006-02-08 23:33:12 +08:00
|
|
|
conf->keys = NULL;
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_http_referer_conf_t *rlcf = conf;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
u_char *p;
|
2005-12-28 22:23:52 +08:00
|
|
|
ngx_str_t *value, uri, name;
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_uint_t i, n;
|
2005-11-15 21:30:52 +08:00
|
|
|
ngx_http_variable_t *var;
|
|
|
|
ngx_http_server_name_t *sn;
|
|
|
|
ngx_http_core_srv_conf_t *cscf;
|
|
|
|
|
2010-05-14 17:56:37 +08:00
|
|
|
ngx_str_set(&name, "invalid_referer");
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
var = ngx_http_add_variable(cf, &name,
|
2007-10-15 02:56:15 +08:00
|
|
|
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOHASH);
|
2005-11-15 21:30:52 +08:00
|
|
|
if (var == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-04-19 23:30:56 +08:00
|
|
|
var->get_handler = ngx_http_referer_variable;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->keys == NULL) {
|
|
|
|
rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t));
|
|
|
|
if (rlcf->keys == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rlcf->keys->pool = cf->pool;
|
|
|
|
rlcf->keys->temp_pool = cf->pool;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (ngx_hash_keys_array_init(rlcf->keys, NGX_HASH_SMALL) != NGX_OK) {
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
for (i = 1; i < cf->args->nelts; i++) {
|
|
|
|
if (value[i].len == 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid referer \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[i].data, "none") == 0) {
|
2005-12-27 01:07:48 +08:00
|
|
|
rlcf->no_referer = 1;
|
2005-11-15 21:30:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[i].data, "blocked") == 0) {
|
2005-12-27 01:07:48 +08:00
|
|
|
rlcf->blocked_referer = 1;
|
2005-11-15 21:30:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-14 17:56:37 +08:00
|
|
|
ngx_str_null(&uri);
|
2005-12-28 22:23:52 +08:00
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
if (ngx_strcmp(value[i].data, "server_names") == 0) {
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
|
|
|
|
|
|
|
|
sn = cscf->server_names.elts;
|
|
|
|
for (n = 0; n < cscf->server_names.nelts; n++) {
|
2007-08-13 04:06:43 +08:00
|
|
|
|
|
|
|
#if (NGX_PCRE)
|
|
|
|
if (sn[n].regex) {
|
|
|
|
|
|
|
|
if (ngx_http_add_regex_referer(cf, rlcf, &sn[n].name,
|
2009-11-16 20:19:02 +08:00
|
|
|
sn[n].regex->regex)
|
2007-08-13 04:06:43 +08:00
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name, &uri)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-08-13 04:06:43 +08:00
|
|
|
if (value[i].data[0] == '~') {
|
|
|
|
if (ngx_http_add_regex_referer(cf, rlcf, &value[i], NULL) != NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-06 20:24:10 +08:00
|
|
|
p = (u_char *) ngx_strchr(value[i].data, '/');
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (p) {
|
2005-12-28 22:23:52 +08:00
|
|
|
uri.len = (value[i].data + value[i].len) - p;
|
|
|
|
uri.data = p;
|
2005-12-27 01:07:48 +08:00
|
|
|
value[i].len = p - value[i].data;
|
|
|
|
}
|
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
if (ngx_http_add_referer(cf, rlcf->keys, &value[i], &uri) != NGX_OK) {
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-12-27 01:07:48 +08:00
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys,
|
2005-12-28 22:23:52 +08:00
|
|
|
ngx_str_t *value, ngx_str_t *uri)
|
2005-12-27 01:07:48 +08:00
|
|
|
{
|
2007-06-12 03:49:22 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t *u;
|
2005-12-27 01:07:48 +08:00
|
|
|
|
2005-12-28 22:23:52 +08:00
|
|
|
if (uri->len == 0) {
|
|
|
|
u = NGX_HTTP_REFERER_NO_URI_PART;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
u = ngx_palloc(cf->pool, sizeof(ngx_str_t));
|
|
|
|
if (u == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*u = *uri;
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
rc = ngx_hash_add_key(keys, value, u, NGX_HASH_WILDCARD_KEY);
|
2005-12-27 01:07:48 +08:00
|
|
|
|
|
|
|
if (rc == NGX_OK) {
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-12 03:49:22 +08:00
|
|
|
if (rc == NGX_DECLINED) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid hostname or wildcard \"%V\"", value);
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rc == NGX_BUSY) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"conflicting parameter \"%V\"", value);
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
|
2007-08-13 04:06:43 +08:00
|
|
|
static char *
|
|
|
|
ngx_http_add_regex_referer(ngx_conf_t *cf, ngx_http_referer_conf_t *rlcf,
|
|
|
|
ngx_str_t *name, ngx_regex_t *regex)
|
|
|
|
{
|
|
|
|
#if (NGX_PCRE)
|
2009-11-16 20:19:02 +08:00
|
|
|
ngx_regex_elt_t *re;
|
|
|
|
ngx_regex_compile_t rc;
|
|
|
|
u_char errstr[NGX_MAX_CONF_ERRSTR];
|
2007-08-13 04:06:43 +08:00
|
|
|
|
2009-09-14 17:48:48 +08:00
|
|
|
if (name->len == 1) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty regex in \"%V\"", name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-12-27 20:13:11 +08:00
|
|
|
if (rlcf->regex == NGX_CONF_UNSET_PTR) {
|
2007-12-27 21:15:43 +08:00
|
|
|
rlcf->regex = ngx_array_create(cf->pool, 2, sizeof(ngx_regex_elt_t));
|
2007-08-13 04:06:43 +08:00
|
|
|
if (rlcf->regex == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-27 21:15:43 +08:00
|
|
|
re = ngx_array_push(rlcf->regex);
|
|
|
|
if (re == NULL) {
|
2007-08-13 04:06:43 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (regex) {
|
2007-12-27 21:15:43 +08:00
|
|
|
re->regex = regex;
|
|
|
|
re->name = name->data;
|
2007-08-13 04:06:43 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
name->len--;
|
|
|
|
name->data++;
|
|
|
|
|
2009-11-16 20:19:02 +08:00
|
|
|
ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
|
|
|
|
|
|
rc.pattern = *name;
|
|
|
|
rc.pool = cf->pool;
|
|
|
|
rc.options = NGX_REGEX_CASELESS;
|
|
|
|
rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
|
|
rc.err.data = errstr;
|
2007-08-13 04:06:43 +08:00
|
|
|
|
2009-11-16 20:19:02 +08:00
|
|
|
if (ngx_regex_compile(&rc) != NGX_OK) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
|
2007-08-13 04:06:43 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-11-16 20:19:02 +08:00
|
|
|
re->regex = rc.regex;
|
2007-12-27 21:15:43 +08:00
|
|
|
re->name = name->data;
|
2007-08-13 04:06:43 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"the using of the regex \"%V\" requires PCRE library",
|
|
|
|
name);
|
|
|
|
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
static int ngx_libc_cdecl
|
|
|
|
ngx_http_cmp_referer_wildcards(const void *one, const void *two)
|
|
|
|
{
|
|
|
|
ngx_hash_key_t *first, *second;
|
|
|
|
|
|
|
|
first = (ngx_hash_key_t *) one;
|
|
|
|
second = (ngx_hash_key_t *) two;
|
|
|
|
|
2009-09-12 17:28:37 +08:00
|
|
|
return ngx_dns_strcmp(first->key.data, second->key.data);
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|