annotate src/http/modules/ngx_http_geoip_module.c @ 502:89dc5654117c NGINX_0_7_63

nginx 0.7.63 *) Security: now "/../" are disabled in "Destination" request header line. *) Change: minimum supported OpenSSL version is 0.9.7. *) Change: the "ask" parameter of the "ssl_verify_client" directive was changed to the "optional" parameter and now it checks a client certificate if it was offered. Thanks to Brice Figureau. *) Feature: now the "-V" switch shows TLS SNI support. *) Feature: the $ssl_client_verify variable. Thanks to Brice Figureau. *) Feature: the "ssl_crl" directive. Thanks to Brice Figureau. *) Bugfix: the $ssl_client_cert variable usage corrupted memory; the bug had appeared in 0.7.7. Thanks to Sergey Zhuravlev. *) Feature: now the start cache loader runs in a separate process; this should improve large caches handling. *) Feature: now temporary files and permanent storage area may reside at different file systems. *) Bugfix: nginx counted incorrectly disk cache size. *) Change: now directive "gzip_disable msie6" does not disable gzipping for MSIE 6.0 SV1. *) Bugfix: nginx always added "Vary: Accept-Encoding" response header line, if both "gzip_static" and "gzip_vary" were on. *) Feature: the "proxy" parameter of the "geo" directive. *) Feature: the ngx_http_geoip_module. *) Feature: the "limit_rate_after" directive. Thanks to Ivan Debnar. *) Feature: the "limit_req_log_level" and "limit_conn_log_level" directives. *) Bugfix: now "limit_req" directive conforms to the leaky bucket algorithm. Thanks to Maxim Dounin. *) Bugfix: in ngx_http_limit_req_module. Thanks to Maxim Dounin. *) Bugfix: now nginx allows underscores in a request method. *) Bugfix: "proxy_pass_header" and "fastcgi_pass_header" directives did not pass to a client the "X-Accel-Redirect", "X-Accel-Limit-Rate", "X-Accel-Buffering", and "X-Accel-Charset" lines from backend response header. Thanks to Maxim Dounin. *) Bugfix: in handling "Last-Modified" and "Accept-Ranges" backend response header lines; the bug had appeared in 0.7.44. Thanks to Maxim Dounin. *) Feature: the "image_filter_transparency" directive. *) Feature: the "image_filter" directive supports variables for setting size. *) Bugfix: in PNG alpha-channel support in the ngx_http_image_filter_module. *) Bugfix: in transparency support in the ngx_http_image_filter_module. *) Feature: now several "perl_modules" directives may be used. *) Bugfix: ngx_http_perl_module responses did not work in subrequests. *) Bugfix: nginx sent '\0' in a "Location" response header line on MKCOL request. Thanks to Xie Zhenye. *) Bugfix: an "error_page" directive did not redirect a 413 error; the bug had appeared in 0.6.10. *) Bugfix: in memory allocation error handling. Thanks to Maxim Dounin and Kirill A. Korinskiy.
author Igor Sysoev <http://sysoev.ru>
date Mon, 26 Oct 2009 00:00:00 +0300
parents
children 68c0ae0a4959
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
502
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
1
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
2 /*
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
4 */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
5
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
6
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
7 #include <ngx_config.h>
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
8 #include <ngx_core.h>
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
9 #include <ngx_http.h>
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
10
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
11 #include <GeoIP.h>
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
12 #include <GeoIPCity.h>
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
13
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
14
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
15 typedef struct {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
16 GeoIP *country;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
17 GeoIP *city;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
18 } ngx_http_geoip_conf_t;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
19
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
20
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
21 typedef struct {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
22 ngx_str_t *name;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
23 uintptr_t data;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
24 } ngx_http_geoip_var_t;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
25
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
26
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
27 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
28
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
29 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
30 ngx_http_variable_value_t *v, uintptr_t data);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
31 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
32 ngx_http_variable_value_t *v, uintptr_t data);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
33
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
34 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
35 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
36 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
37 void *conf);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
38 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
39 void *conf);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
40 static void ngx_http_geoip_cleanup(void *data);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
41
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
42
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
43 static ngx_command_t ngx_http_geoip_commands[] = {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
44
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
45 { ngx_string("geoip_country"),
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
46 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
47 ngx_http_geoip_country,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
48 NGX_HTTP_MAIN_CONF_OFFSET,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
49 0,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
50 NULL },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
51
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
52 { ngx_string("geoip_city"),
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
53 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
54 ngx_http_geoip_city,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
55 NGX_HTTP_MAIN_CONF_OFFSET,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
56 0,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
57 NULL },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
58
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
59 ngx_null_command
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
60 };
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
61
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
62
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
63 static ngx_http_module_t ngx_http_geoip_module_ctx = {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
64 ngx_http_geoip_add_variables, /* preconfiguration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
65 NULL, /* postconfiguration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
66
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
67 ngx_http_geoip_create_conf, /* create main configuration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
68 NULL, /* init main configuration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
69
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
70 NULL, /* create server configuration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
71 NULL, /* merge server configuration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
72
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
73 NULL, /* create location configuration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
74 NULL /* merge location configuration */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
75 };
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
76
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
77
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
78 ngx_module_t ngx_http_geoip_module = {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
79 NGX_MODULE_V1,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
80 &ngx_http_geoip_module_ctx, /* module context */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
81 ngx_http_geoip_commands, /* module directives */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
82 NGX_HTTP_MODULE, /* module type */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
83 NULL, /* init master */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
84 NULL, /* init module */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
85 NULL, /* init process */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
86 NULL, /* init thread */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
87 NULL, /* exit thread */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
88 NULL, /* exit process */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
89 NULL, /* exit master */
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
90 NGX_MODULE_V1_PADDING
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
91 };
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
92
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
93
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
94 static ngx_http_variable_t ngx_http_geoip_vars[] = {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
95
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
96 { ngx_string("geoip_country_code"), NULL, ngx_http_geoip_country_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
97 (uintptr_t) GeoIP_country_code_by_ipnum, 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
98
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
99 { ngx_string("geoip_country_code3"), NULL, ngx_http_geoip_country_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
100 (uintptr_t) GeoIP_country_code3_by_ipnum, 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
101
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
102 { ngx_string("geoip_country_name"), NULL, ngx_http_geoip_country_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
103 (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
104
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
105 { ngx_string("geoip_city_country_code"), NULL, ngx_http_geoip_city_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
106 offsetof(GeoIPRecord, country_code), 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
107
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
108 { ngx_string("geoip_city_country_code3"), NULL,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
109 ngx_http_geoip_city_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
110 offsetof(GeoIPRecord, country_code3), 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
111
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
112 { ngx_string("geoip_city_country_name"), NULL, ngx_http_geoip_city_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
113 offsetof(GeoIPRecord, country_name), 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
114
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
115 { ngx_string("geoip_region"), NULL,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
116 ngx_http_geoip_city_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
117 offsetof(GeoIPRecord, region), 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
118
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
119 { ngx_string("geoip_city"), NULL,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
120 ngx_http_geoip_city_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
121 offsetof(GeoIPRecord, city), 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
122
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
123 { ngx_string("geoip_postal_code"), NULL,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
124 ngx_http_geoip_city_variable,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
125 offsetof(GeoIPRecord, postal_code), 0, 0 },
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
126
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
127 { ngx_null_string, NULL, NULL, 0, 0, 0 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
128 };
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
129
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
130
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
131 static ngx_int_t
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
132 ngx_http_geoip_country_variable(ngx_http_request_t *r,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
133 ngx_http_variable_value_t *v, uintptr_t data)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
134 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
135 ngx_http_geoip_variable_handler_pt handler =
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
136 (ngx_http_geoip_variable_handler_pt) data;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
137
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
138 u_long addr;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
139 const char *val;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
140 struct sockaddr_in *sin;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
141 ngx_http_geoip_conf_t *gcf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
142
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
143 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
144
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
145 if (gcf->country == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
146 goto not_found;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
147 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
148
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
149 if (r->connection->sockaddr->sa_family != AF_INET) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
150 goto not_found;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
151 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
152
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
153 sin = (struct sockaddr_in *) r->connection->sockaddr;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
154 addr = ntohl(sin->sin_addr.s_addr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
155
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
156 val = handler(gcf->country, addr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
157
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
158 if (val == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
159 goto not_found;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
160 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
161
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
162 v->len = ngx_strlen(val);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
163 v->valid = 1;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
164 v->no_cacheable = 0;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
165 v->not_found = 0;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
166 v->data = (u_char *) val;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
167
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
168 return NGX_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
169
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
170 not_found:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
171
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
172 v->not_found = 1;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
173
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
174 return NGX_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
175 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
176
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
177
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
178 static ngx_int_t
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
179 ngx_http_geoip_city_variable(ngx_http_request_t *r,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
180 ngx_http_variable_value_t *v, uintptr_t data)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
181 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
182 u_long addr;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
183 char *val;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
184 size_t len;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
185 GeoIPRecord *gr;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
186 struct sockaddr_in *sin;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
187 ngx_http_geoip_conf_t *gcf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
188
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
189 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
190
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
191 if (gcf->city == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
192 goto not_found;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
193 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
194
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
195 if (r->connection->sockaddr->sa_family != AF_INET) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
196 goto not_found;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
197 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
198
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
199 sin = (struct sockaddr_in *) r->connection->sockaddr;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
200 addr = ntohl(sin->sin_addr.s_addr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
201
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
202 gr = GeoIP_record_by_ipnum(gcf->city, addr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
203
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
204 if (gr == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
205 goto not_found;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
206 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
207
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
208 val = *(char **) ((char *) gr + data);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
209
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
210 if (val == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
211 goto no_value;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
212 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
213
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
214 len = ngx_strlen(val);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
215 v->data = ngx_pnalloc(r->pool, len);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
216
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
217 if (v->data == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
218 GeoIPRecord_delete(gr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
219 return NGX_ERROR;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
220 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
221
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
222 ngx_memcpy(v->data, val, len);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
223
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
224 v->len = len;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
225 v->valid = 1;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
226 v->no_cacheable = 0;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
227 v->not_found = 0;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
228
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
229 GeoIPRecord_delete(gr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
230
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
231 return NGX_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
232
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
233 no_value:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
234
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
235 GeoIPRecord_delete(gr);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
236
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
237 not_found:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
238
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
239 v->not_found = 1;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
240
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
241 return NGX_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
242 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
243
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
244
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
245 static ngx_int_t
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
246 ngx_http_geoip_add_variables(ngx_conf_t *cf)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
247 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
248 ngx_http_variable_t *var, *v;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
249
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
250 for (v = ngx_http_geoip_vars; v->name.len; v++) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
251 var = ngx_http_add_variable(cf, &v->name, v->flags);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
252 if (var == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
253 return NGX_ERROR;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
254 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
255
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
256 var->get_handler = v->get_handler;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
257 var->data = v->data;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
258 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
259
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
260 return NGX_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
261 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
262
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
263
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
264 static void *
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
265 ngx_http_geoip_create_conf(ngx_conf_t *cf)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
266 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
267 ngx_pool_cleanup_t *cln;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
268 ngx_http_geoip_conf_t *conf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
269
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
270 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip_conf_t));
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
271 if (conf == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
272 return NULL;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
273 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
274
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
275 cln = ngx_pool_cleanup_add(cf->pool, 0);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
276 if (cln == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
277 return NULL;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
278 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
279
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
280 cln->handler = ngx_http_geoip_cleanup;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
281 cln->data = conf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
282
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
283 return conf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
284 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
285
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
286
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
287 static char *
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
288 ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
289 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
290 ngx_http_geoip_conf_t *gcf = conf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
291
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
292 ngx_str_t *value;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
293
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
294 if (gcf->country) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
295 return "is duplicate";
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
296 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
297
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
298 value = cf->args->elts;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
299
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
300 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
301
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
302 if (gcf->country == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
303 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
304 "GeoIO_open(\"%V\") failed", &value[1]);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
305
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
306 return NGX_CONF_ERROR;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
307 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
308
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
309 switch (gcf->country->databaseType) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
310
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
311 case GEOIP_COUNTRY_EDITION:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
312 case GEOIP_PROXY_EDITION:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
313 case GEOIP_NETSPEED_EDITION:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
314
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
315 return NGX_CONF_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
316
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
317 default:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
318 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
319 "invalid GeoIP database \"%V\" type:%d",
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
320 &value[1], gcf->country->databaseType);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
321 return NGX_CONF_ERROR;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
322 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
323 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
324
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
325
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
326 static char *
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
327 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
328 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
329 ngx_http_geoip_conf_t *gcf = conf;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
330
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
331 ngx_str_t *value;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
332
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
333 if (gcf->city) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
334 return "is duplicate";
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
335 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
336
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
337 value = cf->args->elts;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
338
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
339 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
340
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
341 if (gcf->city == NULL) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
342 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
343 "GeoIO_open(\"%V\") failed", &value[1]);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
344
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
345 return NGX_CONF_ERROR;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
346 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
347
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
348 switch (gcf->city->databaseType) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
349
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
350 case GEOIP_CITY_EDITION_REV0:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
351 case GEOIP_CITY_EDITION_REV1:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
352
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
353 return NGX_CONF_OK;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
354
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
355 default:
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
356 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
357 "invalid GeoIP City database \"%V\" type:%d",
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
358 &value[1], gcf->city->databaseType);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
359 return NGX_CONF_ERROR;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
360 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
361 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
362
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
363
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
364 static void
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
365 ngx_http_geoip_cleanup(void *data)
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
366 {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
367 ngx_http_geoip_conf_t *gcf = data;
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
368
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
369 if (gcf->country) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
370 GeoIP_delete(gcf->country);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
371 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
372
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
373 if (gcf->city) {
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
374 GeoIP_delete(gcf->city);
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
375 }
89dc5654117c nginx 0.7.63
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
376 }