mirror of
https://github.com/nginx/nginx.git
synced 2025-06-12 13:42:55 +08:00
*) refactor ngx_parse_inet_url()
*) refactor ngx_parse_unix_domain_url() *) delete unused ngx_url_t fields
This commit is contained in:
parent
9c388c0a7f
commit
c9491d113c
@ -179,31 +179,26 @@ static ngx_int_t
|
|||||||
ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
|
ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
|
||||||
{
|
{
|
||||||
#if (NGX_HAVE_UNIX_DOMAIN)
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
||||||
u_char *p;
|
u_char *path, *uri, *last;
|
||||||
size_t len;
|
size_t len;
|
||||||
ngx_uint_t i;
|
|
||||||
struct sockaddr_un *saun;
|
struct sockaddr_un *saun;
|
||||||
|
|
||||||
len = u->url.len;
|
len = u->url.len;
|
||||||
p = u->url.data;
|
path = u->url.data;
|
||||||
|
|
||||||
p += 5;
|
path += 5;
|
||||||
len -= 5;
|
len -= 5;
|
||||||
|
|
||||||
u->uri.len = len;
|
|
||||||
u->uri.data = p;
|
|
||||||
|
|
||||||
if (u->uri_part) {
|
if (u->uri_part) {
|
||||||
for (i = 0; i < len; i++) {
|
|
||||||
|
|
||||||
if (p[i] == ':') {
|
last = path + len;
|
||||||
len = i;
|
uri = ngx_strlchr(path, last, ':');
|
||||||
|
|
||||||
u->uri.len -= len + 1;
|
if (uri) {
|
||||||
u->uri.data += len + 1;
|
len = uri - path;
|
||||||
|
uri++;
|
||||||
break;
|
u->uri.len = last - uri;
|
||||||
}
|
u->uri.data = uri;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,7 +207,11 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
|
|||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len + 1 > sizeof(saun->sun_path)) {
|
u->host.len = len++;
|
||||||
|
u->host.data = path;
|
||||||
|
u->family = AF_UNIX;
|
||||||
|
|
||||||
|
if (len > sizeof(saun->sun_path)) {
|
||||||
u->err = "too long path in the unix domain socket";
|
u->err = "too long path in the unix domain socket";
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
@ -230,18 +229,13 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
|
|||||||
u->naddrs = 1;
|
u->naddrs = 1;
|
||||||
|
|
||||||
saun->sun_family = AF_UNIX;
|
saun->sun_family = AF_UNIX;
|
||||||
(void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1);
|
(void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
|
||||||
|
|
||||||
u->addrs[0].sockaddr = (struct sockaddr *) saun;
|
u->addrs[0].sockaddr = (struct sockaddr *) saun;
|
||||||
u->addrs[0].socklen = sizeof(struct sockaddr_un);
|
u->addrs[0].socklen = sizeof(struct sockaddr_un);
|
||||||
u->addrs[0].name.len = len + 5;
|
u->addrs[0].name.len = len + 4;
|
||||||
u->addrs[0].name.data = u->url.data;
|
u->addrs[0].name.data = u->url.data;
|
||||||
|
|
||||||
u->host.len = len;
|
|
||||||
u->host.data = p;
|
|
||||||
|
|
||||||
u->unix_socket = 1;
|
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -257,148 +251,130 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
|
|||||||
static ngx_int_t
|
static ngx_int_t
|
||||||
ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
|
ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
|
||||||
{
|
{
|
||||||
u_char *p, *host, *port_start;
|
u_char *p, *host, *port, *last, *uri;
|
||||||
size_t len, port_len;
|
size_t len;
|
||||||
ngx_int_t port;
|
ngx_int_t n;
|
||||||
ngx_uint_t i;
|
|
||||||
struct hostent *h;
|
struct hostent *h;
|
||||||
|
|
||||||
len = u->url.len;
|
host = u->url.data;
|
||||||
p = u->url.data;
|
|
||||||
|
|
||||||
u->host.data = p;
|
last = host + u->url.len;
|
||||||
|
|
||||||
port_start = NULL;
|
port = ngx_strlchr(host, last, ':');
|
||||||
port_len = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
uri = ngx_strlchr(port ? port : host, last, '/');
|
||||||
|
|
||||||
if (p[i] == ':') {
|
if (uri) {
|
||||||
port_start = &p[i + 1];
|
if (u->listen || !u->uri_part) {
|
||||||
u->host.len = i;
|
u->err = "invalid host";
|
||||||
|
return NGX_ERROR;
|
||||||
if (!u->uri_part) {
|
|
||||||
port_len = len - (i + 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p[i] == '/') {
|
u->uri.len = last - uri;
|
||||||
u->uri.len = len - i;
|
u->uri.data = uri;
|
||||||
u->uri.data = &p[i];
|
|
||||||
|
|
||||||
if (u->host.len == 0) {
|
last = uri;
|
||||||
u->host.len = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (port_start == NULL) {
|
|
||||||
u->no_port = 1;
|
|
||||||
goto no_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
port_len = &p[i] - port_start;
|
|
||||||
|
|
||||||
if (port_len == 0) {
|
|
||||||
u->err = "invalid port";
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (port_start) {
|
if (port) {
|
||||||
|
port++;
|
||||||
|
|
||||||
if (port_len == 0) {
|
if (last - port == 0) {
|
||||||
port_len = &p[i] - port_start;
|
|
||||||
|
|
||||||
if (port_len == 0) {
|
|
||||||
u->err = "invalid port";
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
port = ngx_atoi(port_start, port_len);
|
|
||||||
|
|
||||||
if (port == NGX_ERROR || port < 1 || port > 65536) {
|
|
||||||
u->err = "invalid port";
|
u->err = "invalid port";
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
u->port_text.len = port_len;
|
u->port_text.len = last - port;
|
||||||
u->port_text.data = port_start;
|
u->port_text.data = port;
|
||||||
|
|
||||||
|
last = port - 1;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
port = ngx_atoi(p, len);
|
if (uri == NULL) {
|
||||||
|
|
||||||
if (port == NGX_ERROR) {
|
if (u->listen) {
|
||||||
u->host.len = len;
|
|
||||||
u->no_port = 1;
|
|
||||||
|
|
||||||
goto no_port;
|
/* test value as port only */
|
||||||
}
|
|
||||||
|
|
||||||
u->wildcard = 1;
|
n = ngx_atoi(host, last - host);
|
||||||
}
|
|
||||||
|
|
||||||
u->port = (in_port_t) port;
|
if (n != NGX_ERROR) {
|
||||||
|
|
||||||
no_port:
|
if (n < 1 || n > 65536) {
|
||||||
|
u->err = "invalid port";
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
if (u->listen) {
|
u->port = (in_port_t) n;
|
||||||
|
|
||||||
if (u->port == 0) {
|
u->port_text.len = last - host;
|
||||||
if (u->default_port == 0) {
|
u->port_text.data = host;
|
||||||
u->err = "no port";
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
u->port = u->default_port;
|
return NGX_OK;
|
||||||
}
|
|
||||||
|
|
||||||
if (u->host.len == 1 && u->host.data[0] == '*') {
|
|
||||||
u->host.len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* AF_INET only */
|
|
||||||
|
|
||||||
if (u->host.len) {
|
|
||||||
|
|
||||||
host = ngx_alloc(u->host.len + 1, pool->log);
|
|
||||||
if (host == NULL) {
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
(void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
|
|
||||||
|
|
||||||
u->addr.in_addr = inet_addr((const char *) host);
|
|
||||||
|
|
||||||
if (u->addr.in_addr == INADDR_NONE) {
|
|
||||||
h = gethostbyname((const char *) host);
|
|
||||||
|
|
||||||
if (h == NULL || h->h_addr_list[0] == NULL) {
|
|
||||||
ngx_free(host);
|
|
||||||
u->err = "host not found";
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_free(host);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
u->addr.in_addr = INADDR_ANY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NGX_OK;
|
u->no_port = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (u->host.len == 0) {
|
len = last - host;
|
||||||
|
|
||||||
|
if (len == 0) {
|
||||||
u->err = "no host";
|
u->err = "no host";
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (len == 1 && *host == '*') {
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
u->host.len = len;
|
||||||
|
u->host.data = host;
|
||||||
|
|
||||||
|
if (len++) {
|
||||||
|
|
||||||
|
p = ngx_alloc(len, pool->log);
|
||||||
|
if (p == NULL) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) ngx_cpystrn(p, host, len);
|
||||||
|
|
||||||
|
u->addr.in_addr = inet_addr((const char *) p);
|
||||||
|
|
||||||
|
if (u->addr.in_addr == INADDR_NONE) {
|
||||||
|
h = gethostbyname((const char *) p);
|
||||||
|
|
||||||
|
if (h == NULL || h->h_addr_list[0] == NULL) {
|
||||||
|
ngx_free(p);
|
||||||
|
u->err = "host not found";
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ngx_free(p);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
u->addr.in_addr = INADDR_ANY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u->port_text.len) {
|
||||||
|
|
||||||
|
n = ngx_atoi(u->port_text.data, u->port_text.len);
|
||||||
|
|
||||||
|
if (n < 1 || n > 65536) {
|
||||||
|
u->err = "invalid port";
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
u->port = (in_port_t) n;
|
||||||
|
}
|
||||||
|
|
||||||
|
u->family = AF_INET;
|
||||||
|
|
||||||
if (u->no_resolve) {
|
if (u->no_resolve) {
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
@ -407,9 +383,8 @@ no_port:
|
|||||||
u->port = u->default_port;
|
u->port = u->default_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (u->port == 0) {
|
if (u->listen) {
|
||||||
u->err = "no port";
|
return NGX_OK;
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
|
if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
|
||||||
|
@ -34,8 +34,6 @@ typedef struct {
|
|||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ngx_int_t type;
|
|
||||||
|
|
||||||
ngx_str_t url;
|
ngx_str_t url;
|
||||||
ngx_str_t host;
|
ngx_str_t host;
|
||||||
ngx_str_t port_text;
|
ngx_str_t port_text;
|
||||||
@ -43,15 +41,14 @@ typedef struct {
|
|||||||
|
|
||||||
in_port_t port;
|
in_port_t port;
|
||||||
in_port_t default_port;
|
in_port_t default_port;
|
||||||
|
int family;
|
||||||
|
|
||||||
unsigned listen:1;
|
unsigned listen:1;
|
||||||
unsigned uri_part:1;
|
unsigned uri_part:1;
|
||||||
unsigned no_resolve:1;
|
unsigned no_resolve:1;
|
||||||
unsigned one_addr:1;
|
unsigned one_addr:1;
|
||||||
|
|
||||||
unsigned wildcard:1;
|
|
||||||
unsigned no_port:1;
|
unsigned no_port:1;
|
||||||
unsigned unix_socket:1;
|
|
||||||
|
|
||||||
ngx_url_addr_t addr;
|
ngx_url_addr_t addr;
|
||||||
|
|
||||||
|
@ -2546,7 +2546,7 @@ static ngx_int_t
|
|||||||
ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u,
|
ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u,
|
||||||
ngx_http_proxy_vars_t *v)
|
ngx_http_proxy_vars_t *v)
|
||||||
{
|
{
|
||||||
if (!u->unix_socket) {
|
if (u->family != AF_UNIX) {
|
||||||
if (u->no_port || u->port == u->default_port) {
|
if (u->no_port || u->port == u->default_port) {
|
||||||
v->host_header = u->host;
|
v->host_header = u->host;
|
||||||
|
|
||||||
|
@ -2962,7 +2962,7 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
|
|
||||||
ngx_memzero(ls, sizeof(ngx_http_listen_t));
|
ngx_memzero(ls, sizeof(ngx_http_listen_t));
|
||||||
|
|
||||||
ls->family = AF_INET;
|
ls->family = u.family;
|
||||||
ls->addr = u.addr.in_addr;
|
ls->addr = u.addr.in_addr;
|
||||||
ls->port = u.port;
|
ls->port = u.port;
|
||||||
ls->file_name = cf->conf_file->file.name.data;
|
ls->file_name = cf->conf_file->file.name.data;
|
||||||
|
@ -329,7 +329,7 @@ ngx_mail_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
|
|
||||||
imls->addr = u.addr.in_addr;
|
imls->addr = u.addr.in_addr;
|
||||||
imls->port = u.port;
|
imls->port = u.port;
|
||||||
imls->family = AF_INET;
|
imls->family = u.family;
|
||||||
imls->ctx = cf->ctx;
|
imls->ctx = cf->ctx;
|
||||||
|
|
||||||
for (m = 0; ngx_modules[m]; m++) {
|
for (m = 0; ngx_modules[m]; m++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user