-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_referer_host_module.c
132 lines (98 loc) · 3.29 KB
/
ngx_http_referer_host_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright 2011 Alexandr Gomoliako
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_referer_host_add_vars(ngx_conf_t *cf);
static ngx_int_t ngx_http_referer_host_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_http_module_t ngx_http_referer_host_module_ctx = {
ngx_http_referer_host_add_vars, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_referer_host_module = {
NGX_MODULE_V1,
&ngx_http_referer_host_module_ctx, /* module context */
NULL, /* 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_http_variable_t ngx_http_referer_host_vars[] = {
{ ngx_string("referer_host"), NULL,
ngx_http_referer_host_variable,
0, 0, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
static ngx_int_t
ngx_http_referer_host_add_vars(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_referer_host_vars; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_referer_host_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
u_char *p, *ref, *last;
size_t len;
ngx_uint_t i;
if (r->headers_in.referer == NULL) {
goto invalid;
}
len = r->headers_in.referer->value.len;
ref = r->headers_in.referer->value.data;
if (len < sizeof("http://i.ru") - 1) {
goto invalid;
}
last = ref + len;
if (ngx_strncasecmp(ref, (u_char *) "http://", 7) == 0) {
ref += 7;
} else if (ngx_strncasecmp(ref, (u_char *) "https://", 8) == 0) {
ref += 8;
} else {
goto invalid;
}
i = 0;
for (p = ref; p < last; p++) {
if (*p == '/' || *p == ':') {
break;
}
*p = ngx_tolower(*p);
if (++i == 256) {
goto invalid;
}
}
if (i <= 0) {
goto invalid;
}
v->len = i;
v->data = ref;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
invalid:
v->not_found = 1;
return NGX_OK;
}