annotate src/http/modules/ngx_http_geoip_module.c @ 2985:31af2d1a742e

ngx_http_geoip_module
author Igor Sysoev <igor@sysoev.ru>
date Mon, 20 Jul 2009 07:10:43 +0000
parents
children 98a8336c5b7c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
2 /*
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4 */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
5
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
6
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
7 #include <ngx_config.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
8 #include <ngx_core.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
9 #include <ngx_http.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
10
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
11 #include <GeoIP.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
12 #include <GeoIPCity.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
13
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
14
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
15 typedef struct {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
16 GeoIP *country;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
17 GeoIP *city;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
18 } ngx_http_geoip_conf_t;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
19
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
20
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
21 typedef struct {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
22 ngx_str_t *name;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
23 uintptr_t data;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
24 } ngx_http_geoip_var_t;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
25
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
26
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
27 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
28
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
29 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
30 ngx_http_variable_value_t *v, uintptr_t data);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
31 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
32 ngx_http_variable_value_t *v, uintptr_t data);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
33
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
34 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
35 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
36 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
37 void *conf);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
38 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
39 void *conf);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
40 static void ngx_http_geoip_cleanup(void *data);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
41
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
42
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
43 static ngx_command_t ngx_http_geoip_commands[] = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
44
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
45 { ngx_string("geoip_country"),
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
46 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
47 ngx_http_geoip_country,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
48 NGX_HTTP_MAIN_CONF_OFFSET,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
49 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
50 NULL },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
51
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
52 { ngx_string("geoip_city"),
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
53 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
54 ngx_http_geoip_city,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
55 NGX_HTTP_MAIN_CONF_OFFSET,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
56 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
57 NULL },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
58
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
59 ngx_null_command
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
60 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
61
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
62
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
63 static ngx_http_module_t ngx_http_geoip_module_ctx = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
64 ngx_http_geoip_add_variables, /* preconfiguration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
65 NULL, /* postconfiguration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
66
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
67 ngx_http_geoip_create_conf, /* create main configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
68 NULL, /* init main configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
69
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
70 NULL, /* create server configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
71 NULL, /* merge server configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
72
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
73 NULL, /* create location configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
74 NULL /* merge location configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
75 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
76
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
77
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
78 ngx_module_t ngx_http_geoip_module = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
79 NGX_MODULE_V1,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
80 &ngx_http_geoip_module_ctx, /* module context */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
81 ngx_http_geoip_commands, /* module directives */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
82 NGX_HTTP_MODULE, /* module type */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
83 NULL, /* init master */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
84 NULL, /* init module */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
85 NULL, /* init process */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
86 NULL, /* init thread */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
87 NULL, /* exit thread */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
88 NULL, /* exit process */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
89 NULL, /* exit master */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
90 NGX_MODULE_V1_PADDING
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
91 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
92
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
93
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
94 static ngx_http_variable_t ngx_http_geoip_vars[] = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
95
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
96 { ngx_string("geoip_country_code"), NULL, ngx_http_geoip_country_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
97 (uintptr_t) GeoIP_country_code_by_ipnum, 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
98
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
99 { ngx_string("geoip_country_code3"), NULL, ngx_http_geoip_country_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
100 (uintptr_t) GeoIP_country_code3_by_ipnum, 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
101
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
102 { ngx_string("geoip_country_name"), NULL, ngx_http_geoip_country_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
103 (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
104
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
105 { ngx_string("geoip_city_country_code"), NULL, ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
106 offsetof(GeoIPRecord, country_code), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
107
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
108 { ngx_string("geoip_city_country_code3"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
109 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
110 offsetof(GeoIPRecord, country_code3), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
111
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
112 { ngx_string("geoip_city_country_name"), NULL, ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
113 offsetof(GeoIPRecord, country_name), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
114
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
115 { ngx_string("geoip_region"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
116 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
117 offsetof(GeoIPRecord, region), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
118
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
119 { ngx_string("geoip_city"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
120 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
121 offsetof(GeoIPRecord, city), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
122
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
123 { ngx_string("geoip_postal_code"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
124 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
125 offsetof(GeoIPRecord, postal_code), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
126
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
127 { ngx_null_string, NULL, NULL, 0, 0, 0 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
128 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
129
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
130
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
131 static ngx_int_t
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
132 ngx_http_geoip_country_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
133 ngx_http_variable_value_t *v, uintptr_t data)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
134 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
135 ngx_http_geoip_variable_handler_pt handler =
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
136 (ngx_http_geoip_variable_handler_pt) data;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
137
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
138 u_long addr;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
139 const char *val;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
140 struct sockaddr_in *sin;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
141 ngx_http_geoip_conf_t *gcf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
142
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
143 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
144
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
145 if (gcf->country == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
146 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
147 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
148
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
149 if (r->connection->sockaddr->sa_family != AF_INET) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
150 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
151 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
152
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
153 sin = (struct sockaddr_in *) r->connection->sockaddr;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
154 addr = ntohl(sin->sin_addr.s_addr);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
155
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
156 val = handler(gcf->country, addr);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
157
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
158 if (val == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
159 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
160 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
161
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
162 v->len = ngx_strlen(val);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
163 v->valid = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
164 v->no_cacheable = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
165 v->not_found = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
166 v->data = (u_char *) val;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
167
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
168 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
169
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
170 not_found:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
171
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
172 v->not_found = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
173
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
174 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
175 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
176
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
177
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
178 static ngx_int_t
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
179 ngx_http_geoip_city_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
180 ngx_http_variable_value_t *v, uintptr_t data)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
181 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
182 u_long addr;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
183 char *val;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
184 GeoIPRecord *gr;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
185 struct sockaddr_in *sin;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
186 ngx_http_geoip_conf_t *gcf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
187
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
188 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
189
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
190 if (gcf->city == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
191 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
192 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
193
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
194 if (r->connection->sockaddr->sa_family != AF_INET) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
195 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
196 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
197
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
198 sin = (struct sockaddr_in *) r->connection->sockaddr;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
199 addr = ntohl(sin->sin_addr.s_addr);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
200
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
201 gr = GeoIP_record_by_ipnum(gcf->city, addr);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
202
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
203 if (gr == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
204 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
205 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
206
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
207 val = *(char **) ((char *) gr + data);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
208
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
209 if (val == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
210 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
211 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
212
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
213 v->len = ngx_strlen(val);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
214 v->valid = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
215 v->no_cacheable = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
216 v->not_found = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
217 v->data = (u_char *) val;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
218
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
219 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
220
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
221 not_found:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
222
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
223 v->not_found = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
224
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
225 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
226 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
227
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
228
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
229 static ngx_int_t
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
230 ngx_http_geoip_add_variables(ngx_conf_t *cf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
231 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
232 ngx_http_variable_t *var, *v;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
233
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
234 for (v = ngx_http_geoip_vars; v->name.len; v++) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
235 var = ngx_http_add_variable(cf, &v->name, v->flags);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
236 if (var == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
237 return NGX_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
238 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
239
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
240 var->get_handler = v->get_handler;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
241 var->data = v->data;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
242 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
243
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
244 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
245 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
246
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
247
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
248 static void *
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
249 ngx_http_geoip_create_conf(ngx_conf_t *cf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
250 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
251 ngx_pool_cleanup_t *cln;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
252 ngx_http_geoip_conf_t *conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
253
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
254 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip_conf_t));
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
255 if (conf == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
256 return NULL;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
257 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
258
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
259 cln = ngx_pool_cleanup_add(cf->pool, 0);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
260 if (cln == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
261 return NULL;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
262 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
263
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
264 cln->handler = ngx_http_geoip_cleanup;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
265 cln->data = conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
266
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
267 return conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
268 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
269
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
270
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
271 static char *
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
272 ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
273 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
274 ngx_http_geoip_conf_t *gcf = conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
275
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
276 ngx_str_t *value;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
277
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
278 if (gcf->country) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
279 return "is duplicate";
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
280 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
281
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
282 value = cf->args->elts;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
283
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
284 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
285
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
286 if (gcf->country == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
287 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
288 "GeoIO_open(\"%V\") failed", &value[1]);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
289
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
290 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
291 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
292
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
293 switch (gcf->country->databaseType) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
294
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
295 case GEOIP_COUNTRY_EDITION:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
296 case GEOIP_PROXY_EDITION:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
297 case GEOIP_NETSPEED_EDITION:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
298
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
299 return NGX_CONF_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
300
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
301 default:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
302 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
303 "invalid GeoIP database \"%V\" type:%d",
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
304 &value[1], gcf->country->databaseType);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
305 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
306 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
307 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
308
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
309
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
310 static char *
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
311 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
312 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
313 ngx_http_geoip_conf_t *gcf = conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
314
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
315 ngx_str_t *value;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
316
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
317 if (gcf->city) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
318 return "is duplicate";
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
319 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
320
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
321 value = cf->args->elts;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
322
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
323 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
324
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
325 if (gcf->city == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
326 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
327 "GeoIO_open(\"%V\") failed", &value[1]);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
328
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
329 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
330 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
331
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
332 switch (gcf->city->databaseType) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
333
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
334 case GEOIP_CITY_EDITION_REV0:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
335 case GEOIP_CITY_EDITION_REV1:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
336
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
337 return NGX_CONF_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
338
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
339 default:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
340 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
341 "invalid GeoIP City database \"%V\" type:%d",
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
342 &value[1], gcf->city->databaseType);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
343 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
344 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
345 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
346
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
347
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
348 static void
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
349 ngx_http_geoip_cleanup(void *data)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
350 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
351 ngx_http_geoip_conf_t *gcf = data;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
352
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
353 if (gcf->country) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
354 GeoIP_delete(gcf->country);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
355 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
356
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
357 if (gcf->city) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
358 GeoIP_delete(gcf->city);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
359 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
360 }