aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2008-08-26 14:24:14 +0000
committerIgor Sysoev <igor@sysoev.ru>2008-08-26 14:24:14 +0000
commitc9491d113c47b36fec61c37b9c66905f097d92cb (patch)
tree620685717cf70aa86bbc72387c3eb598bcc7cb6c /src
parent9c388c0a7fc86beeb584744b5bd5884884111732 (diff)
downloadnginx-c9491d113c47b36fec61c37b9c66905f097d92cb.tar.gz
nginx-c9491d113c47b36fec61c37b9c66905f097d92cb.zip
*) refactor ngx_parse_inet_url()
*) refactor ngx_parse_unix_domain_url() *) delete unused ngx_url_t fields
Diffstat (limited to 'src')
-rw-r--r--src/core/ngx_inet.c215
-rw-r--r--src/core/ngx_inet.h5
-rw-r--r--src/http/modules/ngx_http_proxy_module.c2
-rw-r--r--src/http/ngx_http_core_module.c2
-rw-r--r--src/mail/ngx_mail_core_module.c2
5 files changed, 99 insertions, 127 deletions
diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index e7b49821a..d565705b0 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -179,31 +179,26 @@ static ngx_int_t
ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
{
#if (NGX_HAVE_UNIX_DOMAIN)
- u_char *p;
+ u_char *path, *uri, *last;
size_t len;
- ngx_uint_t i;
struct sockaddr_un *saun;
len = u->url.len;
- p = u->url.data;
+ path = u->url.data;
- p += 5;
+ path += 5;
len -= 5;
- u->uri.len = len;
- u->uri.data = p;
-
if (u->uri_part) {
- for (i = 0; i < len; i++) {
- if (p[i] == ':') {
- len = i;
+ last = path + len;
+ uri = ngx_strlchr(path, last, ':');
- u->uri.len -= len + 1;
- u->uri.data += len + 1;
-
- break;
- }
+ if (uri) {
+ len = uri - path;
+ uri++;
+ 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;
}
- 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";
return NGX_ERROR;
}
@@ -230,18 +229,13 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
u->naddrs = 1;
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].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->host.len = len;
- u->host.data = p;
-
- u->unix_socket = 1;
-
return NGX_OK;
#else
@@ -257,147 +251,129 @@ 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)
{
- u_char *p, *host, *port_start;
- size_t len, port_len;
- ngx_int_t port;
- ngx_uint_t i;
+ u_char *p, *host, *port, *last, *uri;
+ size_t len;
+ ngx_int_t n;
struct hostent *h;
- len = u->url.len;
- p = u->url.data;
-
- u->host.data = p;
+ host = u->url.data;
- port_start = NULL;
- port_len = 0;
+ last = host + u->url.len;
- for (i = 0; i < len; i++) {
+ port = ngx_strlchr(host, last, ':');
- if (p[i] == ':') {
- port_start = &p[i + 1];
- u->host.len = i;
+ uri = ngx_strlchr(port ? port : host, last, '/');
- if (!u->uri_part) {
- port_len = len - (i + 1);
- break;
- }
+ if (uri) {
+ if (u->listen || !u->uri_part) {
+ u->err = "invalid host";
+ return NGX_ERROR;
}
- if (p[i] == '/') {
- u->uri.len = len - i;
- u->uri.data = &p[i];
+ u->uri.len = last - uri;
+ u->uri.data = uri;
- if (u->host.len == 0) {
- u->host.len = i;
- }
+ last = uri;
+ }
- if (port_start == NULL) {
- u->no_port = 1;
- goto no_port;
- }
+ if (port) {
+ port++;
- port_len = &p[i] - port_start;
+ if (last - port == 0) {
+ u->err = "invalid port";
+ return NGX_ERROR;
+ }
- if (port_len == 0) {
- u->err = "invalid port";
- return NGX_ERROR;
- }
+ u->port_text.len = last - port;
+ u->port_text.data = port;
- break;
- }
- }
+ last = port - 1;
- if (port_start) {
+ } else {
+ if (uri == NULL) {
- if (port_len == 0) {
- port_len = &p[i] - port_start;
+ if (u->listen) {
- if (port_len == 0) {
- u->err = "invalid port";
- return NGX_ERROR;
- }
- }
+ /* test value as port only */
- port = ngx_atoi(port_start, port_len);
+ n = ngx_atoi(host, last - host);
- if (port == NGX_ERROR || port < 1 || port > 65536) {
- u->err = "invalid port";
- return NGX_ERROR;
- }
+ if (n != NGX_ERROR) {
- u->port_text.len = port_len;
- u->port_text.data = port_start;
+ if (n < 1 || n > 65536) {
+ u->err = "invalid port";
+ return NGX_ERROR;
+ }
- } else {
- port = ngx_atoi(p, len);
+ u->port = (in_port_t) n;
- if (port == NGX_ERROR) {
- u->host.len = len;
- u->no_port = 1;
+ u->port_text.len = last - host;
+ u->port_text.data = host;
- goto no_port;
+ return NGX_OK;
+ }
+ }
}
- u->wildcard = 1;
+ u->no_port = 1;
}
- u->port = (in_port_t) port;
+ len = last - host;
-no_port:
+ if (len == 0) {
+ u->err = "no host";
+ return NGX_ERROR;
+ }
- if (u->listen) {
+ if (len == 1 && *host == '*') {
+ len = 0;
+ }
- if (u->port == 0) {
- if (u->default_port == 0) {
- u->err = "no port";
- return NGX_ERROR;
- }
+ u->host.len = len;
+ u->host.data = host;
- u->port = u->default_port;
- }
+ if (len++) {
- if (u->host.len == 1 && u->host.data[0] == '*') {
- u->host.len = 0;
+ p = ngx_alloc(len, pool->log);
+ if (p == NULL) {
+ return NGX_ERROR;
}
- /* AF_INET only */
+ (void) ngx_cpystrn(p, host, len);
+
+ u->addr.in_addr = inet_addr((const char *) p);
- if (u->host.len) {
+ if (u->addr.in_addr == INADDR_NONE) {
+ h = gethostbyname((const char *) p);
- host = ngx_alloc(u->host.len + 1, pool->log);
- if (host == NULL) {
+ if (h == NULL || h->h_addr_list[0] == NULL) {
+ ngx_free(p);
+ u->err = "host not found";
return NGX_ERROR;
}
- (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
-
- u->addr.in_addr = inet_addr((const char *) host);
+ u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
+ }
- if (u->addr.in_addr == INADDR_NONE) {
- h = gethostbyname((const char *) host);
+ ngx_free(p);
- if (h == NULL || h->h_addr_list[0] == NULL) {
- ngx_free(host);
- u->err = "host not found";
- return NGX_ERROR;
- }
+ } else {
+ u->addr.in_addr = INADDR_ANY;
+ }
- u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
- }
+ if (u->port_text.len) {
- ngx_free(host);
+ n = ngx_atoi(u->port_text.data, u->port_text.len);
- } else {
- u->addr.in_addr = INADDR_ANY;
+ if (n < 1 || n > 65536) {
+ u->err = "invalid port";
+ return NGX_ERROR;
}
- return NGX_OK;
+ u->port = (in_port_t) n;
}
- if (u->host.len == 0) {
- u->err = "no host";
- return NGX_ERROR;
- }
+ u->family = AF_INET;
if (u->no_resolve) {
return NGX_OK;
@@ -407,9 +383,8 @@ no_port:
u->port = u->default_port;
}
- if (u->port == 0) {
- u->err = "no port";
- return NGX_ERROR;
+ if (u->listen) {
+ return NGX_OK;
}
if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
diff --git a/src/core/ngx_inet.h b/src/core/ngx_inet.h
index cbff8fd43..be78142ea 100644
--- a/src/core/ngx_inet.h
+++ b/src/core/ngx_inet.h
@@ -34,8 +34,6 @@ typedef struct {
typedef struct {
- ngx_int_t type;
-
ngx_str_t url;
ngx_str_t host;
ngx_str_t port_text;
@@ -43,15 +41,14 @@ typedef struct {
in_port_t port;
in_port_t default_port;
+ int family;
unsigned listen:1;
unsigned uri_part:1;
unsigned no_resolve:1;
unsigned one_addr:1;
- unsigned wildcard:1;
unsigned no_port:1;
- unsigned unix_socket:1;
ngx_url_addr_t addr;
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index cf5605474..b69272d5f 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -2546,7 +2546,7 @@ static ngx_int_t
ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u,
ngx_http_proxy_vars_t *v)
{
- if (!u->unix_socket) {
+ if (u->family != AF_UNIX) {
if (u->no_port || u->port == u->default_port) {
v->host_header = u->host;
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 754a788d1..43d0ca949 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -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));
- ls->family = AF_INET;
+ ls->family = u.family;
ls->addr = u.addr.in_addr;
ls->port = u.port;
ls->file_name = cf->conf_file->file.name.data;
diff --git a/src/mail/ngx_mail_core_module.c b/src/mail/ngx_mail_core_module.c
index e52ea5750..07e3ab665 100644
--- a/src/mail/ngx_mail_core_module.c
+++ b/src/mail/ngx_mail_core_module.c
@@ -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->port = u.port;
- imls->family = AF_INET;
+ imls->family = u.family;
imls->ctx = cf->ctx;
for (m = 0; ngx_modules[m]; m++) {