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 {
|
2008-12-11 17:46:45 +08:00
|
|
|
u_short start;
|
|
|
|
u_short end;
|
|
|
|
ngx_http_variable_value_t *value;
|
2008-11-25 23:59:06 +08:00
|
|
|
} ngx_http_geo_range_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_geo_range_t *ranges;
|
|
|
|
ngx_uint_t n;
|
2008-11-25 23:59:06 +08:00
|
|
|
} ngx_http_geo_low_ranges_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_geo_low_ranges_t low[0x10000];
|
|
|
|
ngx_http_variable_value_t *default_value;
|
2008-11-25 23:59:06 +08:00
|
|
|
} ngx_http_geo_high_ranges_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2008-12-11 17:46:45 +08:00
|
|
|
ngx_http_variable_value_t *value;
|
|
|
|
ngx_str_t *net;
|
|
|
|
ngx_http_geo_high_ranges_t *high;
|
|
|
|
ngx_radix_tree_t *tree;
|
|
|
|
ngx_rbtree_t rbtree;
|
|
|
|
ngx_rbtree_node_t sentinel;
|
|
|
|
ngx_pool_t *pool;
|
|
|
|
ngx_pool_t *temp_pool;
|
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;
|
|
|
|
ngx_http_geo_high_ranges_t *high;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
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);
|
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);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
ngx_uint_t i, n;
|
|
|
|
ngx_http_geo_range_t *range;
|
|
|
|
|
2008-12-11 17:46:45 +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
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
range = ctx->u.high->low[addr >> 16].ranges;
|
2008-11-25 23:59:06 +08:00
|
|
|
|
|
|
|
n = addr & 0xffff;
|
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
for (i = 0; i < ctx->u.high->low[addr >> 16].n; i++) {
|
2008-11-25 23:59:06 +08:00
|
|
|
if (n >= (ngx_uint_t) range[i].start
|
|
|
|
&& n <= (ngx_uint_t) range[i].end)
|
|
|
|
{
|
|
|
|
*v = *range[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-23 04:42:51 +08:00
|
|
|
ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel,
|
|
|
|
ngx_http_variable_value_rbtree_insert);
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
ctx.high = NULL;
|
|
|
|
ctx.tree = NULL;
|
2005-12-16 23:07:08 +08:00
|
|
|
ctx.pool = cf->pool;
|
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;
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
if (ctx.high) {
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
for (i = 0; i < 0x10000; i++) {
|
|
|
|
a = (ngx_array_t *) ctx.high->low[i].ranges;
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
if (a == NULL || a->nelts == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.high->low[i].n = a->nelts;
|
|
|
|
|
|
|
|
len = a->nelts * sizeof(ngx_http_geo_range_t);
|
|
|
|
|
|
|
|
ctx.high->low[i].ranges = ngx_palloc(cf->pool, len);
|
|
|
|
if (ctx.high->low[i].ranges == NULL ){
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(ctx.high->low[i].ranges, a->elts, len);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
ngx_destroy_pool(ctx.temp_pool);
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
|
|
|
|
if (ctx.high->default_value == NULL) {
|
|
|
|
ctx.high->default_value = &ngx_http_variable_null_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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;
|
|
|
|
ngx_str_t *value, file;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->high = ngx_pcalloc(ctx->pool,
|
|
|
|
sizeof(ngx_http_geo_high_ranges_t));
|
|
|
|
if (ctx->high == NULL) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
file.len = value[1].len++;
|
|
|
|
|
|
|
|
file.data = ngx_pstrdup(ctx->temp_pool, &value[1]);
|
|
|
|
if (file.data == NULL) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK){
|
|
|
|
goto failed;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
rv = ngx_conf_parse(cf, &file);
|
|
|
|
|
|
|
|
goto done;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
if (ctx->high) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
u_char *p, *last;
|
|
|
|
in_addr_t start, end;
|
|
|
|
ngx_str_t *net;
|
|
|
|
ngx_uint_t del;
|
|
|
|
ngx_http_variable_value_t *old;
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[0].data, "default") == 0) {
|
|
|
|
|
|
|
|
old = ctx->high->default_value;
|
|
|
|
|
|
|
|
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
|
|
|
if (old) {
|
2007-08-10 21:13:28 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
2008-11-25 23:59:06 +08:00
|
|
|
"duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
|
|
|
|
&value[0], ctx->high->default_value, old);
|
2007-08-10 21:13:28 +08:00
|
|
|
}
|
|
|
|
|
2008-11-25 23:59:06 +08:00
|
|
|
return NGX_CONF_OK;
|
2005-02-04 03:33:37 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = (ngx_array_t *) ctx->high->low[h].ranges;
|
|
|
|
|
|
|
|
if (a == NULL) {
|
|
|
|
a = ngx_array_create(ctx->temp_pool, 64,
|
|
|
|
sizeof(ngx_http_geo_range_t));
|
|
|
|
if (a == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->high->low[h].ranges = (ngx_http_geo_range_t *) a;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = (ngx_array_t *) ctx->high->low[h].ranges;
|
|
|
|
|
|
|
|
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;
|
|
|
|
ngx_inet_cidr_t cidrin;
|
|
|
|
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) {
|
|
|
|
cidrin.addr = 0;
|
|
|
|
cidrin.mask = 0;
|
|
|
|
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
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
|
|
|
|
cidrin.addr = 0xffffffff;
|
|
|
|
cidrin.mask = 0xffffffff;
|
2008-11-23 04:42:51 +08:00
|
|
|
|
2008-12-11 17:46:45 +08:00
|
|
|
} else {
|
2008-12-11 18:22:25 +08:00
|
|
|
rc = ngx_ptocidr(net, &cidrin);
|
2008-12-11 17:46:45 +08:00
|
|
|
|
2008-12-11 18:22:25 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid network \"%V\"", net);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2008-12-11 17:46:45 +08:00
|
|
|
|
2008-12-11 18:22:25 +08:00
|
|
|
if (rc == NGX_DONE) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"low address bits of %V are meaningless",
|
2008-12-11 17:46:45 +08:00
|
|
|
net);
|
2008-12-11 18:22:25 +08:00
|
|
|
}
|
2008-12-11 17:46:45 +08:00
|
|
|
|
2008-12-11 18:22:25 +08:00
|
|
|
cidrin.addr = ntohl(cidrin.addr);
|
|
|
|
cidrin.mask = ntohl(cidrin.mask);
|
2008-11-25 23:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (del) {
|
|
|
|
if (ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.mask)
|
|
|
|
!= 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--) {
|
2005-12-16 23:07:08 +08:00
|
|
|
rc = ngx_radix32tree_insert(ctx->tree, cidrin.addr, cidrin.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 */
|
|
|
|
|
|
|
|
old = (ngx_http_variable_value_t *)
|
2008-11-25 23:59:06 +08:00
|
|
|
ngx_radix32tree_find(ctx->tree, cidrin.addr & cidrin.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
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
rc = ngx_radix32tree_delete(ctx->tree, cidrin.addr, cidrin.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)
|
|
|
|
{
|
|
|
|
uint32_t hash;
|
|
|
|
ngx_http_variable_value_t *val;
|
|
|
|
ngx_http_variable_value_node_t *vvn;
|
|
|
|
|
|
|
|
hash = ngx_crc32_long(value->data, value->len);
|
|
|
|
|
|
|
|
val = ngx_http_variable_value_lookup(&ctx->rbtree, value, hash);
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
vvn = ngx_palloc(ctx->temp_pool, sizeof(ngx_http_variable_value_node_t));
|
|
|
|
if (vvn == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vvn->node.key = hash;
|
|
|
|
vvn->len = val->len;
|
|
|
|
vvn->value = val;
|
|
|
|
|
|
|
|
ngx_rbtree_insert(&ctx->rbtree, &vvn->node);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|