comparison src/http/modules/ngx_http_geo_module.c @ 34:aab2ea7c0458 NGINX_0_1_17

nginx 0.1.17 *) Change: the ngx_http_rewrite_module was rewritten from the scratch. Now it is possible to redirect, to return the error codes, to check the variables and referrers. The directives can be used inside locations. The redirect directive was canceled. *) Feature: the ngx_http_geo_module. *) Feature: the proxy_set_x_var and fastcgi_set_var directives. *) Bugfix: the location configuration with "=" modifier may be used in another location. *) Bugfix: the correct content type was set only for requests that use small caps letters in extension. *) Bugfix: if the proxy_pass or fastcgi_pass directives were set in the location, and access was denied, and the error was redirected to a static page, then the segmentation fault occurred. *) Bugfix: if in a proxied "Location" header was a relative URL, then a host name and a slash were added to them; bug appeared in 0.1.14. *) Bugfix: the system error message was not logged on Linux.
author Igor Sysoev <http://sysoev.ru>
date Thu, 03 Feb 2005 00:00:00 +0300
parents
children a39d1b793287
comparison
equal deleted inserted replaced
33:27f09a550803 34:aab2ea7c0458
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_radix_tree_t *tree;
14 ngx_pool_t *pool;
15 ngx_array_t values;
16 } ngx_http_geo_conf_t;
17
18
19 static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
20 static char *ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
21
22
23 static ngx_command_t ngx_http_geo_commands[] = {
24
25 { ngx_string("geo"),
26 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
27 ngx_http_geo_block,
28 NGX_HTTP_MAIN_CONF_OFFSET,
29 0,
30 NULL },
31
32 ngx_null_command
33 };
34
35
36 static ngx_http_module_t ngx_http_geo_module_ctx = {
37 NULL, /* pre conf */
38
39 NULL, /* create main configuration */
40 NULL, /* init main configuration */
41
42 NULL, /* create server configuration */
43 NULL, /* merge server configuration */
44
45 NULL, /* create location configuration */
46 NULL /* merge location configuration */
47 };
48
49
50 ngx_module_t ngx_http_geo_module = {
51 NGX_MODULE,
52 &ngx_http_geo_module_ctx, /* module context */
53 ngx_http_geo_commands, /* module directives */
54 NGX_HTTP_MODULE, /* module type */
55 NULL, /* init module */
56 NULL /* init process */
57 };
58
59
60 static ngx_http_variable_value_t ngx_http_geo_null_value =
61 { 0, ngx_string("0") };
62
63
64 /* AF_INET only */
65
66 static ngx_http_variable_value_t *ngx_http_geo_variable(ngx_http_request_t *r,
67 void *data)
68 {
69 ngx_radix_tree_t *tree = data;
70
71 struct sockaddr_in *sin;
72
73 sin = (struct sockaddr_in *) r->connection->sockaddr;
74
75 return (ngx_http_variable_value_t *)
76 ngx_radix32tree_find(tree, ntohl(sin->sin_addr.s_addr));
77 }
78
79
80 static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
81 {
82 char *rv;
83 ngx_str_t *value;
84 ngx_conf_t save;
85 ngx_pool_t *pool;
86 ngx_radix_tree_t *tree;
87 ngx_http_geo_conf_t geo;
88 ngx_http_variable_t *var;
89
90 if (!(var = ngx_http_add_variable(cf))) {
91 return NGX_CONF_ERROR;
92 }
93
94 if (!(tree = ngx_radix_tree_create(cf->pool))) {
95 return NGX_CONF_ERROR;
96 }
97
98 value = cf->args->elts;
99
100 var->name = value[1];
101 var->handler = ngx_http_geo_variable;
102 var->data = tree;
103
104 /*
105 * create the temporary pool of a huge initial size
106 * to process quickly a large number of geo lines
107 */
108
109 if (!(pool = ngx_create_pool(512 * 1024, cf->log))) {
110 return NGX_CONF_ERROR;
111 }
112
113 if (ngx_array_init(&geo.values, pool, 512,
114 sizeof(ngx_http_variable_value_t *)) == NGX_ERROR)
115 {
116 ngx_destroy_pool(pool);
117 return NGX_CONF_ERROR;
118 }
119
120 geo.tree = tree;
121 geo.pool = cf->pool;
122
123 save = *cf;
124 cf->pool = pool;
125 cf->ctx = &geo;
126 cf->handler = ngx_http_geo;
127 cf->handler_conf = conf;
128
129 rv = ngx_conf_parse(cf, NULL);
130
131 *cf = save;
132
133 ngx_destroy_pool(pool);
134
135 if (ngx_radix32tree_find(tree, 0) != NGX_RADIX_NO_VALUE) {
136 return rv;
137 }
138
139 if (ngx_radix32tree_insert(tree, 0, 0,
140 (uintptr_t) &ngx_http_geo_null_value) == NGX_ERROR)
141 {
142 return NGX_CONF_ERROR;
143 }
144
145 return rv;
146 }
147
148
149 /* AF_INET only */
150
151 static char *ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
152 {
153 ngx_int_t rc, n;
154 ngx_uint_t i;
155 ngx_str_t *value, file;
156 ngx_inet_cidr_t cidrin;
157 ngx_http_geo_conf_t *geo;
158 ngx_http_variable_value_t *var, **v;
159
160 geo = cf->ctx;
161
162 if (cf->args->nelts != 2) {
163 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
164 "invalid number of the geo parameters");
165 return NGX_CONF_ERROR;
166 }
167
168 value = cf->args->elts;
169
170 if (ngx_strcmp(value[0].data, "include") == 0) {
171 file = value[1];
172
173 if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR){
174 return NGX_CONF_ERROR;
175 }
176
177 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
178
179 return ngx_conf_parse(cf, &file);
180 }
181
182 if (ngx_strcmp(value[0].data, "default") == 0) {
183 cidrin.addr = 0;
184 cidrin.mask = 0;
185
186 } else {
187 if (ngx_ptocidr(&value[0], &cidrin) == NGX_ERROR) {
188 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
189 "invalid parameter \"%V\"", &value[0]);
190 return NGX_CONF_ERROR;
191 }
192
193 cidrin.addr = ntohl(cidrin.addr);
194 cidrin.mask = ntohl(cidrin.mask);
195 }
196
197 n = ngx_atoi(value[1].data, value[1].len);
198
199 var = NULL;
200 v = geo->values.elts;
201
202 if (n == NGX_ERROR) {
203 for (i = 0; i < geo->values.nelts; i++) {
204 if (ngx_strcmp(value[1].data, v[i]->text.data) == 0) {
205 var = v[i];
206 break;
207 }
208 }
209
210 } else {
211 for (i = 0; i < geo->values.nelts; i++) {
212 if (v[i]->value == (ngx_uint_t) n) {
213 var = v[i];
214 break;
215 }
216 }
217 }
218
219 if (i == geo->values.nelts) {
220 var = ngx_palloc(geo->pool, sizeof(ngx_http_variable_value_t));
221 if (var == NULL) {
222 return NGX_CONF_ERROR;
223 }
224
225 var->text.len = value[1].len;
226 if (!(var->text.data = ngx_pstrdup(geo->pool, &value[1]))) {
227 return NGX_CONF_ERROR;
228 }
229
230 var->value = (n == NGX_ERROR) ? 0 : n;
231
232 if (!(v = ngx_array_push(&geo->values))) {
233 return NGX_CONF_ERROR;
234 }
235
236 *v = var;
237 }
238
239 rc = ngx_radix32tree_insert(geo->tree, cidrin.addr, cidrin.mask,
240 (uintptr_t) var);
241 if (rc == NGX_ERROR) {
242 return NGX_CONF_ERROR;
243 }
244
245 if (rc == NGX_BUSY) {
246 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "duplicate parameter \"%V\"",
247 &value[0]);
248 return NGX_CONF_ERROR;
249 }
250
251 return NGX_CONF_OK;
252 }