2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2010-06-24 23:26:05 +08:00
|
|
|
ngx_http_variable_value_t *value;
|
2008-12-11 17:46:45 +08:00
|
|
|
u_short start;
|
|
|
|
u_short end;
|
2008-11-25 23:59:06 +08:00
|
|
|
} ngx_http_geo_range_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2010-06-30 00:06:20 +08:00
|
|
|
ngx_http_geo_range_t **low;
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_variable_value_t *default_value;
|
2008-11-25 23:59:06 +08:00
|
|
|
} ngx_http_geo_high_ranges_t;
|
|
|
|
|
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
typedef struct {
|
|
|
|
ngx_str_node_t sn;
|
|
|
|
ngx_http_variable_value_t *value;
|
2010-06-30 00:06:20 +08:00
|
|
|
size_t offset;
|
2010-06-23 23:31:33 +08:00
|
|
|
} ngx_http_geo_variable_value_node_t;
|
|
|
|
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
typedef struct {
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_variable_value_t *value;
|
|
|
|
ngx_str_t *net;
|
2010-06-30 00:06:20 +08:00
|
|
|
ngx_http_geo_high_ranges_t high;
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_radix_tree_t *tree;
|
|
|
|
ngx_rbtree_t rbtree;
|
|
|
|
ngx_rbtree_node_t sentinel;
|
2009-07-22 17:43:14 +08:00
|
|
|
ngx_array_t *proxies;
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_pool_t *pool;
|
|
|
|
ngx_pool_t *temp_pool;
|
2010-06-30 00:06:20 +08:00
|
|
|
|
|
|
|
size_t data_size;
|
|
|
|
|
|
|
|
ngx_str_t include_name;
|
|
|
|
ngx_uint_t includes;
|
|
|
|
ngx_uint_t entries;
|
|
|
|
|
|
|
|
unsigned ranges:1;
|
|
|
|
unsigned outside_entries:1;
|
|
|
|
unsigned allow_binary_include:1;
|
|
|
|
unsigned binary_include:1;
|
2005-12-16 23:07:08 +08:00
|
|
|
} ngx_http_geo_conf_ctx_t;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
ngx_radix_tree_t *tree;
|
2010-06-30 00:06:20 +08:00
|
|
|
ngx_http_geo_high_ranges_t high;
|
2008-12-11 17:46:45 +08:00
|
|
|
} u;
|
|
|
|
|
2009-07-22 17:43:14 +08:00
|
|
|
ngx_array_t *proxies;
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_int_t index;
|
|
|
|
} ngx_http_geo_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
static in_addr_t ngx_http_geo_addr(ngx_http_request_t *r,
|
|
|
|
ngx_http_geo_ctx_t *ctx);
|
2009-07-22 17:43:14 +08:00
|
|
|
static in_addr_t ngx_http_geo_real_addr(ngx_http_request_t *r,
|
|
|
|
ngx_http_geo_ctx_t *ctx);
|
2005-02-04 03:33:37 +08:00
|
|
|
static char *ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
|
|
static char *ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
|
2008-11-25 23:59:06 +08:00
|
|
|
static char *ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *value);
|
|
|
|
static char *ngx_http_geo_add_range(ngx_conf_t *cf,
|
|
|
|
ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
|
|
|
|
static ngx_uint_t ngx_http_geo_delete_range(ngx_conf_t *cf,
|
|
|
|
ngx_http_geo_conf_ctx_t *ctx, in_addr_t start, in_addr_t end);
|
|
|
|
static char *ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *value);
|
|
|
|
static ngx_http_variable_value_t *ngx_http_geo_value(ngx_conf_t *cf,
|
|
|
|
ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *value);
|
2009-07-22 17:43:14 +08:00
|
|
|
static char *ngx_http_geo_add_proxy(ngx_conf_t *cf,
|
|
|
|
ngx_http_geo_conf_ctx_t *ctx, ngx_cidr_t *cidr);
|
|
|
|
static ngx_int_t ngx_http_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
|
|
|
|
ngx_cidr_t *cidr);
|
2010-06-30 00:06:20 +08:00
|
|
|
static char *ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *name);
|
|
|
|
static ngx_int_t ngx_http_geo_include_binary_base(ngx_conf_t *cf,
|
|
|
|
ngx_http_geo_conf_ctx_t *ctx, ngx_str_t *name);
|
|
|
|
static void ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx);
|
|
|
|
static u_char *ngx_http_geo_copy_values(u_char *base, u_char *p,
|
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_geo_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("geo"),
|
2008-12-11 17:46:45 +08:00
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
|
2005-02-04 03:33:37 +08:00
|
|
|
ngx_http_geo_block,
|
|
|
|
NGX_HTTP_MAIN_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_geo_module_ctx = {
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
NULL, /* preconfiguration */
|
|
|
|
NULL, /* postconfiguration */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
NULL, /* create location configuration */
|
|
|
|
NULL /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_geo_module = {
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
NGX_MODULE_V1,
|
2005-02-04 03:33:37 +08:00
|
|
|
&ngx_http_geo_module_ctx, /* module context */
|
|
|
|
ngx_http_geo_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
2005-09-08 22:36:09 +08:00
|
|
|
NULL, /* init master */
|
2005-02-04 03:33:37 +08:00
|
|
|
NULL, /* init module */
|
2005-09-08 22:36:09 +08:00
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
2005-02-04 03:33:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
typedef struct {
|
|
|
|
u_char GEORNG[6];
|
|
|
|
u_char version;
|
|
|
|
u_char ptr_size;
|
|
|
|
uint32_t endianess;
|
|
|
|
uint32_t crc32;
|
|
|
|
} ngx_http_geo_header_t;
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_geo_header_t ngx_http_geo_header = {
|
|
|
|
{ 'G', 'E', 'O', 'R', 'N', 'G' }, 0, sizeof(void *), 0x12345678, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-04 03:33:37 +08:00
|
|
|
/* AF_INET only */
|
|
|
|
|
nginx-0.3.8-RELEASE import
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; the bug had
appeared in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
2005-11-10 01:25:55 +08:00
|
|
|
static ngx_int_t
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_http_geo_cidr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
nginx-0.3.8-RELEASE import
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; the bug had
appeared in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
2005-11-10 01:25:55 +08:00
|
|
|
uintptr_t data)
|
2005-02-04 03:33:37 +08:00
|
|
|
{
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
nginx-0.3.8-RELEASE import
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; the bug had
appeared in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
2005-11-10 01:25:55 +08:00
|
|
|
ngx_http_variable_value_t *vv;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
nginx-0.3.8-RELEASE import
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; the bug had
appeared in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
2005-11-10 01:25:55 +08:00
|
|
|
vv = (ngx_http_variable_value_t *)
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_radix32tree_find(ctx->u.tree, ngx_http_geo_addr(r, ctx));
|
2005-02-16 21:40:36 +08:00
|
|
|
|
nginx-0.3.8-RELEASE import
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; the bug had
appeared in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
2005-11-10 01:25:55 +08:00
|
|
|
*v = *vv;
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http geo: %v", v);
|
2005-02-16 21:40:36 +08:00
|
|
|
|
nginx-0.3.8-RELEASE import
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; the bug had
appeared in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
2005-11-10 01:25:55 +08:00
|
|
|
return NGX_OK;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_geo_range_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
|
|
uintptr_t data)
|
|
|
|
{
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_geo_ctx_t *ctx = (ngx_http_geo_ctx_t *) data;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
in_addr_t addr;
|
2010-06-24 23:26:05 +08:00
|
|
|
ngx_uint_t n;
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_http_geo_range_t *range;
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
*v = *ctx->u.high.default_value;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
addr = ngx_http_geo_addr(r, ctx);
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
range = ctx->u.high.low[addr >> 16];
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-24 23:26:05 +08:00
|
|
|
if (range) {
|
|
|
|
n = addr & 0xffff;
|
|
|
|
do {
|
|
|
|
if (n >= (ngx_uint_t) range->start && n <= (ngx_uint_t) range->end)
|
|
|
|
{
|
|
|
|
*v = *range->value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ((++range)->value);
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http geo: %v", v);
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
static in_addr_t
|
|
|
|
ngx_http_geo_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx)
|
2009-07-22 17:43:14 +08:00
|
|
|
{
|
|
|
|
u_char *p, *ip;
|
|
|
|
size_t len;
|
|
|
|
in_addr_t addr;
|
|
|
|
ngx_uint_t i, n;
|
|
|
|
ngx_in_cidr_t *proxies;
|
|
|
|
ngx_table_elt_t *xfwd;
|
|
|
|
|
|
|
|
addr = ngx_http_geo_real_addr(r, ctx);
|
|
|
|
|
|
|
|
xfwd = r->headers_in.x_forwarded_for;
|
|
|
|
|
|
|
|
if (xfwd == NULL || ctx->proxies == NULL) {
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies = ctx->proxies->elts;
|
|
|
|
n = ctx->proxies->nelts;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if ((addr & proxies[i].mask) == proxies[i].addr) {
|
|
|
|
|
|
|
|
len = xfwd->value.len;
|
|
|
|
ip = xfwd->value.data;
|
|
|
|
|
|
|
|
for (p = ip + len - 1; p > ip; p--) {
|
|
|
|
if (*p == ' ' || *p == ',') {
|
|
|
|
p++;
|
|
|
|
len -= p - ip;
|
|
|
|
ip = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntohl(ngx_inet_addr(ip, len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static in_addr_t
|
|
|
|
ngx_http_geo_real_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx)
|
2008-12-11 17:46:45 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
ngx_http_variable_value_t *v;
|
|
|
|
|
|
|
|
if (ctx->index == -1) {
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http geo started: %V", &r->connection->addr_text);
|
|
|
|
|
2009-02-21 15:02:02 +08:00
|
|
|
if (r->connection->sockaddr->sa_family != AF_INET) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
sin = (struct sockaddr_in *) r->connection->sockaddr;
|
|
|
|
return ntohl(sin->sin_addr.s_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = ngx_http_get_flushed_variable(r, ctx->index);
|
|
|
|
|
|
|
|
if (v == NULL || v->not_found) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http geo not found");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"http geo started: %v", v);
|
|
|
|
|
|
|
|
return ntohl(ngx_inet_addr(v->data, v->len));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-16 21:40:36 +08:00
|
|
|
static char *
|
|
|
|
ngx_http_geo_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
2005-02-04 03:33:37 +08:00
|
|
|
{
|
2005-12-16 23:07:08 +08:00
|
|
|
char *rv;
|
2010-06-24 23:26:05 +08:00
|
|
|
void **p;
|
2008-11-25 23:59:06 +08:00
|
|
|
size_t len;
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_str_t *value, name;
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_uint_t i;
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_conf_t save;
|
|
|
|
ngx_pool_t *pool;
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_array_t *a;
|
2007-08-20 17:57:19 +08:00
|
|
|
ngx_http_variable_t *var;
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_geo_ctx_t *geo;
|
2005-12-16 23:07:08 +08:00
|
|
|
ngx_http_geo_conf_ctx_t ctx;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
value = cf->args->elts;
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
geo = ngx_palloc(cf->pool, sizeof(ngx_http_geo_ctx_t));
|
|
|
|
if (geo == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = value[1];
|
|
|
|
name.len--;
|
|
|
|
name.data++;
|
|
|
|
|
|
|
|
if (cf->args->nelts == 3) {
|
|
|
|
|
|
|
|
geo->index = ngx_http_get_variable_index(cf, &name);
|
|
|
|
if (geo->index == NGX_ERROR) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = value[2];
|
|
|
|
name.len--;
|
|
|
|
name.data++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
geo->index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
2005-03-19 20:38:37 +08:00
|
|
|
if (var == NULL) {
|
2005-02-04 03:33:37 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
pool = ngx_create_pool(16384, cf->log);
|
|
|
|
if (pool == NULL) {
|
2005-03-19 20:38:37 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
ngx_memzero(&ctx, sizeof(ngx_http_geo_conf_ctx_t));
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
ctx.temp_pool = ngx_create_pool(16384, cf->log);
|
|
|
|
if (ctx.temp_pool == NULL) {
|
2005-02-04 03:33:37 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value);
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
ctx.pool = cf->pool;
|
2010-06-30 00:06:20 +08:00
|
|
|
ctx.data_size = sizeof(ngx_http_geo_header_t)
|
|
|
|
+ sizeof(ngx_http_variable_value_t)
|
|
|
|
+ 0x10000 * sizeof(ngx_http_geo_range_t *);
|
|
|
|
ctx.allow_binary_include = 1;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
save = *cf;
|
|
|
|
cf->pool = pool;
|
2005-12-16 23:07:08 +08:00
|
|
|
cf->ctx = &ctx;
|
2005-02-04 03:33:37 +08:00
|
|
|
cf->handler = ngx_http_geo;
|
|
|
|
cf->handler_conf = conf;
|
|
|
|
|
|
|
|
rv = ngx_conf_parse(cf, NULL);
|
|
|
|
|
|
|
|
*cf = save;
|
|
|
|
|
2009-07-22 17:43:14 +08:00
|
|
|
geo->proxies = ctx.proxies;
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ctx.high.low) {
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (!ctx.binary_include) {
|
|
|
|
for (i = 0; i < 0x10000; i++) {
|
|
|
|
a = (ngx_array_t *) ctx.high.low[i];
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (a == NULL || a->nelts == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
len = a->nelts * sizeof(ngx_http_geo_range_t);
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *));
|
|
|
|
if (ctx.high.low[i] == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = (void **) ngx_cpymem(ctx.high.low[i], a->elts, len);
|
|
|
|
*p = NULL;
|
|
|
|
ctx.data_size += len + sizeof(void *);
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ctx.allow_binary_include
|
|
|
|
&& !ctx.outside_entries
|
|
|
|
&& ctx.entries > 100000
|
|
|
|
&& ctx.includes == 1)
|
|
|
|
{
|
|
|
|
ngx_http_geo_create_binary_base(&ctx);
|
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
geo->u.high = ctx.high;
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
var->get_handler = ngx_http_geo_range_variable;
|
2008-12-11 17:46:45 +08:00
|
|
|
var->data = (uintptr_t) geo;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ctx.high.default_value == NULL) {
|
|
|
|
ctx.high.default_value = &ngx_http_variable_null_value;
|
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_destroy_pool(ctx.temp_pool);
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
|
|
|
|
} else {
|
2008-11-29 01:22:35 +08:00
|
|
|
if (ctx.tree == NULL) {
|
|
|
|
ctx.tree = ngx_radix_tree_create(cf->pool, -1);
|
|
|
|
if (ctx.tree == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
geo->u.tree = ctx.tree;
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
var->get_handler = ngx_http_geo_cidr_variable;
|
2008-12-11 17:46:45 +08:00
|
|
|
var->data = (uintptr_t) geo;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
ngx_destroy_pool(ctx.temp_pool);
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
|
|
|
|
if (ngx_radix32tree_find(ctx.tree, 0) != NGX_RADIX_NO_VALUE) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_radix32tree_insert(ctx.tree, 0, 0,
|
|
|
|
(uintptr_t) &ngx_http_variable_null_value)
|
|
|
|
== NGX_ERROR)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-16 21:40:36 +08:00
|
|
|
static char *
|
|
|
|
ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
2005-02-04 03:33:37 +08:00
|
|
|
{
|
2008-11-25 23:59:06 +08:00
|
|
|
char *rv;
|
2010-06-30 00:06:20 +08:00
|
|
|
ngx_str_t *value;
|
2009-07-22 17:43:14 +08:00
|
|
|
ngx_cidr_t cidr;
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_http_geo_conf_ctx_t *ctx;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
ctx = cf->ctx;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
if (cf->args->nelts == 1) {
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[0].data, "ranges") == 0) {
|
|
|
|
|
|
|
|
if (ctx->tree) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"the \"ranges\" directive must be "
|
|
|
|
"the first directive inside \"geo\" block");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
ctx->ranges = 1;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
rv = NGX_CONF_OK;
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-04 03:33:37 +08:00
|
|
|
if (cf->args->nelts != 2) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid number of the geo parameters");
|
2008-11-25 23:59:06 +08:00
|
|
|
goto failed;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[0].data, "include") == 0) {
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
rv = ngx_http_geo_include(cf, ctx, &value[1]);
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2009-07-22 17:43:14 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
} else if (ngx_strcmp(value[0].data, "proxy") == 0) {
|
|
|
|
|
|
|
|
if (ngx_http_geo_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ngx_http_geo_add_proxy(cf, ctx, &cidr);
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
goto done;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ctx->ranges) {
|
2008-11-25 23:59:06 +08:00
|
|
|
rv = ngx_http_geo_range(cf, ctx, value);
|
2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
} else {
|
2008-11-25 23:59:06 +08:00
|
|
|
rv = ngx_http_geo_cidr(cf, ctx, value);
|
|
|
|
}
|
2007-08-10 21:13:28 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
done:
|
|
|
|
|
|
|
|
ngx_reset_pool(cf->pool);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
|
|
|
ngx_reset_pool(cf->pool);
|
|
|
|
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geo_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *value)
|
|
|
|
{
|
2010-06-29 21:36:16 +08:00
|
|
|
u_char *p, *last;
|
|
|
|
in_addr_t start, end;
|
|
|
|
ngx_str_t *net;
|
|
|
|
ngx_uint_t del;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
if (ngx_strcmp(value[0].data, "default") == 0) {
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ctx->high.default_value) {
|
2010-06-29 21:36:16 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"duplicate default geo range value: \"%V\", old value: \"%v\"",
|
2010-06-30 00:06:20 +08:00
|
|
|
&value[1], ctx->high.default_value);
|
2010-06-29 21:36:16 +08:00
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
ctx->high.default_value = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
|
|
if (ctx->high.default_value == NULL) {
|
2005-02-04 03:33:37 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
return NGX_CONF_OK;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ctx->binary_include) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"binary geo range base \"%s\" may not be mixed with usual entries",
|
|
|
|
ctx->include_name.data);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->high.low == NULL) {
|
|
|
|
ctx->high.low = ngx_pcalloc(ctx->pool,
|
|
|
|
0x10000 * sizeof(ngx_http_geo_range_t *));
|
|
|
|
if (ctx->high.low == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->entries++;
|
|
|
|
ctx->outside_entries = 1;
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
if (ngx_strcmp(value[0].data, "delete") == 0) {
|
|
|
|
net = &value[1];
|
|
|
|
del = 1;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
} else {
|
|
|
|
net = &value[0];
|
|
|
|
del = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last = net->data + net->len;
|
|
|
|
|
|
|
|
p = ngx_strlchr(net->data, last, '-');
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = ngx_inet_addr(net->data, p - net->data);
|
|
|
|
|
|
|
|
if (start == INADDR_NONE) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = ntohl(start);
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
end = ngx_inet_addr(p, last - p);
|
|
|
|
|
|
|
|
if (end == INADDR_NONE) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = ntohl(end);
|
|
|
|
|
|
|
|
if (start > end) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (del) {
|
|
|
|
if (ngx_http_geo_delete_range(cf, ctx, start, end)) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"no address range \"%V\" to delete", net);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->value = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
|
|
|
|
|
|
if (ctx->value == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->net = net;
|
|
|
|
|
|
|
|
return ngx_http_geo_add_range(cf, ctx, start, end);
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid range \"%V\"", net);
|
|
|
|
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* the add procedure is optimized to add a growing up sequence */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
in_addr_t start, in_addr_t end)
|
|
|
|
{
|
|
|
|
in_addr_t n;
|
|
|
|
ngx_uint_t h, i, s, e;
|
|
|
|
ngx_array_t *a;
|
|
|
|
ngx_http_geo_range_t *range;
|
|
|
|
|
2010-02-26 01:26:01 +08:00
|
|
|
for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
h = n >> 16;
|
2009-01-16 23:44:39 +08:00
|
|
|
|
|
|
|
if (n == start) {
|
|
|
|
s = n & 0xffff;
|
|
|
|
} else {
|
|
|
|
s = 0;
|
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
if ((n | 0xffff) > end) {
|
|
|
|
e = end & 0xffff;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
e = 0xffff;
|
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
a = (ngx_array_t *) ctx->high.low[h];
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
if (a == NULL) {
|
|
|
|
a = ngx_array_create(ctx->temp_pool, 64,
|
|
|
|
sizeof(ngx_http_geo_range_t));
|
|
|
|
if (a == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
ctx->high.low[h] = (ngx_http_geo_range_t *) a;
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i = a->nelts;
|
|
|
|
range = a->elts;
|
|
|
|
|
|
|
|
while (i) {
|
|
|
|
|
|
|
|
i--;
|
|
|
|
|
|
|
|
if (e < (ngx_uint_t) range[i].start) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > (ngx_uint_t) range[i].end) {
|
|
|
|
|
|
|
|
/* add after the range */
|
|
|
|
|
|
|
|
range = ngx_array_push(a);
|
|
|
|
if (range == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = a->elts;
|
|
|
|
|
|
|
|
ngx_memcpy(&range[i + 2], &range[i + 1],
|
|
|
|
(a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
|
|
|
|
|
2009-01-16 21:17:12 +08:00
|
|
|
range[i + 1].start = (u_short) s;
|
|
|
|
range[i + 1].end = (u_short) e;
|
|
|
|
range[i + 1].value = ctx->value;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s == (ngx_uint_t) range[i].start
|
|
|
|
&& e == (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
|
|
|
|
ctx->net, ctx->value, range[i].value);
|
2009-01-16 21:17:12 +08:00
|
|
|
|
|
|
|
range[i].value = ctx->value;
|
|
|
|
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > (ngx_uint_t) range[i].start
|
|
|
|
&& e < (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
/* split the range and insert the new one */
|
|
|
|
|
|
|
|
range = ngx_array_push(a);
|
|
|
|
if (range == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = ngx_array_push(a);
|
|
|
|
if (range == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = a->elts;
|
|
|
|
|
|
|
|
ngx_memcpy(&range[i + 3], &range[i + 1],
|
|
|
|
(a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t));
|
|
|
|
|
|
|
|
range[i + 2].start = (u_short) (e + 1);
|
|
|
|
range[i + 2].end = range[i].end;
|
|
|
|
range[i + 2].value = range[i].value;
|
|
|
|
|
2009-01-17 00:29:23 +08:00
|
|
|
range[i + 1].start = (u_short) s;
|
|
|
|
range[i + 1].end = (u_short) e;
|
|
|
|
range[i + 1].value = ctx->value;
|
|
|
|
|
2009-01-16 21:17:12 +08:00
|
|
|
range[i].end = (u_short) (s - 1);
|
|
|
|
|
2009-01-17 00:29:23 +08:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s == (ngx_uint_t) range[i].start
|
|
|
|
&& e < (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
/* shift the range start and insert the new range */
|
|
|
|
|
|
|
|
range = ngx_array_push(a);
|
|
|
|
if (range == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = a->elts;
|
|
|
|
|
2009-01-20 00:42:14 +08:00
|
|
|
ngx_memcpy(&range[i + 1], &range[i],
|
|
|
|
(a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
|
2009-01-17 00:29:23 +08:00
|
|
|
|
|
|
|
range[i + 1].start = (u_short) (e + 1);
|
|
|
|
|
|
|
|
range[i].start = (u_short) s;
|
|
|
|
range[i].end = (u_short) e;
|
|
|
|
range[i].value = ctx->value;
|
|
|
|
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > (ngx_uint_t) range[i].start
|
|
|
|
&& e == (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
/* shift the range end and insert the new range */
|
|
|
|
|
|
|
|
range = ngx_array_push(a);
|
|
|
|
if (range == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = a->elts;
|
|
|
|
|
|
|
|
ngx_memcpy(&range[i + 2], &range[i + 1],
|
|
|
|
(a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
|
|
|
|
|
2009-01-16 21:17:12 +08:00
|
|
|
range[i + 1].start = (u_short) s;
|
|
|
|
range[i + 1].end = (u_short) e;
|
|
|
|
range[i + 1].value = ctx->value;
|
|
|
|
|
2009-01-17 00:29:23 +08:00
|
|
|
range[i].end = (u_short) (s - 1);
|
|
|
|
|
2009-01-16 21:17:12 +08:00
|
|
|
goto next;
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
2009-01-16 23:47:05 +08:00
|
|
|
s = (ngx_uint_t) range[i].start;
|
|
|
|
e = (ngx_uint_t) range[i].end;
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2009-01-16 23:47:05 +08:00
|
|
|
"range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"",
|
|
|
|
ctx->net,
|
|
|
|
h >> 8, h & 0xff, s >> 8, s & 0xff,
|
|
|
|
h >> 8, h & 0xff, e >> 8, e & 0xff);
|
2005-02-04 03:33:37 +08:00
|
|
|
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
/* add the first range */
|
|
|
|
|
|
|
|
range = ngx_array_push(a);
|
|
|
|
if (range == NULL) {
|
2005-02-04 03:33:37 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
range->start = (u_short) s;
|
|
|
|
range->end = (u_short) e;
|
|
|
|
range->value = ctx->value;
|
2009-01-16 21:17:12 +08:00
|
|
|
|
|
|
|
next:
|
|
|
|
|
|
|
|
continue;
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_uint_t
|
|
|
|
ngx_http_geo_delete_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
in_addr_t start, in_addr_t end)
|
|
|
|
{
|
|
|
|
in_addr_t n;
|
|
|
|
ngx_uint_t h, i, s, e, warn;
|
|
|
|
ngx_array_t *a;
|
|
|
|
ngx_http_geo_range_t *range;
|
|
|
|
|
|
|
|
warn = 0;
|
|
|
|
|
2009-01-17 00:09:58 +08:00
|
|
|
for (n = start; n <= end; n += 0x10000) {
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
h = n >> 16;
|
2009-01-16 23:58:27 +08:00
|
|
|
|
|
|
|
if (n == start) {
|
|
|
|
s = n & 0xffff;
|
|
|
|
} else {
|
|
|
|
s = 0;
|
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
if ((n | 0xffff) > end) {
|
|
|
|
e = end & 0xffff;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
e = 0xffff;
|
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
a = (ngx_array_t *) ctx->high.low[h];
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
if (a == NULL) {
|
|
|
|
warn = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
range = a->elts;
|
|
|
|
for (i = 0; i < a->nelts; i++) {
|
|
|
|
|
|
|
|
if (s == (ngx_uint_t) range[i].start
|
|
|
|
&& e == (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
ngx_memcpy(&range[i], &range[i + 1],
|
|
|
|
(a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
|
2009-01-17 00:02:30 +08:00
|
|
|
|
|
|
|
a->nelts--;
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s != (ngx_uint_t) range[i].start
|
|
|
|
&& e != (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
warn = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return warn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *value)
|
|
|
|
{
|
|
|
|
ngx_int_t rc, del;
|
|
|
|
ngx_str_t *net;
|
|
|
|
ngx_uint_t i;
|
2009-02-24 22:01:40 +08:00
|
|
|
ngx_cidr_t cidr;
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_http_variable_value_t *val, *old;
|
|
|
|
|
|
|
|
if (ctx->tree == NULL) {
|
|
|
|
ctx->tree = ngx_radix_tree_create(ctx->pool, -1);
|
|
|
|
if (ctx->tree == NULL) {
|
2005-02-04 03:33:37 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[0].data, "default") == 0) {
|
2009-02-24 22:01:40 +08:00
|
|
|
cidr.u.in.addr = 0;
|
|
|
|
cidr.u.in.mask = 0;
|
2008-11-25 23:59:06 +08:00
|
|
|
net = &value[0];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ngx_strcmp(value[0].data, "delete") == 0) {
|
|
|
|
net = &value[1];
|
|
|
|
del = 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
net = &value[0];
|
|
|
|
del = 0;
|
|
|
|
}
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2009-07-22 17:43:14 +08:00
|
|
|
if (ngx_http_geo_cidr_value(cf, net, &cidr) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (del) {
|
2009-02-24 22:01:40 +08:00
|
|
|
if (ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr,
|
|
|
|
cidr.u.in.mask)
|
2008-11-25 23:59:06 +08:00
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"no network \"%V\" to delete", net);
|
|
|
|
}
|
2008-12-02 23:02:36 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val = ngx_http_geo_value(cf, ctx, &value[1]);
|
|
|
|
|
|
|
|
if (val == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
2005-10-04 18:38:53 +08:00
|
|
|
for (i = 2; i; i--) {
|
2009-02-24 22:01:40 +08:00
|
|
|
rc = ngx_radix32tree_insert(ctx->tree, cidr.u.in.addr, cidr.u.in.mask,
|
2008-11-23 04:42:51 +08:00
|
|
|
(uintptr_t) val);
|
2005-10-04 18:38:53 +08:00
|
|
|
if (rc == NGX_OK) {
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2005-10-04 18:38:53 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rc == NGX_BUSY */
|
|
|
|
|
2009-09-11 21:57:50 +08:00
|
|
|
old = (ngx_http_variable_value_t *)
|
2009-02-24 22:01:40 +08:00
|
|
|
ngx_radix32tree_find(ctx->tree, cidr.u.in.addr & cidr.u.in.mask);
|
2005-10-04 18:38:53 +08:00
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
2008-11-25 23:59:06 +08:00
|
|
|
"duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
|
|
|
|
net, val, old);
|
2005-10-04 18:38:53 +08:00
|
|
|
|
2009-02-24 22:01:40 +08:00
|
|
|
rc = ngx_radix32tree_delete(ctx->tree, cidr.u.in.addr, cidr.u.in.mask);
|
2005-10-04 18:38:53 +08:00
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
|
2005-10-04 18:38:53 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
2005-10-04 18:38:53 +08:00
|
|
|
return NGX_CONF_ERROR;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_variable_value_t *
|
|
|
|
ngx_http_geo_value(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *value)
|
|
|
|
{
|
2010-06-23 23:31:33 +08:00
|
|
|
uint32_t hash;
|
|
|
|
ngx_http_variable_value_t *val;
|
|
|
|
ngx_http_geo_variable_value_node_t *gvvn;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
hash = ngx_crc32_long(value->data, value->len);
|
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
gvvn = (ngx_http_geo_variable_value_node_t *)
|
|
|
|
ngx_str_rbtree_lookup(&ctx->rbtree, value, hash);
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
if (gvvn) {
|
|
|
|
return gvvn->value;
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
val = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t));
|
|
|
|
if (val == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
val->len = value->len;
|
|
|
|
val->data = ngx_pstrdup(ctx->pool, value);
|
|
|
|
if (val->data == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
val->valid = 1;
|
|
|
|
val->no_cacheable = 0;
|
|
|
|
val->not_found = 0;
|
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
gvvn = ngx_palloc(ctx->temp_pool,
|
|
|
|
sizeof(ngx_http_geo_variable_value_node_t));
|
|
|
|
if (gvvn == NULL) {
|
2008-11-25 23:59:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
gvvn->sn.node.key = hash;
|
|
|
|
gvvn->sn.str.len = val->len;
|
|
|
|
gvvn->sn.str.data = val->data;
|
|
|
|
gvvn->value = val;
|
2010-06-30 00:06:20 +08:00
|
|
|
gvvn->offset = 0;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
ngx_rbtree_insert(&ctx->rbtree, &gvvn->sn.node);
|
2008-11-25 23:59:06 +08:00
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
ctx->data_size += ngx_align(sizeof(ngx_http_variable_value_t) + value->len,
|
|
|
|
sizeof(void *));
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
return val;
|
|
|
|
}
|
2009-07-22 17:43:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geo_add_proxy(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_cidr_t *cidr)
|
|
|
|
{
|
|
|
|
ngx_in_cidr_t *c;
|
|
|
|
|
|
|
|
if (ctx->proxies == NULL) {
|
|
|
|
ctx->proxies = ngx_array_create(ctx->pool, 4, sizeof(ngx_in_cidr_t));
|
|
|
|
if (ctx->proxies == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = ngx_array_push(ctx->proxies);
|
|
|
|
if (c == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->addr = cidr->u.in.addr;
|
|
|
|
c->mask = cidr->u.in.mask;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
|
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
|
|
|
|
|
|
|
if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
|
|
|
|
cidr->u.in.addr = 0xffffffff;
|
|
|
|
cidr->u.in.mask = 0xffffffff;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_ptocidr(net, cidr);
|
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cidr->family != AF_INET) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"geo\" supports IPv4 only");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == NGX_DONE) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"low address bits of %V are meaningless", net);
|
|
|
|
}
|
|
|
|
|
|
|
|
cidr->u.in.addr = ntohl(cidr->u.in.addr);
|
|
|
|
cidr->u.in.mask = ntohl(cidr->u.in.mask);
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2010-06-30 00:06:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_geo_include(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *name)
|
|
|
|
{
|
|
|
|
char *rv;
|
|
|
|
ngx_str_t file;
|
|
|
|
|
|
|
|
file.len = name->len + 4;
|
|
|
|
file.data = ngx_pnalloc(ctx->temp_pool, name->len + 5);
|
|
|
|
if (file.data == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_sprintf(file.data, "%V.bin%Z", name);
|
|
|
|
|
|
|
|
if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-06-30 18:05:56 +08:00
|
|
|
if (ctx->ranges) {
|
2010-06-30 22:28:54 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
|
|
|
|
|
|
switch (ngx_http_geo_include_binary_base(cf, ctx, &file)) {
|
|
|
|
case NGX_OK:
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
case NGX_ERROR:
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-06-30 00:06:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
file.len -= 4;
|
|
|
|
file.data[file.len] = '\0';
|
|
|
|
|
|
|
|
ctx->include_name = file;
|
|
|
|
|
|
|
|
if (ctx->outside_entries) {
|
|
|
|
ctx->allow_binary_include = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
|
|
|
|
|
|
rv = ngx_conf_parse(cf, &file);
|
|
|
|
|
|
|
|
ctx->includes++;
|
|
|
|
ctx->outside_entries = 0;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_geo_include_binary_base(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
|
|
|
|
ngx_str_t *name)
|
|
|
|
{
|
2010-06-30 18:02:05 +08:00
|
|
|
u_char *base, ch;
|
|
|
|
time_t mtime;
|
2010-06-30 00:06:20 +08:00
|
|
|
size_t size, len;
|
|
|
|
ssize_t n;
|
|
|
|
uint32_t crc32;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_file_t file;
|
|
|
|
ngx_file_info_t fi;
|
|
|
|
ngx_http_geo_range_t *range, **ranges;
|
|
|
|
ngx_http_geo_header_t *header;
|
|
|
|
ngx_http_variable_value_t *vv;
|
|
|
|
|
|
|
|
ngx_memzero(&file, sizeof(ngx_file_t));
|
|
|
|
file.name = *name;
|
|
|
|
file.log = cf->log;
|
|
|
|
|
|
|
|
file.fd = ngx_open_file(name->data, NGX_FILE_RDONLY, 0, 0);
|
|
|
|
if (file.fd == NGX_INVALID_FILE) {
|
|
|
|
err = ngx_errno;
|
|
|
|
if (err != NGX_ENOENT) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_CRIT, cf, err,
|
|
|
|
ngx_open_file_n " \"%s\" failed", name->data);
|
|
|
|
}
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->outside_entries) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"binary geo range base \"%s\" may not be mixed with usual entries",
|
|
|
|
name->data);
|
|
|
|
rc = NGX_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-06-30 18:12:46 +08:00
|
|
|
if (ctx->binary_include) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"second binary geo range base \"%s\" may not be mixed with \"%s\"",
|
|
|
|
name->data, ctx->include_name.data);
|
|
|
|
rc = NGX_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-06-30 00:06:20 +08:00
|
|
|
if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
|
|
|
|
ngx_fd_info_n " \"%s\" failed", name->data);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = (size_t) ngx_file_size(&fi);
|
2010-06-30 18:02:05 +08:00
|
|
|
mtime = ngx_file_mtime(&fi);
|
|
|
|
|
|
|
|
ch = name->data[name->len - 4];
|
|
|
|
name->data[name->len - 4] = '\0';
|
|
|
|
|
|
|
|
if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
|
|
|
|
ngx_file_info_n " \"%s\" failed", name->data);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
name->data[name->len - 4] = ch;
|
|
|
|
|
|
|
|
if (mtime < ngx_file_mtime(&fi)) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"stale binary geo range base \"%s\"", name->data);
|
|
|
|
goto failed;
|
|
|
|
}
|
2010-06-30 00:06:20 +08:00
|
|
|
|
|
|
|
base = ngx_palloc(ctx->pool, size);
|
|
|
|
if (base == NULL) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ngx_read_file(&file, base, size, 0);
|
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_CRIT, cf, ngx_errno,
|
|
|
|
ngx_read_file_n " \"%s\" failed", name->data);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size_t) n != size) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_CRIT, cf, 0,
|
|
|
|
ngx_read_file_n " \"%s\" returned only %z bytes instead of %z",
|
|
|
|
name->data, n, size);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = (ngx_http_geo_header_t *) base;
|
|
|
|
|
2010-06-30 18:13:51 +08:00
|
|
|
if (size < 16 || ngx_memcmp(&ngx_http_geo_header, header, 12) != 0) {
|
2010-06-30 00:06:20 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"incompatible binary geo range base \"%s\"", name->data);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_crc32_init(crc32);
|
|
|
|
|
|
|
|
vv = (ngx_http_variable_value_t *) (base + sizeof(ngx_http_geo_header_t));
|
|
|
|
|
|
|
|
while(vv->data) {
|
|
|
|
len = ngx_align(sizeof(ngx_http_variable_value_t) + vv->len,
|
|
|
|
sizeof(void *));
|
|
|
|
ngx_crc32_update(&crc32, (u_char *) vv, len);
|
|
|
|
vv->data += (size_t) base;
|
|
|
|
vv = (ngx_http_variable_value_t *) ((u_char *) vv + len);
|
|
|
|
}
|
|
|
|
ngx_crc32_update(&crc32, (u_char *) vv, sizeof(ngx_http_variable_value_t));
|
|
|
|
vv++;
|
|
|
|
|
|
|
|
ranges = (ngx_http_geo_range_t **) vv;
|
|
|
|
|
|
|
|
for (i = 0; i < 0x10000; i++) {
|
|
|
|
ngx_crc32_update(&crc32, (u_char *) &ranges[i], sizeof(void *));
|
|
|
|
if (ranges[i]) {
|
|
|
|
ranges[i] = (ngx_http_geo_range_t *)
|
|
|
|
((u_char *) ranges[i] + (size_t) base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
range = (ngx_http_geo_range_t *) &ranges[0x10000];
|
|
|
|
|
|
|
|
while ((u_char *) range < base + size) {
|
|
|
|
while (range->value) {
|
|
|
|
ngx_crc32_update(&crc32, (u_char *) range,
|
|
|
|
sizeof(ngx_http_geo_range_t));
|
|
|
|
range->value = (ngx_http_variable_value_t *)
|
|
|
|
((u_char *) range->value + (size_t) base);
|
|
|
|
range++;
|
|
|
|
}
|
|
|
|
ngx_crc32_update(&crc32, (u_char *) range, sizeof(void *));
|
|
|
|
range = (ngx_http_geo_range_t *) ((u_char *) range + sizeof(void *));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_crc32_final(crc32);
|
|
|
|
|
|
|
|
if (crc32 != header->crc32) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"CRC32 mismatch in binary geo range base \"%s\"", name->data);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0,
|
|
|
|
"using binary geo range base \"%s\"", name->data);
|
|
|
|
|
|
|
|
ctx->include_name = *name;
|
|
|
|
ctx->binary_include = 1;
|
|
|
|
ctx->high.low = ranges;
|
|
|
|
rc = NGX_OK;
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
|
|
|
rc = NGX_DECLINED;
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_geo_create_binary_base(ngx_http_geo_conf_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
uint32_t hash;
|
|
|
|
ngx_str_t s;
|
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_file_mapping_t fm;
|
|
|
|
ngx_http_geo_range_t *r, *range, **ranges;
|
|
|
|
ngx_http_geo_header_t *header;
|
|
|
|
ngx_http_geo_variable_value_node_t *gvvn;
|
|
|
|
|
|
|
|
fm.name = ngx_pnalloc(ctx->temp_pool, ctx->include_name.len + 5);
|
|
|
|
if (fm.name == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_sprintf(fm.name, "%V.bin%Z", &ctx->include_name);
|
|
|
|
|
|
|
|
fm.size = ctx->data_size;
|
|
|
|
fm.log = ctx->pool->log;
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, fm.log, 0,
|
|
|
|
"creating binary geo range base \"%s\"", fm.name);
|
|
|
|
|
|
|
|
if (ngx_create_file_mapping(&fm) != NGX_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_cpymem(fm.addr, &ngx_http_geo_header,
|
|
|
|
sizeof(ngx_http_geo_header_t));
|
|
|
|
|
|
|
|
p = ngx_http_geo_copy_values(fm.addr, p, ctx->rbtree.root,
|
|
|
|
ctx->rbtree.sentinel);
|
|
|
|
|
|
|
|
p += sizeof(ngx_http_variable_value_t);
|
|
|
|
|
|
|
|
ranges = (ngx_http_geo_range_t **) p;
|
|
|
|
|
|
|
|
p += 0x10000 * sizeof(ngx_http_geo_range_t *);
|
|
|
|
|
|
|
|
for (i = 0; i < 0x10000; i++) {
|
|
|
|
r = ctx->high.low[i];
|
|
|
|
if (r == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = (ngx_http_geo_range_t *) p;
|
|
|
|
ranges[i] = (ngx_http_geo_range_t *) (p - (u_char *) fm.addr);
|
|
|
|
|
|
|
|
do {
|
|
|
|
s.len = r->value->len;
|
|
|
|
s.data = r->value->data;
|
|
|
|
hash = ngx_crc32_long(s.data, s.len);
|
|
|
|
gvvn = (ngx_http_geo_variable_value_node_t *)
|
|
|
|
ngx_str_rbtree_lookup(&ctx->rbtree, &s, hash);
|
|
|
|
|
|
|
|
range->value = (ngx_http_variable_value_t *) gvvn->offset;
|
|
|
|
range->start = r->start;
|
|
|
|
range->end = r->end;
|
|
|
|
range++;
|
|
|
|
|
|
|
|
} while ((++r)->value);
|
|
|
|
|
|
|
|
range->value = NULL;
|
|
|
|
|
|
|
|
p = (u_char *) range + sizeof(void *);
|
|
|
|
}
|
|
|
|
|
|
|
|
header = fm.addr;
|
|
|
|
header->crc32 = ngx_crc32_long((u_char *) fm.addr
|
|
|
|
+ sizeof(ngx_http_geo_header_t),
|
|
|
|
fm.size - sizeof(ngx_http_geo_header_t));
|
|
|
|
|
|
|
|
ngx_close_file_mapping(&fm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static u_char *
|
|
|
|
ngx_http_geo_copy_values(u_char *base, u_char *p, ngx_rbtree_node_t *node,
|
|
|
|
ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
|
|
|
ngx_http_variable_value_t *vv;
|
|
|
|
ngx_http_geo_variable_value_node_t *gvvn;
|
|
|
|
|
|
|
|
if (node == sentinel) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
gvvn = (ngx_http_geo_variable_value_node_t *) node;
|
|
|
|
gvvn->offset = p - base;
|
|
|
|
|
|
|
|
vv = (ngx_http_variable_value_t *) p;
|
|
|
|
*vv = *gvvn->value;
|
|
|
|
p += sizeof(ngx_http_variable_value_t);
|
|
|
|
vv->data = (u_char *) (p - base);
|
|
|
|
|
|
|
|
p = ngx_cpymem(p, gvvn->sn.str.data, gvvn->sn.str.len);
|
|
|
|
|
|
|
|
p = ngx_align_ptr(p, sizeof(void *));
|
|
|
|
|
|
|
|
p = ngx_http_geo_copy_values(base, p, node->left, sentinel);
|
|
|
|
|
|
|
|
return ngx_http_geo_copy_values(base, p, node->right, sentinel);
|
|
|
|
}
|