changeset 2533:c843171d5dc2

axe r->port_text
author Igor Sysoev <igor@sysoev.ru>
date Tue, 24 Feb 2009 07:29:55 +0000
parents aa53ef3e36e9
children 2749e4bf6af0
files src/http/ngx_http.c src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h src/http/ngx_http_header_filter_module.c src/http/ngx_http_request.c src/http/ngx_http_request.h src/http/ngx_http_variables.c
diffstat 7 files changed, 59 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -1603,7 +1603,7 @@ ngx_http_cmp_dns_wildcards(const void *o
 static ngx_int_t
 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
 {
-    ngx_uint_t                 i, a, last, bind_wildcard;
+    ngx_uint_t                 i, last, bind_wildcard;
     ngx_listening_t           *ls;
     ngx_http_port_t           *hport;
     ngx_http_conf_addr_t      *addr;
@@ -1626,16 +1626,16 @@ ngx_http_init_listening(ngx_conf_t *cf, 
         bind_wildcard = 0;
     }
 
-    a = 0;
-
-    while (a < last) {
-
-        if (bind_wildcard && !addr[a].bind) {
-            a++;
+    i = 0;
+
+    while (i < last) {
+
+        if (bind_wildcard && !addr[i].bind) {
+            i++;
             continue;
         }
 
-        ls = ngx_http_add_listening(cf, &addr[a]);
+        ls = ngx_http_add_listening(cf, &addr[i]);
         if (ls == NULL) {
             return NGX_ERROR;
         }
@@ -1647,21 +1647,12 @@ ngx_http_init_listening(ngx_conf_t *cf, 
 
         ls->servers = hport;
 
-        for (i = ls->addr_text.len - 1; i; i--) {
-
-            if (ls->addr_text.data[i] == ':') {
-                hport->port_text.len = ls->addr_text.len - i;
-                hport->port_text.data = &ls->addr_text.data[i];
-                break;
-            }
-        }
-
-        if (a == last - 1) {
+        if (i == last - 1) {
             hport->naddrs = last;
 
         } else {
             hport->naddrs = 1;
-            a = 0;
+            i = 0;
         }
 
         switch (ls->sockaddr->sa_family) {
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -2061,8 +2061,6 @@ ngx_http_subrequest(ngx_http_request_t *
         c->data = sr;
     }
 
-    sr->port_text = r->port_text;
-
     sr->variables = r->variables;
 
     sr->log_handler = r->log_handler;
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -200,8 +200,6 @@ typedef struct {
 
 
 typedef struct {
-    ngx_str_t                  port_text;
-
     /* ngx_http_in_addr_t or ngx_http_in6_addr_t */
     void                      *addrs;
     ngx_uint_t                 naddrs;
--- a/src/http/ngx_http_header_filter_module.c
+++ b/src/http/ngx_http_header_filter_module.c
@@ -315,26 +315,22 @@ ngx_http_header_filter(ngx_http_request_
             break;
         }
 
-#if (NGX_HTTP_SSL)
-        if (r->connection->ssl) {
-            len += sizeof("Location: https://") - 1
-                   + host.len
-                   + r->headers_out.location->value.len + 2;
+        len += sizeof("Location: https://") - 1
+               + host.len
+               + r->headers_out.location->value.len + 2;
 
-            if (clcf->port_in_redirect && port != 443) {
-                len += r->port_text->len;
-            }
+        if (clcf->port_in_redirect) {
 
-        } else
+#if (NGX_HTTP_SSL)
+	    if (r->connection->ssl)
+		port = (port == 443) ? 0 : port;
+	    else
 #endif
-        {
-            len += sizeof("Location: http://") - 1
-                   + host.len
-                   + r->headers_out.location->value.len + 2;
+		port = (port == 80) ? 0 : port;
+	}
 
-            if (clcf->port_in_redirect && port != 80) {
-                len += r->port_text->len;
-            }
+        if (port) {
+            len += sizeof(":65535") - 1;
         }
 
     } else {
@@ -492,21 +488,8 @@ ngx_http_header_filter(ngx_http_request_
         *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
         b->last = ngx_copy(b->last, host.data, host.len);
 
-        if (clcf->port_in_redirect) {
-#if (NGX_HTTP_SSL)
-            if (r->connection->ssl) {
-                if (port != 443) {
-                    b->last = ngx_copy(b->last, r->port_text->data,
-                                       r->port_text->len);
-                }
-            } else
-#endif
-            {
-                if (port != 80) {
-                    b->last = ngx_copy(b->last, r->port_text->data,
-                                       r->port_text->len);
-                }
-            }
+        if (port) {
+            b->last = ngx_sprintf(b->last, ":%ui", port);
         }
 
         b->last = ngx_copy(b->last, r->headers_out.location->value.data,
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -300,8 +300,6 @@ ngx_http_init_request(ngx_event_t *rev)
 
     port = c->listening->servers;
 
-    r->port_text = &port->port_text;
-
     r->connection = c;
 
     if (port->naddrs > 1) {
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -384,7 +384,6 @@ struct ngx_http_request_s {
     ngx_http_post_subrequest_t       *post_subrequest;
     ngx_http_posted_request_t        *posted_requests;
 
-    ngx_str_t                        *port_text;    /* ":80" */
     ngx_http_virtual_names_t         *virtual_names;
 
     ngx_int_t                         phase_handler;
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -956,11 +956,44 @@ static ngx_int_t
 ngx_http_variable_server_port(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {
-    v->len = r->port_text->len - 1;
+    ngx_uint_t            port;
+    struct sockaddr_in   *sin;
+#if (NGX_HAVE_INET6)
+    struct sockaddr_in6  *sin6;
+#endif
+
+    v->len = 0;
     v->valid = 1;
     v->no_cacheable = 0;
     v->not_found = 0;
-    v->data = r->port_text->data + 1;
+
+    if (ngx_http_server_addr(r, NULL) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
+    v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
+    if (v->data == NULL) {
+        return NGX_ERROR;
+    }
+
+    switch (r->connection->local_sockaddr->sa_family) {
+
+#if (NGX_HAVE_INET6)
+    case AF_INET6:
+        sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
+        port = ntohs(sin6->sin6_port);
+        break;
+#endif
+
+    default: /* AF_INET */
+        sin = (struct sockaddr_in *) r->connection->local_sockaddr;
+        port = ntohs(sin->sin_port);
+        break;
+    }
+
+    if (port > 0 && port < 65536) {
+        v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
+    }
 
     return NGX_OK;
 }