2002-12-15 14:25:09 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
#include <ngx_config.h>
|
2003-06-03 23:42:58 +08:00
|
|
|
#include <ngx_core.h>
|
2002-12-15 14:25:09 +08:00
|
|
|
|
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
static ngx_int_t ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u);
|
|
|
|
static ngx_int_t ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u);
|
|
|
|
|
|
|
|
|
2007-11-24 00:59:24 +08:00
|
|
|
/* AF_INET only */
|
|
|
|
|
|
|
|
in_addr_t
|
|
|
|
ngx_inet_addr(u_char *text, size_t len)
|
|
|
|
{
|
|
|
|
u_char *p, c;
|
|
|
|
in_addr_t addr;
|
|
|
|
ngx_uint_t octet, n;
|
|
|
|
|
|
|
|
addr = 0;
|
|
|
|
octet = 0;
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (p = text; p < text + len; p++) {
|
|
|
|
|
|
|
|
c = *p;
|
|
|
|
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
octet = octet * 10 + (c - '0');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '.' && octet < 256) {
|
|
|
|
addr = (addr << 8) + octet;
|
|
|
|
octet = 0;
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INADDR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n != 3) {
|
|
|
|
return INADDR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (octet < 256) {
|
|
|
|
addr = (addr << 8) + octet;
|
|
|
|
return htonl(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return INADDR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
/* AF_INET only */
|
|
|
|
|
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
|
|
|
size_t
|
2008-08-22 03:24:07 +08:00
|
|
|
ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len)
|
2003-01-30 15:28:09 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *p;
|
nginx-0.1.14-RELEASE import
*) Feature: the autoconfiguration directives:
--http-client-body-temp-path=PATH, --http-proxy-temp-path=PATH, and
--http-fastcgi-temp-path=PATH
*) Change: the directory name for the temporary files with the client
request body is specified by directive client_body_temp_path, by
default it is <prefix>/client_body_temp.
*) Feature: the ngx_http_fastcgi_module and the directives:
fastcgi_pass, fastcgi_root, fastcgi_index, fastcgi_params,
fastcgi_connect_timeout, fastcgi_send_timeout, fastcgi_read_timeout,
fastcgi_send_lowat, fastcgi_header_buffer_size, fastcgi_buffers,
fastcgi_busy_buffers_size, fastcgi_temp_path,
fastcgi_max_temp_file_size, fastcgi_temp_file_write_size,
fastcgi_next_upstream, and fastcgi_x_powered_by.
*) Bugfix: the "[alert] zero size buf" error; the bug had appeared in
0.1.3.
*) Change: the URI must be specified after the host name in the
proxy_pass directive.
*) Change: the %3F symbol in the URI was considered as the argument
string start.
*) Feature: the unix domain sockets support in the
ngx_http_proxy_module.
*) Feature: the ssl_engine and ssl_ciphers directives.
Thanks to Sergey Skvortsov for SSL-accelerator.
2005-01-18 21:03:58 +08:00
|
|
|
struct sockaddr_in *sin;
|
2003-01-30 15:28:09 +08:00
|
|
|
|
2008-08-22 03:24:07 +08:00
|
|
|
if (sa->sa_family == AF_INET) {
|
2004-03-16 21:35:20 +08:00
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
sin = (struct sockaddr_in *) sa;
|
|
|
|
p = (u_char *) &sin->sin_addr;
|
2003-01-30 15:28:09 +08:00
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
|
|
|
|
p[0], p[1], p[2], p[3])
|
|
|
|
- text;
|
2004-03-16 21:35:20 +08:00
|
|
|
}
|
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
return 0;
|
2003-01-30 15:28:09 +08:00
|
|
|
}
|
|
|
|
|
2008-01-04 19:54:55 +08:00
|
|
|
|
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
|
|
|
size_t
|
|
|
|
ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
|
2002-12-15 14:25:09 +08:00
|
|
|
{
|
2008-08-22 02:47:23 +08:00
|
|
|
u_char *p;
|
2004-03-16 21:35:20 +08:00
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
if (family == AF_INET) {
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
p = (u_char *) addr;
|
2004-03-16 21:35:20 +08:00
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
|
|
|
|
p[0], p[1], p[2], p[3])
|
|
|
|
- text;
|
2004-03-16 21:35:20 +08:00
|
|
|
}
|
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
return 0;
|
2008-01-04 19:54:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-25 23:28:46 +08:00
|
|
|
/* AF_INET only */
|
|
|
|
|
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_int_t
|
|
|
|
ngx_ptocidr(ngx_str_t *text, void *cidr)
|
2004-05-25 23:28:46 +08:00
|
|
|
{
|
2008-08-26 22:19:37 +08:00
|
|
|
u_char *addr, *mask, *last;
|
|
|
|
ngx_int_t shift;
|
2004-05-25 23:28:46 +08:00
|
|
|
ngx_inet_cidr_t *in_cidr;
|
|
|
|
|
|
|
|
in_cidr = cidr;
|
2008-08-26 22:19:37 +08:00
|
|
|
addr = text->data;
|
|
|
|
last = addr + text->len;
|
2004-05-25 23:28:46 +08:00
|
|
|
|
2008-08-26 22:19:37 +08:00
|
|
|
mask = ngx_strlchr(addr, last, '/');
|
2004-05-25 23:28:46 +08:00
|
|
|
|
2008-08-26 22:19:37 +08:00
|
|
|
in_cidr->addr = ngx_inet_addr(addr, (mask ? mask : last) - addr);
|
2004-05-25 23:28:46 +08:00
|
|
|
|
|
|
|
if (in_cidr->addr == INADDR_NONE) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-08-26 22:19:37 +08:00
|
|
|
if (mask == NULL) {
|
|
|
|
in_cidr->mask = 0xffffffff;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask++;
|
|
|
|
|
|
|
|
shift = ngx_atoi(mask, last - mask);
|
|
|
|
if (shift == NGX_ERROR) {
|
2004-05-25 23:28:46 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-08-26 22:19:37 +08:00
|
|
|
if (shift == 0) {
|
2004-06-25 00:07:04 +08:00
|
|
|
|
|
|
|
/* the x86 compilers use the shl instruction that shifts by modulo 32 */
|
|
|
|
|
|
|
|
in_cidr->mask = 0;
|
2008-08-26 22:19:37 +08:00
|
|
|
|
|
|
|
if (in_cidr->addr == 0) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_DONE;
|
2004-06-25 00:07:04 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 22:19:37 +08:00
|
|
|
in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift))));
|
2004-06-25 00:07:04 +08:00
|
|
|
|
2007-08-10 21:13:28 +08:00
|
|
|
if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_cidr->addr &= in_cidr->mask;
|
|
|
|
|
|
|
|
return NGX_DONE;
|
2004-05-25 23:28:46 +08:00
|
|
|
}
|
2004-09-14 23:55:24 +08:00
|
|
|
|
|
|
|
|
2006-05-23 22:54:58 +08:00
|
|
|
ngx_int_t
|
2007-10-08 16:55:12 +08:00
|
|
|
ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)
|
2006-05-23 22:54:58 +08:00
|
|
|
{
|
2008-08-22 21:36:56 +08:00
|
|
|
u_char *p;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
p = u->url.data;
|
|
|
|
|
2007-02-15 02:51:19 +08:00
|
|
|
if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {
|
2008-08-22 21:36:56 +08:00
|
|
|
return ngx_parse_unix_domain_url(pool, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p[0] == ':' || p[0] == '/') && !u->listen) {
|
|
|
|
u->err = "invalid host";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
return ngx_parse_inet_url(pool, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
|
|
|
|
{
|
2006-05-23 22:54:58 +08:00
|
|
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
2008-08-26 22:24:14 +08:00
|
|
|
u_char *path, *uri, *last;
|
2008-08-22 21:36:56 +08:00
|
|
|
size_t len;
|
|
|
|
struct sockaddr_un *saun;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
len = u->url.len;
|
2008-08-26 22:24:14 +08:00
|
|
|
path = u->url.data;
|
2008-08-22 21:36:56 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
path += 5;
|
2008-08-22 21:36:56 +08:00
|
|
|
len -= 5;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
if (u->uri_part) {
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
last = path + len;
|
|
|
|
uri = ngx_strlchr(path, last, ':');
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (uri) {
|
|
|
|
len = uri - path;
|
|
|
|
uri++;
|
|
|
|
u->uri.len = last - uri;
|
|
|
|
u->uri.data = uri;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
2008-08-22 21:36:56 +08:00
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
if (len == 0) {
|
|
|
|
u->err = "no path in the unix domain socket";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->host.len = len++;
|
|
|
|
u->host.data = path;
|
|
|
|
u->family = AF_UNIX;
|
|
|
|
|
|
|
|
if (len > sizeof(saun->sun_path)) {
|
2008-08-22 21:36:56 +08:00
|
|
|
u->err = "too long path in the unix domain socket";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
|
|
|
|
if (u->addrs == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un));
|
|
|
|
if (saun == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
u->naddrs = 1;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
saun->sun_family = AF_UNIX;
|
2008-08-26 22:24:14 +08:00
|
|
|
(void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
u->addrs[0].sockaddr = (struct sockaddr *) saun;
|
|
|
|
u->addrs[0].socklen = sizeof(struct sockaddr_un);
|
2008-08-26 22:24:14 +08:00
|
|
|
u->addrs[0].name.len = len + 4;
|
2008-08-22 21:36:56 +08:00
|
|
|
u->addrs[0].name.data = u->url.data;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
return NGX_OK;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
u->err = "the unix domain sockets are not supported on this platform";
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
#endif
|
2008-08-22 21:36:56 +08:00
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 21:36:56 +08:00
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
|
|
|
|
{
|
2008-08-26 22:24:14 +08:00
|
|
|
u_char *p, *host, *port, *last, *uri;
|
|
|
|
size_t len;
|
|
|
|
ngx_int_t n;
|
2008-08-22 21:36:56 +08:00
|
|
|
struct hostent *h;
|
|
|
|
|
2008-08-27 00:11:30 +08:00
|
|
|
u->family = AF_INET;
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
host = u->url.data;
|
2006-12-13 00:46:16 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
last = host + u->url.len;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
port = ngx_strlchr(host, last, ':');
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-10-24 23:12:11 +08:00
|
|
|
uri = ngx_strlchr(host, last, '/');
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (uri) {
|
|
|
|
if (u->listen || !u->uri_part) {
|
|
|
|
u->err = "invalid host";
|
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->uri.len = last - uri;
|
|
|
|
u->uri.data = uri;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
last = uri;
|
2008-10-24 23:12:11 +08:00
|
|
|
|
|
|
|
if (uri < port) {
|
|
|
|
port = NULL;
|
|
|
|
}
|
2008-08-26 22:24:14 +08:00
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (port) {
|
|
|
|
port++;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-27 00:11:30 +08:00
|
|
|
len = last - port;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
u->err = "invalid port";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ngx_atoi(port, len);
|
|
|
|
|
|
|
|
if (n < 1 || n > 65536) {
|
2008-08-26 22:24:14 +08:00
|
|
|
u->err = "invalid port";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-27 00:11:30 +08:00
|
|
|
u->port = (in_port_t) n;
|
|
|
|
|
|
|
|
u->port_text.len = len;
|
2008-08-26 22:24:14 +08:00
|
|
|
u->port_text.data = port;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
last = port - 1;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
} else {
|
|
|
|
if (uri == NULL) {
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (u->listen) {
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
/* test value as port only */
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
n = ngx_atoi(host, last - host);
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (n != NGX_ERROR) {
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (n < 1 || n > 65536) {
|
|
|
|
u->err = "invalid port";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2007-11-29 03:55:31 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->port = (in_port_t) n;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->port_text.len = last - host;
|
|
|
|
u->port_text.data = host;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->no_port = 1;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
len = last - host;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
u->err = "no host";
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (len == 1 && *host == '*') {
|
|
|
|
len = 0;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->host.len = len;
|
|
|
|
u->host.data = host;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2008-08-27 00:11:30 +08:00
|
|
|
if (u->no_resolve) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (len++) {
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
p = ngx_alloc(len, pool->log);
|
|
|
|
if (p == NULL) {
|
|
|
|
return NGX_ERROR;
|
2006-10-24 21:06:55 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
(void) ngx_cpystrn(p, host, len);
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->addr.in_addr = inet_addr((const char *) p);
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (u->addr.in_addr == INADDR_NONE) {
|
|
|
|
h = gethostbyname((const char *) p);
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (h == NULL || h->h_addr_list[0] == NULL) {
|
|
|
|
ngx_free(p);
|
|
|
|
u->err = "host not found";
|
2007-10-08 16:55:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
|
|
|
|
}
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
ngx_free(p);
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
} else {
|
|
|
|
u->addr.in_addr = INADDR_ANY;
|
|
|
|
}
|
2006-10-24 21:06:55 +08:00
|
|
|
|
2006-12-13 00:46:16 +08:00
|
|
|
if (u->no_port) {
|
|
|
|
u->port = u->default_port;
|
|
|
|
}
|
|
|
|
|
2008-08-26 22:24:14 +08:00
|
|
|
if (u->listen) {
|
|
|
|
return NGX_OK;
|
2006-12-13 00:46:16 +08:00
|
|
|
}
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
|
2006-05-23 22:54:58 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
ngx_int_t
|
2007-10-08 16:55:12 +08:00
|
|
|
ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
|
2006-05-23 22:54:58 +08:00
|
|
|
{
|
2006-12-13 00:46:16 +08:00
|
|
|
u_char *p, *host;
|
2006-05-23 22:54:58 +08:00
|
|
|
size_t len;
|
|
|
|
in_addr_t in_addr;
|
|
|
|
ngx_uint_t i;
|
|
|
|
struct hostent *h;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
host = ngx_alloc(u->host.len + 1, pool->log);
|
2006-05-23 22:54:58 +08:00
|
|
|
if (host == NULL) {
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
(void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
/* AF_INET only */
|
|
|
|
|
|
|
|
in_addr = inet_addr((char *) host);
|
|
|
|
|
|
|
|
if (in_addr == INADDR_NONE) {
|
|
|
|
h = gethostbyname((char *) host);
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
ngx_free(host);
|
|
|
|
|
2006-05-23 22:54:58 +08:00
|
|
|
if (h == NULL || h->h_addr_list[0] == NULL) {
|
2006-12-05 00:46:13 +08:00
|
|
|
u->err = "host not found";
|
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
if (u->one_addr == 0) {
|
|
|
|
for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
|
|
|
|
|
|
|
|
} else {
|
|
|
|
i = 1;
|
|
|
|
}
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
/* MP: ngx_shared_palloc() */
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
u->addrs = ngx_pcalloc(pool, i * sizeof(ngx_peer_addr_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
if (u->addrs == NULL) {
|
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
u->naddrs = i;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
for (i = 0; h->h_addr_list[i] != NULL; i++) {
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
|
2006-05-23 22:54:58 +08:00
|
|
|
if (sin == NULL) {
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sin->sin_family = AF_INET;
|
2006-12-13 00:46:16 +08:00
|
|
|
sin->sin_port = htons(u->port);
|
2006-05-23 22:54:58 +08:00
|
|
|
sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
u->addrs[i].sockaddr = (struct sockaddr *) sin;
|
|
|
|
u->addrs[i].socklen = sizeof(struct sockaddr_in);
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-08-22 02:47:23 +08:00
|
|
|
len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-06-17 23:00:30 +08:00
|
|
|
p = ngx_pnalloc(pool, len);
|
2006-12-13 00:46:16 +08:00
|
|
|
if (p == NULL) {
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2008-08-22 03:24:07 +08:00
|
|
|
len = ngx_sock_ntop((struct sockaddr *) sin, p, len);
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2006-12-13 00:46:16 +08:00
|
|
|
u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p;
|
|
|
|
u->addrs[i].name.data = p;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
ngx_free(host);
|
|
|
|
|
2006-05-23 22:54:58 +08:00
|
|
|
/* MP: ngx_shared_palloc() */
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
if (u->addrs == NULL) {
|
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
|
2006-05-23 22:54:58 +08:00
|
|
|
if (sin == NULL) {
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
u->naddrs = 1;
|
2006-05-23 22:54:58 +08:00
|
|
|
|
|
|
|
sin->sin_family = AF_INET;
|
2006-12-13 00:46:16 +08:00
|
|
|
sin->sin_port = htons(u->port);
|
2006-05-23 22:54:58 +08:00
|
|
|
sin->sin_addr.s_addr = in_addr;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
u->addrs[0].sockaddr = (struct sockaddr *) sin;
|
|
|
|
u->addrs[0].socklen = sizeof(struct sockaddr_in);
|
2006-05-23 22:54:58 +08:00
|
|
|
|
2008-06-17 23:00:30 +08:00
|
|
|
p = ngx_pnalloc(pool, u->host.len + sizeof(":65536") - 1);
|
2006-12-13 00:46:16 +08:00
|
|
|
if (p == NULL) {
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-13 00:46:16 +08:00
|
|
|
u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p;
|
|
|
|
u->addrs[0].name.data = p;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_OK;
|
2006-05-23 22:54:58 +08:00
|
|
|
}
|