nginx 0.1.19
[nginx-catap.git] / src / http / modules / ngx_http_geo_module.c
blob0d8267d8e5f3a045b2ea1cc0a6a0f6a6551db6d8
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
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;
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);
23 static ngx_command_t ngx_http_geo_commands[] = {
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,
30 NULL },
32 ngx_null_command
36 static ngx_http_module_t ngx_http_geo_module_ctx = {
37 NULL, /* pre conf */
39 NULL, /* create main configuration */
40 NULL, /* init main configuration */
42 NULL, /* create server configuration */
43 NULL, /* merge server configuration */
45 NULL, /* create location configuration */
46 NULL /* merge location configuration */
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 */
60 static ngx_http_variable_value_t ngx_http_geo_null_value =
61 { 0, ngx_string("0") };
64 /* AF_INET only */
66 static ngx_http_variable_value_t *
67 ngx_http_geo_variable(ngx_http_request_t *r, void *data)
69 ngx_radix_tree_t *tree = data;
71 struct sockaddr_in *sin;
72 ngx_http_variable_value_t *var;
74 sin = (struct sockaddr_in *) r->connection->sockaddr;
76 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
77 "http geo started");
79 var = (ngx_http_variable_value_t *)
80 ngx_radix32tree_find(tree, ntohl(sin->sin_addr.s_addr));
82 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
83 "http geo: %V %V", &r->connection->addr_text, &var->text);
85 return var;
89 static char *
90 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
92 char *rv;
93 ngx_str_t *value;
94 ngx_conf_t save;
95 ngx_pool_t *pool;
96 ngx_radix_tree_t *tree;
97 ngx_http_geo_conf_t geo;
98 ngx_http_variable_t *var;
100 if (!(var = ngx_http_add_variable(cf))) {
101 return NGX_CONF_ERROR;
104 if (!(tree = ngx_radix_tree_create(cf->pool, -1))) {
105 return NGX_CONF_ERROR;
108 value = cf->args->elts;
110 var->name = value[1];
111 var->handler = ngx_http_geo_variable;
112 var->data = tree;
115 * create the temporary pool of a huge initial size
116 * to process quickly a large number of geo lines
119 if (!(pool = ngx_create_pool(512 * 1024, cf->log))) {
120 return NGX_CONF_ERROR;
123 if (ngx_array_init(&geo.values, pool, 512,
124 sizeof(ngx_http_variable_value_t *)) == NGX_ERROR)
126 ngx_destroy_pool(pool);
127 return NGX_CONF_ERROR;
130 geo.tree = tree;
131 geo.pool = cf->pool;
133 save = *cf;
134 cf->pool = pool;
135 cf->ctx = &geo;
136 cf->handler = ngx_http_geo;
137 cf->handler_conf = conf;
139 rv = ngx_conf_parse(cf, NULL);
141 *cf = save;
143 ngx_destroy_pool(pool);
145 if (ngx_radix32tree_find(tree, 0) != NGX_RADIX_NO_VALUE) {
146 return rv;
149 if (ngx_radix32tree_insert(tree, 0, 0,
150 (uintptr_t) &ngx_http_geo_null_value) == NGX_ERROR)
152 return NGX_CONF_ERROR;
155 return rv;
159 /* AF_INET only */
161 static char *
162 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
164 ngx_int_t rc, n;
165 ngx_uint_t i;
166 ngx_str_t *value, file;
167 ngx_inet_cidr_t cidrin;
168 ngx_http_geo_conf_t *geo;
169 ngx_http_variable_value_t *var, **v;
171 geo = cf->ctx;
173 if (cf->args->nelts != 2) {
174 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
175 "invalid number of the geo parameters");
176 return NGX_CONF_ERROR;
179 value = cf->args->elts;
181 if (ngx_strcmp(value[0].data, "include") == 0) {
182 file = value[1];
184 if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR){
185 return NGX_CONF_ERROR;
188 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
190 return ngx_conf_parse(cf, &file);
193 if (ngx_strcmp(value[0].data, "default") == 0) {
194 cidrin.addr = 0;
195 cidrin.mask = 0;
197 } else {
198 if (ngx_ptocidr(&value[0], &cidrin) == NGX_ERROR) {
199 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
200 "invalid parameter \"%V\"", &value[0]);
201 return NGX_CONF_ERROR;
204 cidrin.addr = ntohl(cidrin.addr);
205 cidrin.mask = ntohl(cidrin.mask);
208 n = ngx_atoi(value[1].data, value[1].len);
210 var = NULL;
211 v = geo->values.elts;
213 if (n == NGX_ERROR) {
214 for (i = 0; i < geo->values.nelts; i++) {
215 if (ngx_strcmp(value[1].data, v[i]->text.data) == 0) {
216 var = v[i];
217 break;
221 } else {
222 for (i = 0; i < geo->values.nelts; i++) {
223 if (v[i]->value == (ngx_uint_t) n) {
224 var = v[i];
225 break;
230 if (i == geo->values.nelts) {
231 var = ngx_palloc(geo->pool, sizeof(ngx_http_variable_value_t));
232 if (var == NULL) {
233 return NGX_CONF_ERROR;
236 var->text.len = value[1].len;
237 if (!(var->text.data = ngx_pstrdup(geo->pool, &value[1]))) {
238 return NGX_CONF_ERROR;
241 var->value = (n == NGX_ERROR) ? 0 : n;
243 if (!(v = ngx_array_push(&geo->values))) {
244 return NGX_CONF_ERROR;
247 *v = var;
250 rc = ngx_radix32tree_insert(geo->tree, cidrin.addr, cidrin.mask,
251 (uintptr_t) var);
252 if (rc == NGX_ERROR) {
253 return NGX_CONF_ERROR;
256 if (rc == NGX_BUSY) {
257 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "duplicate parameter \"%V\"",
258 &value[0]);
259 return NGX_CONF_ERROR;
262 return NGX_CONF_OK;