Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_geo_module.c @ 2706:f905a6ab0ca8
hide cacheable Set-Cookie and P3P response headers
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 15 Apr 2009 11:42:49 +0000 |
parents | a472d954c534 |
children | 2465ff3da161 415400fa0f10 |
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 { | |
2399 | 13 u_short start; |
14 u_short end; | |
15 ngx_http_variable_value_t *value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
16 } ngx_http_geo_range_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
17 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
18 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
19 typedef struct { |
2399 | 20 ngx_http_geo_range_t *ranges; |
21 ngx_uint_t n; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
22 } ngx_http_geo_low_ranges_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
23 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
24 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
25 typedef struct { |
2399 | 26 ngx_http_geo_low_ranges_t low[0x10000]; |
27 ngx_http_variable_value_t *default_value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
28 } ngx_http_geo_high_ranges_t; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
29 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
30 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
31 typedef struct { |
2399 | 32 ngx_http_variable_value_t *value; |
33 ngx_str_t *net; | |
34 ngx_http_geo_high_ranges_t *high; | |
35 ngx_radix_tree_t *tree; | |
36 ngx_rbtree_t rbtree; | |
37 ngx_rbtree_node_t sentinel; | |
38 ngx_pool_t *pool; | |
39 ngx_pool_t *temp_pool; | |
589 | 40 } ngx_http_geo_conf_ctx_t; |
485 | 41 |
42 | |
2399 | 43 typedef struct { |
44 union { | |
45 ngx_radix_tree_t *tree; | |
46 ngx_http_geo_high_ranges_t *high; | |
47 } u; | |
48 | |
49 ngx_int_t index; | |
50 } ngx_http_geo_ctx_t; | |
51 | |
52 | |
53 static in_addr_t ngx_http_geo_addr(ngx_http_request_t *r, | |
54 ngx_http_geo_ctx_t *ctx); | |
485 | 55 static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
56 static char *ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
57 static char *ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
58 ngx_str_t *value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
59 static char *ngx_http_geo_add_range(ngx_conf_t *cf, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
60 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
61 static ngx_uint_t ngx_http_geo_delete_range(ngx_conf_t *cf, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
62 ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
63 static char *ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
64 ngx_str_t *value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
65 static ngx_http_variable_value_t *ngx_http_geo_value(ngx_conf_t *cf, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
66 ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *value); |
485 | 67 |
68 | |
69 static ngx_command_t ngx_http_geo_commands[] = { | |
70 | |
71 { ngx_string("geo"), | |
2399 | 72 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12, |
485 | 73 ngx_http_geo_block, |
74 NGX_HTTP_MAIN_CONF_OFFSET, | |
75 0, | |
76 NULL }, | |
77 | |
78 ngx_null_command | |
79 }; | |
80 | |
81 | |
82 static ngx_http_module_t ngx_http_geo_module_ctx = { | |
509 | 83 NULL, /* preconfiguration */ |
84 NULL, /* postconfiguration */ | |
485 | 85 |
86 NULL, /* create main configuration */ | |
87 NULL, /* init main configuration */ | |
88 | |
89 NULL, /* create server configuration */ | |
90 NULL, /* merge server configuration */ | |
91 | |
92 NULL, /* create location configuration */ | |
93 NULL /* merge location configuration */ | |
94 }; | |
95 | |
96 | |
97 ngx_module_t ngx_http_geo_module = { | |
509 | 98 NGX_MODULE_V1, |
485 | 99 &ngx_http_geo_module_ctx, /* module context */ |
100 ngx_http_geo_commands, /* module directives */ | |
101 NGX_HTTP_MODULE, /* module type */ | |
541 | 102 NULL, /* init master */ |
485 | 103 NULL, /* init module */ |
541 | 104 NULL, /* init process */ |
105 NULL, /* init thread */ | |
106 NULL, /* exit thread */ | |
107 NULL, /* exit process */ | |
108 NULL, /* exit master */ | |
109 NGX_MODULE_V1_PADDING | |
485 | 110 }; |
111 | |
112 | |
113 /* AF_INET only */ | |
114 | |
573 | 115 static ngx_int_t |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
116 ngx_http_geo_cidr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
573 | 117 uintptr_t data) |
485 | 118 { |
2399 | 119 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data; |
485 | 120 |
573 | 121 ngx_http_variable_value_t *vv; |
485 | 122 |
573 | 123 vv = (ngx_http_variable_value_t *) |
2399 | 124 ngx_radix32tree_find(ctx->u.tree, ngx_http_geo_addr(r, ctx)); |
489 | 125 |
573 | 126 *v = *vv; |
127 | |
2399 | 128 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
129 "http geo: %v", v); | |
489 | 130 |
573 | 131 return NGX_OK; |
485 | 132 } |
133 | |
134 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
135 static ngx_int_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
136 ngx_http_geo_range_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
137 uintptr_t data) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
138 { |
2399 | 139 ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
140 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
141 in_addr_t addr; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
142 ngx_uint_t i, n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
143 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
144 |
2399 | 145 *v = *ctx->u.high->default_value; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
146 |
2399 | 147 addr = ngx_http_geo_addr(r, ctx); |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
148 |
2399 | 149 range = ctx->u.high->low[addr >> 16].ranges; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
150 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
151 n = addr & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
152 |
2399 | 153 for (i = 0; i < ctx->u.high->low[addr >> 16].n; i++) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
154 if (n >= (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
155 && n <= (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
156 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
157 *v = *range[i].value; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
158 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
159 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
160 |
2399 | 161 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
162 "http geo: %v", v); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
163 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
164 return NGX_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
165 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
166 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
167 |
2399 | 168 static in_addr_t |
169 ngx_http_geo_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx) | |
170 { | |
171 struct sockaddr_in *sin; | |
172 ngx_http_variable_value_t *v; | |
173 | |
174 if (ctx->index == -1) { | |
175 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
176 "http geo started: %V", &r->connection->addr_text); | |
177 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2457
diff
changeset
|
178 if (r->connection->sockaddr->sa_family != AF_INET) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2457
diff
changeset
|
179 return 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2457
diff
changeset
|
180 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2457
diff
changeset
|
181 |
2399 | 182 sin = (struct sockaddr_in *) r->connection->sockaddr; |
183 return ntohl(sin->sin_addr.s_addr); | |
184 } | |
185 | |
186 v = ngx_http_get_flushed_variable(r, ctx->index); | |
187 | |
188 if (v == NULL || v->not_found) { | |
189 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
190 "http geo not found"); | |
191 | |
192 return 0; | |
193 } | |
194 | |
195 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
196 "http geo started: %v", v); | |
197 | |
198 return ntohl(ngx_inet_addr(v->data, v->len)); | |
199 } | |
200 | |
201 | |
489 | 202 static char * |
203 ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 204 { |
589 | 205 char *rv; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
206 size_t len; |
2399 | 207 ngx_str_t *value, name; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
208 ngx_uint_t i; |
589 | 209 ngx_conf_t save; |
210 ngx_pool_t *pool; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
211 ngx_array_t *a; |
1406
03341711f9a2
use %v for ngx_variable_value_t in ngx_sprintf(),
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
212 ngx_http_variable_t *var; |
2399 | 213 ngx_http_geo_ctx_t *geo; |
589 | 214 ngx_http_geo_conf_ctx_t ctx; |
485 | 215 |
501 | 216 value = cf->args->elts; |
217 | |
2399 | 218 geo = ngx_palloc(cf->pool, sizeof(ngx_http_geo_ctx_t)); |
219 if (geo == NULL) { | |
220 return NGX_CONF_ERROR; | |
221 } | |
222 | |
223 name = value[1]; | |
224 name.len--; | |
225 name.data++; | |
226 | |
227 if (cf->args->nelts == 3) { | |
228 | |
229 geo->index = ngx_http_get_variable_index(cf, &name); | |
230 if (geo->index == NGX_ERROR) { | |
231 return NGX_CONF_ERROR; | |
232 } | |
233 | |
234 name = value[2]; | |
235 name.len--; | |
236 name.data++; | |
237 | |
238 } else { | |
239 geo->index = -1; | |
240 } | |
241 | |
242 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); | |
501 | 243 if (var == NULL) { |
485 | 244 return NGX_CONF_ERROR; |
245 } | |
246 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
247 pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
248 if (pool == NULL) { |
485 | 249 return NGX_CONF_ERROR; |
250 } | |
251 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
252 ctx.temp_pool = ngx_create_pool(16384, cf->log); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
253 if (ctx.temp_pool == NULL) { |
485 | 254 return NGX_CONF_ERROR; |
255 } | |
256 | |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
257 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, |
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
258 ngx_http_variable_value_rbtree_insert); |
485 | 259 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
260 ctx.high = NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
261 ctx.tree = NULL; |
589 | 262 ctx.pool = cf->pool; |
485 | 263 |
264 save = *cf; | |
265 cf->pool = pool; | |
589 | 266 cf->ctx = &ctx; |
485 | 267 cf->handler = ngx_http_geo; |
268 cf->handler_conf = conf; | |
269 | |
270 rv = ngx_conf_parse(cf, NULL); | |
271 | |
272 *cf = save; | |
273 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
274 if (ctx.high) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
275 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
276 for (i = 0; i < 0x10000; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
277 a = (ngx_array_t *) ctx.high->low[i].ranges; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
278 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
279 if (a == NULL || a->nelts == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
280 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
281 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
282 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
283 ctx.high->low[i].n = a->nelts; |
485 | 284 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
285 len = a->nelts * sizeof(ngx_http_geo_range_t); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
286 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
287 ctx.high->low[i].ranges = ngx_palloc(cf->pool, len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
288 if (ctx.high->low[i].ranges == NULL ){ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
289 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
290 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
291 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
292 ngx_memcpy(ctx.high->low[i].ranges, a->elts, len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
293 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
294 |
2399 | 295 geo->u.high = ctx.high; |
296 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
297 var->get_handler = ngx_http_geo_range_variable; |
2399 | 298 var->data = (uintptr_t) geo; |
485 | 299 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
300 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
301 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
302 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
303 if (ctx.high->default_value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
304 ctx.high->default_value = &ngx_http_variable_null_value; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
305 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
306 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
307 } else { |
2358
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
308 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
309 ctx.tree = ngx_radix_tree_create(cf->pool, -1); |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
310 if (ctx.tree == NULL) { |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
311 return NGX_CONF_ERROR; |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
312 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
313 } |
ac0cf19eb582
create empty radix tree for "geo $geo { }" case
Igor Sysoev <igor@sysoev.ru>
parents:
2340
diff
changeset
|
314 |
2399 | 315 geo->u.tree = ctx.tree; |
316 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
317 var->get_handler = ngx_http_geo_cidr_variable; |
2399 | 318 var->data = (uintptr_t) geo; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
319 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
320 ngx_destroy_pool(ctx.temp_pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
321 ngx_destroy_pool(pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
322 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
323 if (ngx_radix32tree_find(ctx.tree, 0) != NGX_RADIX_NO_VALUE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
324 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
325 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
326 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
327 if (ngx_radix32tree_insert(ctx.tree, 0, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
328 (uintptr_t) &ngx_http_variable_null_value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
329 == NGX_ERROR) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
330 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
331 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
332 } |
485 | 333 } |
334 | |
335 return rv; | |
336 } | |
337 | |
338 | |
489 | 339 static char * |
340 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
485 | 341 { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
342 char *rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
343 ngx_str_t *value, file; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
344 ngx_http_geo_conf_ctx_t *ctx; |
485 | 345 |
589 | 346 ctx = cf->ctx; |
485 | 347 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
348 value = cf->args->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
349 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
350 if (cf->args->nelts == 1) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
351 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
352 if (ngx_strcmp(value[0].data, "ranges") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
353 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
354 if (ctx->tree) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
355 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
356 "the \"ranges\" directive must be " |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
357 "the first directive inside \"geo\" block"); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
358 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
359 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
360 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
361 ctx->high = ngx_pcalloc(ctx->pool, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
362 sizeof(ngx_http_geo_high_ranges_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
363 if (ctx->high == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
364 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
365 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
366 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
367 rv = NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
368 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
369 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
370 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
371 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
372 |
485 | 373 if (cf->args->nelts != 2) { |
374 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
375 "invalid number of the geo parameters"); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
376 goto failed; |
485 | 377 } |
378 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
379 if (ngx_strcmp(value[0].data, "include") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
380 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
381 file.len = value[1].len++; |
485 | 382 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
383 file.data = ngx_pstrdup(ctx->temp_pool, &value[1]); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
384 if (file.data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
385 goto failed; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
386 } |
485 | 387 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
388 if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK){ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
389 goto failed; |
485 | 390 } |
391 | |
392 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data); | |
393 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
394 rv = ngx_conf_parse(cf, &file); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
395 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
396 goto done; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
397 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
398 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
399 if (ctx->high) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
400 rv = ngx_http_geo_range(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
401 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
402 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
403 rv = ngx_http_geo_cidr(cf, ctx, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
404 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
405 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
406 done: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
407 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
408 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
409 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
410 return rv; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
411 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
412 failed: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
413 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
414 ngx_reset_pool(cf->pool); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
415 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
416 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
417 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
418 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
419 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
420 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
421 ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
422 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
423 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
424 u_char *p, *last; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
425 in_addr_t start, end; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
426 ngx_str_t *net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
427 ngx_uint_t del; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
428 ngx_http_variable_value_t *old; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
429 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
430 if (ngx_strcmp(value[0].data, "default") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
431 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
432 old = ctx->high->default_value; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
433 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
434 ctx->high->default_value = ngx_http_geo_value(cf, ctx, &value[1]); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
435 if (ctx->high->default_value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
436 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
437 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
438 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
439 if (old) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
440 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
441 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
442 &value[0], ctx->high->default_value, old); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
443 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
444 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
445 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
446 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
447 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
448 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
449 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
450 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
451 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
452 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
453 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
454 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
455 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
456 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
457 last = net->data + net->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
458 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
459 p = ngx_strlchr(net->data, last, '-'); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
460 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
461 if (p == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
462 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
463 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
464 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
465 start = ngx_inet_addr(net->data, p - net->data); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
466 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
467 if (start == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
468 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
469 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
470 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
471 start = ntohl(start); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
472 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
473 p++; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
474 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
475 end = ngx_inet_addr(p, last - p); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
476 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
477 if (end == INADDR_NONE) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
478 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
479 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
480 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
481 end = ntohl(end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
482 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
483 if (start > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
484 goto invalid; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
485 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
486 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
487 if (del) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
488 if (ngx_http_geo_delete_range(cf, ctx, start, end)) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
489 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
490 "no address range \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
491 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
492 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
493 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
494 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
495 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
496 ctx->value = ngx_http_geo_value(cf, ctx, &value[1]); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
497 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
498 if (ctx->value == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
499 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
500 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
501 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
502 ctx->net = net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
503 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
504 return ngx_http_geo_add_range(cf, ctx, start, end); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
505 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
506 invalid: |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
507 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
508 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid range \"%V\"", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
509 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
510 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
511 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
512 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
513 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
514 /* the add procedure is optimized to add a growing up sequence */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
515 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
516 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
517 ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
518 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
519 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
520 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
521 ngx_uint_t h, i, s, e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
522 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
523 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
524 |
2451 | 525 for (n = start; n <= end; n += 0x10000) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
526 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
527 h = n >> 16; |
2447
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
528 |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
529 if (n == start) { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
530 s = n & 0xffff; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
531 } else { |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
532 s = 0; |
3d868fc44879
fix range start for ranges those spread in two or more slots
Igor Sysoev <igor@sysoev.ru>
parents:
2442
diff
changeset
|
533 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
534 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
535 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
536 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
537 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
538 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
539 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
540 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
541 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
542 a = (ngx_array_t *) ctx->high->low[h].ranges; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
543 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
544 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
545 a = ngx_array_create(ctx->temp_pool, 64, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
546 sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
547 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
548 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
549 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
550 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
551 ctx->high->low[h].ranges = (ngx_http_geo_range_t *) a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
552 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
553 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
554 i = a->nelts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
555 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
556 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
557 while (i) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
558 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
559 i--; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
560 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
561 if (e < (ngx_uint_t) range[i].start) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
562 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
563 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
564 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
565 if (s > (ngx_uint_t) range[i].end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
566 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
567 /* add after the range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
568 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
569 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
570 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
571 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
572 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
573 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
574 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
575 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
576 ngx_memcpy(&range[i + 2], &range[i + 1], |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
577 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
578 |
2442 | 579 range[i + 1].start = (u_short) s; |
580 range[i + 1].end = (u_short) e; | |
581 range[i + 1].value = ctx->value; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
582 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
583 goto next; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
584 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
585 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
586 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
587 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
588 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
589 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
590 "duplicate range \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
591 ctx->net, ctx->value, range[i].value); |
2442 | 592 |
593 range[i].value = ctx->value; | |
594 | |
595 goto next; | |
596 } | |
597 | |
598 if (s > (ngx_uint_t) range[i].start | |
599 && e < (ngx_uint_t) range[i].end) | |
600 { | |
601 /* split the range and insert the new one */ | |
602 | |
603 range = ngx_array_push(a); | |
604 if (range == NULL) { | |
605 return NGX_CONF_ERROR; | |
606 } | |
607 | |
608 range = ngx_array_push(a); | |
609 if (range == NULL) { | |
610 return NGX_CONF_ERROR; | |
611 } | |
612 | |
613 range = a->elts; | |
614 | |
615 ngx_memcpy(&range[i + 3], &range[i + 1], | |
616 (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t)); | |
617 | |
618 range[i + 2].start = (u_short) (e + 1); | |
619 range[i + 2].end = range[i].end; | |
620 range[i + 2].value = range[i].value; | |
621 | |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
622 range[i + 1].start = (u_short) s; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
623 range[i + 1].end = (u_short) e; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
624 range[i + 1].value = ctx->value; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
625 |
2442 | 626 range[i].end = (u_short) (s - 1); |
627 | |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
628 goto next; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
629 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
630 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
631 if (s == (ngx_uint_t) range[i].start |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
632 && e < (ngx_uint_t) range[i].end) |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
633 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
634 /* shift the range start and insert the new range */ |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
635 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
636 range = ngx_array_push(a); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
637 if (range == NULL) { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
638 return NGX_CONF_ERROR; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
639 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
640 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
641 range = a->elts; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
642 |
2457
1aff348d9aa8
fix segfault when geo range replaces starting part of another range
Igor Sysoev <igor@sysoev.ru>
parents:
2452
diff
changeset
|
643 ngx_memcpy(&range[i + 1], &range[i], |
1aff348d9aa8
fix segfault when geo range replaces starting part of another range
Igor Sysoev <igor@sysoev.ru>
parents:
2452
diff
changeset
|
644 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t)); |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
645 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
646 range[i + 1].start = (u_short) (e + 1); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
647 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
648 range[i].start = (u_short) s; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
649 range[i].end = (u_short) e; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
650 range[i].value = ctx->value; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
651 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
652 goto next; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
653 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
654 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
655 if (s > (ngx_uint_t) range[i].start |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
656 && e == (ngx_uint_t) range[i].end) |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
657 { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
658 /* shift the range end and insert the new range */ |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
659 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
660 range = ngx_array_push(a); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
661 if (range == NULL) { |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
662 return NGX_CONF_ERROR; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
663 } |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
664 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
665 range = a->elts; |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
666 |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
667 ngx_memcpy(&range[i + 2], &range[i + 1], |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
668 (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t)); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
669 |
2442 | 670 range[i + 1].start = (u_short) s; |
671 range[i + 1].end = (u_short) e; | |
672 range[i + 1].value = ctx->value; | |
673 | |
2452
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
674 range[i].end = (u_short) (s - 1); |
95f078e7d743
allow insertion range if its start or end is the same as existent one
Igor Sysoev <igor@sysoev.ru>
parents:
2451
diff
changeset
|
675 |
2442 | 676 goto next; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
677 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
678 |
2448 | 679 s = (ngx_uint_t) range[i].start; |
680 e = (ngx_uint_t) range[i].end; | |
681 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
682 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2448 | 683 "range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"", |
684 ctx->net, | |
685 h >> 8, h & 0xff, s >> 8, s & 0xff, | |
686 h >> 8, h & 0xff, e >> 8, e & 0xff); | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
687 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
688 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
689 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
690 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
691 /* add the first range */ |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
692 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
693 range = ngx_array_push(a); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
694 if (range == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
695 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
696 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
697 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
698 range->start = (u_short) s; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
699 range->end = (u_short) e; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
700 range->value = ctx->value; |
2442 | 701 |
702 next: | |
703 | |
704 continue; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
705 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
706 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
707 return NGX_CONF_OK; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
708 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
709 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
710 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
711 static ngx_uint_t |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
712 ngx_http_geo_delete_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
713 in_addr_t start, in_addr_t end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
714 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
715 in_addr_t n; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
716 ngx_uint_t h, i, s, e, warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
717 ngx_array_t *a; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
718 ngx_http_geo_range_t *range; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
719 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
720 warn = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
721 |
2451 | 722 for (n = start; n <= end; n += 0x10000) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
723 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
724 h = n >> 16; |
2449
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
725 |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
726 if (n == start) { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
727 s = n & 0xffff; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
728 } else { |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
729 s = 0; |
450a277dad48
repeat r2448 for range deletion
Igor Sysoev <igor@sysoev.ru>
parents:
2448
diff
changeset
|
730 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
731 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
732 if ((n | 0xffff) > end) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
733 e = end & 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
734 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
735 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
736 e = 0xffff; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
737 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
738 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
739 a = (ngx_array_t *) ctx->high->low[h].ranges; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
740 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
741 if (a == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
742 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
743 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
744 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
745 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
746 range = a->elts; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
747 for (i = 0; i < a->nelts; i++) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
748 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
749 if (s == (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
750 && e == (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
751 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
752 ngx_memcpy(&range[i], &range[i + 1], |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
753 (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t)); |
2450 | 754 |
755 a->nelts--; | |
756 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
757 break; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
758 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
759 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
760 if (s != (ngx_uint_t) range[i].start |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
761 && e != (ngx_uint_t) range[i].end) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
762 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
763 continue; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
764 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
765 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
766 warn = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
767 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
768 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
769 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
770 return warn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
771 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
772 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
773 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
774 static char * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
775 ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
776 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
777 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
778 ngx_int_t rc, del; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
779 ngx_str_t *net; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
780 ngx_uint_t i; |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
781 ngx_cidr_t cidr; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
782 ngx_http_variable_value_t *val, *old; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
783 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
784 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
785 ctx->tree = ngx_radix_tree_create(ctx->pool, -1); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
786 if (ctx->tree == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
787 return NGX_CONF_ERROR; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
788 } |
485 | 789 } |
790 | |
791 if (ngx_strcmp(value[0].data, "default") == 0) { | |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
792 cidr.u.in.addr = 0; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
793 cidr.u.in.mask = 0; |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
794 net = &value[0]; |
485 | 795 |
796 } else { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
797 if (ngx_strcmp(value[0].data, "delete") == 0) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
798 net = &value[1]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
799 del = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
800 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
801 } else { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
802 net = &value[0]; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
803 del = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
804 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
805 |
2399 | 806 if (ngx_strcmp(net->data, "255.255.255.255") == 0) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
807 cidr.u.in.addr = 0xffffffff; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
808 cidr.u.in.mask = 0xffffffff; |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
809 |
2399 | 810 } else { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
811 rc = ngx_ptocidr(net, &cidr); |
485 | 812 |
2401 | 813 if (rc == NGX_ERROR) { |
814 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
815 "invalid network \"%V\"", net); | |
816 return NGX_CONF_ERROR; | |
817 } | |
2399 | 818 |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
819 if (cidr.family != AF_INET) { |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
820 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
821 "\"geo\" supports IPv4 only"); |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
822 return NGX_CONF_ERROR; |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
823 } |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
824 |
2401 | 825 if (rc == NGX_DONE) { |
826 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
827 "low address bits of %V are meaningless", | |
2399 | 828 net); |
2401 | 829 } |
2399 | 830 |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
831 cidr.u.in.addr = ntohl(cidr.u.in.addr); |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
832 cidr.u.in.mask = ntohl(cidr.u.in.mask); |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
833 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1352
diff
changeset
|
834 |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
835 if (del) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
836 if (ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, |
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
837 cidr.u.in.mask) |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
838 != NGX_OK) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
839 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
840 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
841 "no network \"%V\" to delete", net); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
842 } |
2365 | 843 |
844 return NGX_CONF_OK; | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
845 } |
485 | 846 } |
847 | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
848 val = ngx_http_geo_value(cf, ctx, &value[1]); |
485 | 849 |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
850 if (val == NULL) { |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
851 return NGX_CONF_ERROR; |
485 | 852 } |
853 | |
553 | 854 for (i = 2; i; i--) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
855 rc = ngx_radix32tree_insert(ctx->tree, cidr.u.in.addr, cidr.u.in.mask, |
2334
6e30d64f919b
use value rbtree instead of array in geo configuration
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
856 (uintptr_t) val); |
553 | 857 if (rc == NGX_OK) { |
858 return NGX_CONF_OK; | |
859 } | |
860 | |
861 if (rc == NGX_ERROR) { | |
862 return NGX_CONF_ERROR; | |
863 } | |
864 | |
865 /* rc == NGX_BUSY */ | |
866 | |
867 old = (ngx_http_variable_value_t *) | |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
868 ngx_radix32tree_find(ctx->tree, cidr.u.in.addr & cidr.u.in.mask); |
553 | 869 |
870 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
871 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
872 net, val, old); |
553 | 873 |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
874 rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, cidr.u.in.mask); |
553 | 875 |
876 if (rc == NGX_ERROR) { | |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
877 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); |
553 | 878 return NGX_CONF_ERROR; |
879 } | |
485 | 880 } |
881 | |
553 | 882 return NGX_CONF_ERROR; |
485 | 883 } |
2340
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
884 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
885 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
886 static ngx_http_variable_value_t * |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
887 ngx_http_geo_value(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
888 ngx_str_t *value) |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
889 { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
890 uint32_t hash; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
891 ngx_http_variable_value_t *val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
892 ngx_http_variable_value_node_t *vvn; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
893 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
894 hash = ngx_crc32_long(value->data, value->len); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
895 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
896 val = ngx_http_variable_value_lookup(&ctx->rbtree, value, hash); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
897 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
898 if (val) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
899 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
900 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
901 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
902 val = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
903 if (val == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
904 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
905 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
906 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
907 val->len = value->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
908 val->data = ngx_pstrdup(ctx->pool, value); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
909 if (val->data == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
910 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
911 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
912 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
913 val->valid = 1; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
914 val->no_cacheable = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
915 val->not_found = 0; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
916 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
917 vvn = ngx_palloc(ctx->temp_pool, sizeof(ngx_http_variable_value_node_t)); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
918 if (vvn == NULL) { |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
919 return NULL; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
920 } |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
921 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
922 vvn->node.key = hash; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
923 vvn->len = val->len; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
924 vvn->value = val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
925 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
926 ngx_rbtree_insert(&ctx->rbtree, &vvn->node); |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
927 |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
928 return val; |
22d2b6609853
*) descrease geo configuration memory usage
Igor Sysoev <igor@sysoev.ru>
parents:
2334
diff
changeset
|
929 } |