Mercurial > hg > nginx-ranges
comparison src/http/modules/ngx_http_geoip_module.c @ 635:e67b227c8dbb default tip
Merge with current.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 25 Apr 2011 04:07:55 +0400 |
parents | bb20316269e4 |
children |
comparison
equal
deleted
inserted
replaced
578:f3a9e57d2e17 | 635:e67b227c8dbb |
---|---|
28 | 28 |
29 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, | 29 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, |
30 ngx_http_variable_value_t *v, uintptr_t data); | 30 ngx_http_variable_value_t *v, uintptr_t data); |
31 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r, | 31 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r, |
32 ngx_http_variable_value_t *v, uintptr_t data); | 32 ngx_http_variable_value_t *v, uintptr_t data); |
33 static ngx_int_t ngx_http_geoip_region_name_variable(ngx_http_request_t *r, | |
34 ngx_http_variable_value_t *v, uintptr_t data); | |
33 static ngx_int_t ngx_http_geoip_city_float_variable(ngx_http_request_t *r, | 35 static ngx_int_t ngx_http_geoip_city_float_variable(ngx_http_request_t *r, |
36 ngx_http_variable_value_t *v, uintptr_t data); | |
37 static ngx_int_t ngx_http_geoip_city_int_variable(ngx_http_request_t *r, | |
34 ngx_http_variable_value_t *v, uintptr_t data); | 38 ngx_http_variable_value_t *v, uintptr_t data); |
35 static GeoIPRecord *ngx_http_geoip_get_city_record(ngx_http_request_t *r); | 39 static GeoIPRecord *ngx_http_geoip_get_city_record(ngx_http_request_t *r); |
36 | 40 |
37 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf); | 41 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf); |
38 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf); | 42 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf); |
44 | 48 |
45 | 49 |
46 static ngx_command_t ngx_http_geoip_commands[] = { | 50 static ngx_command_t ngx_http_geoip_commands[] = { |
47 | 51 |
48 { ngx_string("geoip_country"), | 52 { ngx_string("geoip_country"), |
49 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | 53 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12, |
50 ngx_http_geoip_country, | 54 ngx_http_geoip_country, |
51 NGX_HTTP_MAIN_CONF_OFFSET, | 55 NGX_HTTP_MAIN_CONF_OFFSET, |
52 0, | 56 0, |
53 NULL }, | 57 NULL }, |
54 | 58 |
55 { ngx_string("geoip_city"), | 59 { ngx_string("geoip_city"), |
56 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | 60 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12, |
57 ngx_http_geoip_city, | 61 ngx_http_geoip_city, |
58 NGX_HTTP_MAIN_CONF_OFFSET, | 62 NGX_HTTP_MAIN_CONF_OFFSET, |
59 0, | 63 0, |
60 NULL }, | 64 NULL }, |
61 | 65 |
126 | 130 |
127 { ngx_string("geoip_region"), NULL, | 131 { ngx_string("geoip_region"), NULL, |
128 ngx_http_geoip_city_variable, | 132 ngx_http_geoip_city_variable, |
129 offsetof(GeoIPRecord, region), 0, 0 }, | 133 offsetof(GeoIPRecord, region), 0, 0 }, |
130 | 134 |
135 { ngx_string("geoip_region_name"), NULL, | |
136 ngx_http_geoip_region_name_variable, | |
137 0, 0, 0 }, | |
138 | |
131 { ngx_string("geoip_city"), NULL, | 139 { ngx_string("geoip_city"), NULL, |
132 ngx_http_geoip_city_variable, | 140 ngx_http_geoip_city_variable, |
133 offsetof(GeoIPRecord, city), 0, 0 }, | 141 offsetof(GeoIPRecord, city), 0, 0 }, |
134 | 142 |
135 { ngx_string("geoip_postal_code"), NULL, | 143 { ngx_string("geoip_postal_code"), NULL, |
141 offsetof(GeoIPRecord, latitude), 0, 0 }, | 149 offsetof(GeoIPRecord, latitude), 0, 0 }, |
142 | 150 |
143 { ngx_string("geoip_longitude"), NULL, | 151 { ngx_string("geoip_longitude"), NULL, |
144 ngx_http_geoip_city_float_variable, | 152 ngx_http_geoip_city_float_variable, |
145 offsetof(GeoIPRecord, longitude), 0, 0 }, | 153 offsetof(GeoIPRecord, longitude), 0, 0 }, |
154 | |
155 { ngx_string("geoip_dma_code"), NULL, | |
156 ngx_http_geoip_city_int_variable, | |
157 offsetof(GeoIPRecord, dma_code), 0, 0 }, | |
158 | |
159 { ngx_string("geoip_area_code"), NULL, | |
160 ngx_http_geoip_city_int_variable, | |
161 offsetof(GeoIPRecord, area_code), 0, 0 }, | |
146 | 162 |
147 { ngx_null_string, NULL, NULL, 0, 0, 0 } | 163 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
148 }; | 164 }; |
149 | 165 |
150 | 166 |
213 goto no_value; | 229 goto no_value; |
214 } | 230 } |
215 | 231 |
216 len = ngx_strlen(val); | 232 len = ngx_strlen(val); |
217 v->data = ngx_pnalloc(r->pool, len); | 233 v->data = ngx_pnalloc(r->pool, len); |
218 | |
219 if (v->data == NULL) { | 234 if (v->data == NULL) { |
220 GeoIPRecord_delete(gr); | 235 GeoIPRecord_delete(gr); |
221 return NGX_ERROR; | 236 return NGX_ERROR; |
222 } | 237 } |
223 | 238 |
243 return NGX_OK; | 258 return NGX_OK; |
244 } | 259 } |
245 | 260 |
246 | 261 |
247 static ngx_int_t | 262 static ngx_int_t |
263 ngx_http_geoip_region_name_variable(ngx_http_request_t *r, | |
264 ngx_http_variable_value_t *v, uintptr_t data) | |
265 { | |
266 size_t len; | |
267 const char *val; | |
268 GeoIPRecord *gr; | |
269 | |
270 gr = ngx_http_geoip_get_city_record(r); | |
271 if (gr == NULL) { | |
272 goto not_found; | |
273 } | |
274 | |
275 val = GeoIP_region_name_by_code(gr->country_code, gr->region); | |
276 | |
277 GeoIPRecord_delete(gr); | |
278 | |
279 if (val == NULL) { | |
280 goto not_found; | |
281 } | |
282 | |
283 len = ngx_strlen(val); | |
284 v->data = ngx_pnalloc(r->pool, len); | |
285 if (v->data == NULL) { | |
286 return NGX_ERROR; | |
287 } | |
288 | |
289 ngx_memcpy(v->data, val, len); | |
290 | |
291 v->len = len; | |
292 v->valid = 1; | |
293 v->no_cacheable = 0; | |
294 v->not_found = 0; | |
295 | |
296 return NGX_OK; | |
297 | |
298 not_found: | |
299 | |
300 v->not_found = 1; | |
301 | |
302 return NGX_OK; | |
303 } | |
304 | |
305 | |
306 static ngx_int_t | |
248 ngx_http_geoip_city_float_variable(ngx_http_request_t *r, | 307 ngx_http_geoip_city_float_variable(ngx_http_request_t *r, |
249 ngx_http_variable_value_t *v, uintptr_t data) | 308 ngx_http_variable_value_t *v, uintptr_t data) |
250 { | 309 { |
251 float val; | 310 float val; |
252 GeoIPRecord *gr; | 311 GeoIPRecord *gr; |
264 } | 323 } |
265 | 324 |
266 val = *(float *) ((char *) gr + data); | 325 val = *(float *) ((char *) gr + data); |
267 | 326 |
268 v->len = ngx_sprintf(v->data, "%.4f", val) - v->data; | 327 v->len = ngx_sprintf(v->data, "%.4f", val) - v->data; |
328 | |
329 GeoIPRecord_delete(gr); | |
330 | |
331 return NGX_OK; | |
332 } | |
333 | |
334 | |
335 static ngx_int_t | |
336 ngx_http_geoip_city_int_variable(ngx_http_request_t *r, | |
337 ngx_http_variable_value_t *v, uintptr_t data) | |
338 { | |
339 int val; | |
340 GeoIPRecord *gr; | |
341 | |
342 gr = ngx_http_geoip_get_city_record(r); | |
343 if (gr == NULL) { | |
344 v->not_found = 1; | |
345 return NGX_OK; | |
346 } | |
347 | |
348 v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN); | |
349 if (v->data == NULL) { | |
350 GeoIPRecord_delete(gr); | |
351 return NGX_ERROR; | |
352 } | |
353 | |
354 val = *(int *) ((char *) gr + data); | |
355 | |
356 v->len = ngx_sprintf(v->data, "%d", val) - v->data; | |
269 | 357 |
270 GeoIPRecord_delete(gr); | 358 GeoIPRecord_delete(gr); |
271 | 359 |
272 return NGX_OK; | 360 return NGX_OK; |
273 } | 361 } |
356 "GeoIO_open(\"%V\") failed", &value[1]); | 444 "GeoIO_open(\"%V\") failed", &value[1]); |
357 | 445 |
358 return NGX_CONF_ERROR; | 446 return NGX_CONF_ERROR; |
359 } | 447 } |
360 | 448 |
449 if (cf->args->nelts == 3) { | |
450 if (ngx_strcmp(value[2].data, "utf8") == 0) { | |
451 GeoIP_set_charset (gcf->country, GEOIP_CHARSET_UTF8); | |
452 | |
453 } else { | |
454 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
455 "invalid parameter \"%V\"", &value[2]); | |
456 return NGX_CONF_ERROR; | |
457 } | |
458 } | |
459 | |
361 switch (gcf->country->databaseType) { | 460 switch (gcf->country->databaseType) { |
362 | 461 |
363 case GEOIP_COUNTRY_EDITION: | 462 case GEOIP_COUNTRY_EDITION: |
364 case GEOIP_PROXY_EDITION: | 463 case GEOIP_PROXY_EDITION: |
365 case GEOIP_NETSPEED_EDITION: | 464 case GEOIP_NETSPEED_EDITION: |
395 "GeoIO_open(\"%V\") failed", &value[1]); | 494 "GeoIO_open(\"%V\") failed", &value[1]); |
396 | 495 |
397 return NGX_CONF_ERROR; | 496 return NGX_CONF_ERROR; |
398 } | 497 } |
399 | 498 |
499 if (cf->args->nelts == 3) { | |
500 if (ngx_strcmp(value[2].data, "utf8") == 0) { | |
501 GeoIP_set_charset (gcf->city, GEOIP_CHARSET_UTF8); | |
502 | |
503 } else { | |
504 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
505 "invalid parameter \"%V\"", &value[2]); | |
506 return NGX_CONF_ERROR; | |
507 } | |
508 } | |
509 | |
400 switch (gcf->city->databaseType) { | 510 switch (gcf->city->databaseType) { |
401 | 511 |
402 case GEOIP_CITY_EDITION_REV0: | 512 case GEOIP_CITY_EDITION_REV0: |
403 case GEOIP_CITY_EDITION_REV1: | 513 case GEOIP_CITY_EDITION_REV1: |
404 | 514 |