Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_geo_module.c @ 2223:005fc65f7ce7
style fix: remove tabs and trailing spaces
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 01 Sep 2008 14:05:20 +0000 |
parents | 4c43e25d11ea |
children | 6e30d64f919b |
rev | line source |
---|---|
485 | 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 { | |
489 | 13 ngx_radix_tree_t *tree; |
14 ngx_pool_t *pool; | |
15 ngx_array_t values; | |
589 | 16 } ngx_http_geo_conf_ctx_t; |
485 | 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 = { | |
509 | 37 NULL, /* preconfiguration */ |
38 NULL, /* postconfiguration */ | |
485 | 39 |
40 NULL, /* create main configuration */ | |
41 NULL, /* init main configuration */ | |
42 | |
43 NULL, /* create server configuration */ | |
44 NULL, /* merge server configuration */ | |
45 | |
46 NULL, /* create location configuration */ | |
47 NULL /* merge location configuration */ | |
48 }; | |
49 | |
50 | |
51 ngx_module_t ngx_http_geo_module = { | |
509 | 52 NGX_MODULE_V1, |
485 | 53 &ngx_http_geo_module_ctx, /* module context */ |
54 ngx_http_geo_commands, /* module directives */ | |
55 NGX_HTTP_MODULE, /* module type */ | |
541 | 56 NULL, /* init master */ |
485 | 57 NULL, /* init module */ |
541 | 58 NULL, /* init process */ |
59 NULL, /* init thread */ | |
60 NULL, /* exit thread */ | |
61 NULL, /* exit process */ | |
62 NULL, /* exit master */ | |
63 NGX_MODULE_V1_PADDING | |
485 | 64 }; |
65 | |
66 | |
67 /* AF_INET only */ | |
68 | |
573 | 69 static ngx_int_t |
70 ngx_http_geo_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
71 uintptr_t data) | |
485 | 72 { |
501 | 73 ngx_radix_tree_t *tree = (ngx_radix_tree_t *) data; |
485 | 74 |
489 | 75 struct sockaddr_in *sin; |
573 | 76 ngx_http_variable_value_t *vv; |
485 | 77 |
78 sin = (struct sockaddr_in *) r->connection->sockaddr; | |
79 | |
489 | 80 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
81 "http geo started"); | |
82 | |
573 | 83 vv = (ngx_http_variable_value_t *) |
485 | 84 ngx_radix32tree_find(tree, ntohl(sin->sin_addr.s_addr)); |
489 | 85 |
573 | 86 *v = *vv; |
87 | |
489 | 88 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1406
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
89 "http geo: %V %v", &r->connection->addr_text, v); |
489 | 90 |
573 | 91 return NGX_OK; |
485 | 92 } |
93 | |
94 | |
489 | 95 static char * |
96 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 97 { |
589 | 98 char *rv; |
99 ngx_str_t *value, name; | |
100 ngx_conf_t save; | |
101 ngx_pool_t *pool; | |
102 ngx_radix_tree_t *tree; | |
1406
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
103 ngx_http_variable_t *var; |
589 | 104 ngx_http_geo_conf_ctx_t ctx; |
485 | 105 |
501 | 106 value = cf->args->elts; |
107 | |
108 name = value[1]; | |
109 | |
110 if (name.data[0] != '$') { | |
111 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
112 "\"%V\" variable name should start with '$'", | |
113 &value[1]); | |
114 } else { | |
115 name.len--; | |
116 name.data++; | |
117 } | |
118 | |
1565 | 119 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); |
501 | 120 if (var == NULL) { |
485 | 121 return NGX_CONF_ERROR; |
122 } | |
123 | |
501 | 124 tree = ngx_radix_tree_create(cf->pool, -1); |
563 | 125 |
501 | 126 if (tree == NULL) { |
485 | 127 return NGX_CONF_ERROR; |
128 } | |
129 | |
637 | 130 var->get_handler = ngx_http_geo_variable; |
501 | 131 var->data = (uintptr_t) tree; |
485 | 132 |
583 | 133 pool = ngx_create_pool(16384, cf->log); |
501 | 134 if (pool == NULL) { |
485 | 135 return NGX_CONF_ERROR; |
136 } | |
137 | |
589 | 138 if (ngx_array_init(&ctx.values, pool, 512, |
583 | 139 sizeof(ngx_http_variable_value_t *)) |
589 | 140 != NGX_OK) |
485 | 141 { |
142 ngx_destroy_pool(pool); | |
143 return NGX_CONF_ERROR; | |
144 } | |
145 | |
589 | 146 ctx.tree = tree; |
147 ctx.pool = cf->pool; | |
485 | 148 |
149 save = *cf; | |
150 cf->pool = pool; | |
589 | 151 cf->ctx = &ctx; |
485 | 152 cf->handler = ngx_http_geo; |
153 cf->handler_conf = conf; | |
154 | |
155 rv = ngx_conf_parse(cf, NULL); | |
156 | |
157 *cf = save; | |
158 | |
159 ngx_destroy_pool(pool); | |
160 | |
161 if (ngx_radix32tree_find(tree, 0) != NGX_RADIX_NO_VALUE) { | |
162 return rv; | |
163 } | |
164 | |
165 if (ngx_radix32tree_insert(tree, 0, 0, | |
577 | 166 (uintptr_t) &ngx_http_variable_null_value) |
167 == NGX_ERROR) | |
485 | 168 { |
169 return NGX_CONF_ERROR; | |
170 } | |
171 | |
172 return rv; | |
173 } | |
174 | |
175 | |
176 /* AF_INET only */ | |
177 | |
489 | 178 static char * |
179 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
485 | 180 { |
573 | 181 ngx_int_t rc; |
553 | 182 ngx_str_t *value, file; |
485 | 183 ngx_uint_t i; |
184 ngx_inet_cidr_t cidrin; | |
589 | 185 ngx_http_geo_conf_ctx_t *ctx; |
553 | 186 ngx_http_variable_value_t *var, *old, **v; |
485 | 187 |
589 | 188 ctx = cf->ctx; |
485 | 189 |
190 if (cf->args->nelts != 2) { | |
191 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
192 "invalid number of the geo parameters"); | |
193 return NGX_CONF_ERROR; | |
194 } | |
195 | |
196 value = cf->args->elts; | |
197 | |
198 if (ngx_strcmp(value[0].data, "include") == 0) { | |
199 file = value[1]; | |
200 | |
1352 | 201 if (ngx_conf_full_name(cf->cycle, &file, 1) == NGX_ERROR){ |
485 | 202 return NGX_CONF_ERROR; |
203 } | |
204 | |
205 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
206 | |
207 return ngx_conf_parse(cf, &file); | |
208 } | |
209 | |
210 if (ngx_strcmp(value[0].data, "default") == 0) { | |
211 cidrin.addr = 0; | |
212 cidrin.mask = 0; | |
213 | |
214 } else { | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
215 rc = ngx_ptocidr(&value[0], &cidrin); |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
216 |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
217 if (rc == NGX_ERROR) { |
485 | 218 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
219 "invalid parameter \"%V\"", &value[0]); | |
220 return NGX_CONF_ERROR; | |
221 } | |
222 | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
223 if (rc == NGX_DONE) { |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
224 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
225 "low address bits of %V are meaningless", |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
226 &value[0]); |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
227 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
228 |
485 | 229 cidrin.addr = ntohl(cidrin.addr); |
230 cidrin.mask = ntohl(cidrin.mask); | |
231 } | |
232 | |
233 var = NULL; | |
589 | 234 v = ctx->values.elts; |
485 | 235 |
589 | 236 for (i = 0; i < ctx->values.nelts; i++) { |
573 | 237 if ((size_t) v[i]->len != value[1].len) { |
238 continue; | |
485 | 239 } |
240 | |
589 | 241 if (ngx_strncmp(value[1].data, v[i]->data, value[1].len) == 0) { |
573 | 242 var = v[i]; |
243 break; | |
485 | 244 } |
245 } | |
246 | |
501 | 247 if (var == NULL) { |
589 | 248 var = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t)); |
485 | 249 if (var == NULL) { |
250 return NGX_CONF_ERROR; | |
251 } | |
252 | |
573 | 253 var->len = value[1].len; |
589 | 254 var->data = ngx_pstrdup(ctx->pool, &value[1]); |
573 | 255 if (var->data == NULL) { |
485 | 256 return NGX_CONF_ERROR; |
257 } | |
258 | |
573 | 259 var->valid = 1; |
1565 | 260 var->no_cacheable = 0; |
573 | 261 var->not_found = 0; |
485 | 262 |
589 | 263 v = ngx_array_push(&ctx->values); |
501 | 264 if (v == NULL) { |
485 | 265 return NGX_CONF_ERROR; |
266 } | |
267 | |
268 *v = var; | |
269 } | |
270 | |
553 | 271 for (i = 2; i; i--) { |
589 | 272 rc = ngx_radix32tree_insert(ctx->tree, cidrin.addr, cidrin.mask, |
553 | 273 (uintptr_t) var); |
274 if (rc == NGX_OK) { | |
275 return NGX_CONF_OK; | |
276 } | |
277 | |
278 if (rc == NGX_ERROR) { | |
279 return NGX_CONF_ERROR; | |
280 } | |
281 | |
282 /* rc == NGX_BUSY */ | |
283 | |
284 old = (ngx_http_variable_value_t *) | |
589 | 285 ngx_radix32tree_find(ctx->tree, cidrin.addr & cidrin.mask); |
553 | 286 |
287 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
1406
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
288 "duplicate parameter \"%V\", value: \"%v\", old value: \"%v\"", |
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
289 &value[0], var, old); |
553 | 290 |
589 | 291 rc = ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.mask); |
553 | 292 |
293 if (rc == NGX_ERROR) { | |
294 return NGX_CONF_ERROR; | |
295 } | |
485 | 296 } |
297 | |
553 | 298 return NGX_CONF_ERROR; |
485 | 299 } |