changeset 2203:8e5bf1bc87e2

*) refactor ngx_parse_inet_url() *) refactor ngx_parse_unix_domain_url() *) delete unused ngx_url_t fields
author Igor Sysoev <igor@sysoev.ru>
date Tue, 26 Aug 2008 14:24:14 +0000
parents 2300ab9b069e
children 70a2bcc7e307
files src/core/ngx_inet.c src/core/ngx_inet.h src/http/modules/ngx_http_proxy_module.c src/http/ngx_http_core_module.c src/mail/ngx_mail_core_module.c
diffstat 5 files changed, 107 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- 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 *po
         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 *po
     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 *po
 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;
+    host = u->url.data;
 
-    u->host.data = p;
+    last = host + u->url.len;
 
-    port_start = NULL;
-    port_len = 0;
+    port = ngx_strlchr(host, last, ':');
 
-    for (i = 0; i < len; i++) {
+    uri = ngx_strlchr(port ? port : host, last, '/');
 
-        if (p[i] == ':') {
-            port_start = &p[i + 1];
-            u->host.len = i;
-
-            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];
-
-            if (u->host.len == 0) {
-                u->host.len = i;
-            }
+        u->uri.len = last - uri;
+        u->uri.data = uri;
 
-            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;
-        }
+        last = uri;
     }
 
-    if (port_start) {
-
-        if (port_len == 0) {
-            port_len = &p[i] - port_start;
+    if (port) {
+        port++;
 
-            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) {
+        if (last - port == 0) {
             u->err = "invalid port";
             return NGX_ERROR;
         }
 
-        u->port_text.len = port_len;
-        u->port_text.data = port_start;
+        u->port_text.len = last - port;
+        u->port_text.data = port;
+
+        last = port - 1;
 
     } else {
-        port = ngx_atoi(p, len);
+        if (uri == NULL) {
+
+            if (u->listen) {
+
+                /* test value as port only */
+
+                n = ngx_atoi(host, last - host);
+
+                if (n != NGX_ERROR) {
 
-        if (port == NGX_ERROR) {
-            u->host.len = len;
-            u->no_port = 1;
+                    if (n < 1 || n > 65536) {
+                        u->err = "invalid port";
+                        return NGX_ERROR;
+                    }
+
+                    u->port = (in_port_t) n;
 
-            goto no_port;
+                    u->port_text.len = last - host;
+                    u->port_text.data = host;
+
+                    return NGX_OK;
+                }
+            }
         }
 
-        u->wildcard = 1;
+        u->no_port = 1;
+    }
+
+    len = last - host;
+
+    if (len == 0) {
+        u->err = "no host";
+        return NGX_ERROR;
+    }
+
+    if (len == 1 && *host == '*') {
+        len = 0;
     }
 
-    u->port = (in_port_t) port;
+    u->host.len = len;
+    u->host.data = host;
+
+    if (len++) {
 
-no_port:
+        p = ngx_alloc(len, pool->log);
+        if (p == NULL) {
+            return NGX_ERROR;
+        }
 
-    if (u->listen) {
+        (void) ngx_cpystrn(p, host, len);
+
+        u->addr.in_addr = inet_addr((const char *) p);
 
-        if (u->port == 0) {
-            if (u->default_port == 0) {
-                u->err = "no port";
+        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->port = u->default_port;
-        }
-
-        if (u->host.len == 1 && u->host.data[0] == '*') {
-            u->host.len = 0;
+            u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
         }
 
-        /* AF_INET only */
-
-        if (u->host.len) {
+        ngx_free(p);
 
-            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);
+    } else {
+        u->addr.in_addr = INADDR_ANY;
+    }
 
-            if (u->addr.in_addr == INADDR_NONE) {
-                h = gethostbyname((const char *) host);
+    if (u->port_text.len) {
+
+        n = ngx_atoi(u->port_text.data, u->port_text.len);
 
-                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;
+        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) {
--- 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;
 
--- 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;
 
--- 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
 
     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;
--- 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
 
     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++) {