# HG changeset patch # User Igor Sysoev # Date 1165941976 0 # Node ID a0310ac2814fa089c2dc3dfde115689df315c454 # Parent be52cfdb3e317e03601eaa759ad71ba9d2cfe993 rewritten upstream diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c --- a/src/core/ngx_inet.c +++ b/src/core/ngx_inet.c @@ -221,8 +221,8 @@ ngx_ptocidr(ngx_str_t *text, void *cidr) ngx_int_t ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u) { - u_char *p, *host; - size_t len; + u_char *p, *host, *port_start; + size_t len, port_len; ngx_int_t port; ngx_uint_t i; struct hostent *h; @@ -290,8 +290,7 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t u->host.len = len; u->host.data = p; - u->host_header.len = sizeof("localhost") - 1; - u->host_header.data = (u_char *) "localhost"; + u->unix_socket = 1; return NGX_OK; @@ -309,17 +308,18 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t } u->host.data = p; - u->host_header.len = len; - u->host_header.data = p; + + port_start = NULL; + port_len = 0; for (i = 0; i < len; i++) { if (p[i] == ':') { - u->port.data = &p[i + 1]; + port_start = &p[i + 1]; u->host.len = i; if (!u->uri_part) { - u->port.len = len - (i + 1); + port_len = len - (i + 1); break; } } @@ -327,20 +327,19 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t if (p[i] == '/') { u->uri.len = len - i; u->uri.data = &p[i]; - u->host_header.len = i; if (u->host.len == 0) { u->host.len = i; } - if (u->port.data == NULL) { + if (port_start == NULL) { u->no_port = 1; goto no_port; } - u->port.len = &p[i] - u->port.data; + port_len = &p[i] - port_start; - if (u->port.len == 0) { + if (port_len == 0) { u->err = "invalid port"; return NGX_ERROR; } @@ -349,18 +348,18 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t } } - if (u->port.data) { + if (port_start) { - if (u->port.len == 0) { - u->port.len = &p[i] - u->port.data; + if (port_len == 0) { + port_len = &p[i] - port_start; - if (u->port.len == 0) { + if (port_len == 0) { u->err = "invalid port"; return NGX_ERROR; } } - port = ngx_atoi(u->port.data, u->port.len); + port = ngx_atoi(port_start, port_len); if (port == NGX_ERROR || port < 1 || port > 65536) { u->err = "invalid port"; @@ -377,24 +376,22 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t goto no_port; } - u->port.len = len; - u->port.data = p; u->wildcard = 1; } - u->portn = (in_port_t) port; + u->port = (in_port_t) port; no_port: if (u->listen) { - if (u->portn == 0) { - if (u->default_portn == 0) { + if (u->port == 0) { + if (u->default_port == 0) { u->err = "no port"; return NGX_ERROR; } - u->portn = u->default_portn; + u->port = u->default_port; } if (u->host.len == 1 && u->host.data[0] == '*') { @@ -422,7 +419,7 @@ no_port: return NGX_ERROR; } - u->addr.in_addr = *(in_addr_t *)(h->h_addr_list[0]); + u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]); } } else { @@ -432,28 +429,6 @@ no_port: return NGX_OK; } - if (u->no_port) { - - if (u->default_portn == 0 && !u->upstream) { - u->err = "no port"; - return NGX_ERROR; - } - - u->portn = u->default_portn; - - u->port.data = ngx_palloc(cf->pool, sizeof("65536") - 1); - if (u->port.data == NULL) { - return NGX_ERROR; - } - - u->port.len = ngx_sprintf(u->port.data, "%d", u->portn) - u->port.data; - - } else if (u->portn == 0) { - - u->err = "no port"; - return NGX_ERROR; - } - if (u->host.len == 0) { u->err = "no host"; return NGX_ERROR; @@ -463,6 +438,15 @@ no_port: return NGX_OK; } + if (u->no_port) { + u->port = u->default_port; + } + + if (u->port == 0) { + u->err = "no port"; + return NGX_ERROR; + } + if (ngx_inet_resolve_host(cf, u) != NGX_OK) { return NGX_ERROR; } @@ -474,7 +458,7 @@ no_port: ngx_int_t ngx_inet_resolve_host(ngx_conf_t *cf, ngx_url_t *u) { - u_char *host; + u_char *p, *host; size_t len; in_addr_t in_addr; ngx_uint_t i; @@ -524,7 +508,7 @@ ngx_inet_resolve_host(ngx_conf_t *cf, ng } sin->sin_family = AF_INET; - sin->sin_port = htons(u->portn); + sin->sin_port = htons(u->port); sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); u->addrs[i].sockaddr = (struct sockaddr *) sin; @@ -532,17 +516,15 @@ ngx_inet_resolve_host(ngx_conf_t *cf, ng len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; - u->addrs[i].name.data = ngx_palloc(cf->pool, len); - if (u->addrs[i].name.data == NULL) { + p = ngx_palloc(cf->pool, len); + if (p == NULL) { return NGX_ERROR; } - len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, - u->addrs[i].name.data, len); + len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, p, len); - u->addrs[i].name.len = ngx_sprintf(&u->addrs[i].name.data[len], - ":%d", u->portn) - - u->addrs[i].name.data; + u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p; + u->addrs[i].name.data = p; } } else { @@ -562,21 +544,19 @@ ngx_inet_resolve_host(ngx_conf_t *cf, ng u->naddrs = 1; sin->sin_family = AF_INET; - sin->sin_port = htons(u->portn); + sin->sin_port = htons(u->port); sin->sin_addr.s_addr = in_addr; u->addrs[0].sockaddr = (struct sockaddr *) sin; u->addrs[0].socklen = sizeof(struct sockaddr_in); - u->addrs[0].name.data = ngx_palloc(cf->pool, - u->host.len + sizeof(":65536") - 1); - if (u->addrs[0].name.data == NULL) { + p = ngx_palloc(cf->pool, u->host.len + sizeof(":65536") - 1); + if (p == NULL) { return NGX_ERROR; } - u->addrs[0].name.len = ngx_sprintf(u->addrs[0].name.data, "%V:%d", - &u->host, u->portn) - - u->addrs[0].name.data; + u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p; + u->addrs[0].name.data = p; } return NGX_OK; diff --git a/src/core/ngx_inet.h b/src/core/ngx_inet.h --- a/src/core/ngx_inet.h +++ b/src/core/ngx_inet.h @@ -35,21 +35,19 @@ typedef struct { ngx_str_t url; ngx_str_t host; - ngx_str_t host_header; - ngx_str_t port; ngx_str_t uri; - in_port_t portn; - in_port_t default_portn; + in_port_t port; + in_port_t default_port; unsigned listen:1; unsigned uri_part:1; - unsigned upstream: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_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c --- a/src/http/modules/ngx_http_fastcgi_module.c +++ b/src/http/modules/ngx_http_fastcgi_module.c @@ -2010,7 +2010,6 @@ ngx_http_fastcgi_pass(ngx_conf_t *cf, ng ngx_memzero(&u, sizeof(ngx_url_t)); u.url = value[1]; - u.upstream = 1; u.no_resolve = 1; lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c --- a/src/http/modules/ngx_http_memcached_module.c +++ b/src/http/modules/ngx_http_memcached_module.c @@ -583,7 +583,6 @@ ngx_http_memcached_pass(ngx_conf_t *cf, ngx_memzero(&u, sizeof(ngx_url_t)); u.url = value[1]; - u.upstream = 1; u.no_resolve = 1; /* u.uri_part = 1; may be used as namespace */ diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c --- a/src/http/modules/ngx_http_proxy_module.c +++ b/src/http/modules/ngx_http_proxy_module.c @@ -51,7 +51,7 @@ typedef struct { ngx_str_t method; ngx_str_t host_header; - ngx_str_t port_text; + ngx_str_t port; ngx_flag_t redirect; } ngx_http_proxy_loc_conf_t; @@ -1232,11 +1232,11 @@ ngx_http_proxy_port_variable(ngx_http_re plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); - v->len = plcf->port_text.len; + v->len = plcf->port.len; v->valid = 1; v->no_cachable = 0; v->not_found = 0; - v->data = plcf->port_text.data; + v->data = plcf->port.data; return NGX_OK; } @@ -1817,7 +1817,7 @@ peers: conf->upstream.upstream = prev->upstream.upstream; conf->host_header = prev->host_header; - conf->port_text = prev->port_text; + conf->port = prev->port; conf->upstream.schema = prev->upstream.schema; } @@ -2093,6 +2093,7 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_ { ngx_http_proxy_loc_conf_t *plcf = conf; + u_char *p; size_t add; u_short port; ngx_str_t *value, *url; @@ -2158,18 +2159,49 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_ u.url.len = url->len - add; u.url.data = url->data + add; - u.default_portn = port; - u.upstream = 1; + u.default_port = port; + u.uri_part = 1; u.no_resolve = 1; - u.uri_part = 1; plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); if (plcf->upstream.upstream == NULL) { return NGX_CONF_ERROR; } - plcf->host_header = u.host_header; - plcf->port_text = u.port; + if (!u.unix_socket) { + if (u.no_port || u.port == port) { + plcf->host_header = u.host; + + if (port == 80) { + plcf->port.len = sizeof("80") - 1; + plcf->port.data = (u_char *) "80"; + } else { + plcf->port.len = sizeof("443") - 1; + plcf->port.data = (u_char *) "443"; + } + + } else { + p = ngx_palloc(cf->pool, u.host.len + sizeof(":65536") - 1); + if (p == NULL) { + return NGX_CONF_ERROR; + } + + plcf->host_header.len = ngx_sprintf(p, "%V:%d", &u.host, u.port) + - p; + plcf->host_header.data = p; + + plcf->port.len = plcf->host_header.len - u.host.len - 1; + plcf->port.data = p + u.host.len + 1; + } + + + } else { + plcf->host_header.len = sizeof("localhost") - 1; + plcf->host_header.data = (u_char *) "localhost"; + plcf->port.len = 0; + plcf->port.data = (u_char *) ""; + } + plcf->upstream.uri = u.uri; plcf->upstream.schema.len = add; diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -2390,7 +2390,7 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx u.url = value[1]; u.listen = 1; - u.default_portn = 80; + u.default_port = 80; if (ngx_parse_url(cf, &u) != NGX_OK) { if (u.err) { @@ -2411,7 +2411,7 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx ls->family = AF_INET; ls->addr = u.addr.in_addr; - ls->port = u.portn; + ls->port = u.port; ls->file_name = cf->conf_file->file.name; ls->line = cf->conf_file->line; ls->conf.backlog = -1; diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -2598,7 +2598,6 @@ ngx_http_upstream(ngx_conf_t *cf, ngx_co value = cf->args->elts; u.host = value[1]; - u.upstream = 1; u.no_resolve = 1; uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE @@ -2722,7 +2721,7 @@ ngx_http_upstream_server(ngx_conf_t *cf, ngx_memzero(&u, sizeof(ngx_url_t)); u.url = value[1]; - u.default_portn = 80; + u.default_port = 80; if (ngx_parse_url(cf, &u) != NGX_OK) { if (u.err) { @@ -2843,8 +2842,8 @@ ngx_http_upstream_add(ngx_conf_t *cf, ng uscfp = umcf->upstreams.elts; for (i = 0; i < umcf->upstreams.nelts; i++) { - if ((uscfp[i]->port && uscfp[i]->port != u->portn) - || uscfp[i]->host.len != u->host.len + + if (uscfp[i]->host.len != u->host.len || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) != 0) { @@ -2859,10 +2858,23 @@ ngx_http_upstream_add(ngx_conf_t *cf, ng return NULL; } - if (uscfp[i]->port == 0 && u->portn && !u->no_port) { + if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) { ngx_conf_log_error(NGX_LOG_WARN, cf, 0, - "upstream \"%V\" port %d is ignored", - &u->host, u->portn); + "upstream \"%V\" may not have port %d", + &u->host, u->port); + return NULL; + } + + if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) { + ngx_log_error(NGX_LOG_WARN, cf->log, 0, + "upstream \"%V\" may not have port %d in %s:%ui", + &u->host, uscfp[i]->port, + uscfp[i]->file_name.data, uscfp[i]->line); + return NULL; + } + + if (uscfp[i]->port != u->port) { + continue; } return uscfp[i]; @@ -2877,7 +2889,8 @@ ngx_http_upstream_add(ngx_conf_t *cf, ng uscf->host = u->host; uscf->file_name = cf->conf_file->file.name; uscf->line = cf->conf_file->line; - uscf->port = u->portn; + uscf->port = u->port; + uscf->default_port = u->default_port; if (u->naddrs == 1) { uscf->servers = ngx_array_create(cf->pool, 1, diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h --- a/src/http/ngx_http_upstream.h +++ b/src/http/ngx_http_upstream.h @@ -94,6 +94,7 @@ struct ngx_http_upstream_srv_conf_s { ngx_str_t file_name; ngx_uint_t line; in_port_t port; + in_port_t default_port; }; diff --git a/src/http/ngx_http_upstream_round_robin.c b/src/http/ngx_http_upstream_round_robin.c --- a/src/http/ngx_http_upstream_round_robin.c +++ b/src/http/ngx_http_upstream_round_robin.c @@ -61,10 +61,17 @@ ngx_http_upstream_init_round_robin(ngx_c /* an upstream implicitly defined by proxy_pass, etc. */ + if (us->port == 0 && us->default_port == 0) { + ngx_log_error(NGX_LOG_EMERG, cf->log, 0, + "no port in upstream \"%V\" in %s:%ui", + &us->host, us->file_name.data, us->line); + return NGX_ERROR; + } + ngx_memzero(&u, sizeof(ngx_url_t)); u.host = us->host; - u.portn = us->port; + u.port = us->port ? us->port : us->default_port; if (ngx_inet_resolve_host(cf, &u) != NGX_OK) { if (u.err) { @@ -76,13 +83,6 @@ ngx_http_upstream_init_round_robin(ngx_c return NGX_ERROR; } - if (us->port == 0) { - ngx_log_error(NGX_LOG_EMERG, cf->log, 0, - "no port in upstream \"%V\" in %s:%ui", - &us->host, us->file_name.data, us->line); - return NGX_ERROR; - } - n = u.naddrs; peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) diff --git a/src/imap/ngx_imap_auth_http_module.c b/src/imap/ngx_imap_auth_http_module.c --- a/src/imap/ngx_imap_auth_http_module.c +++ b/src/imap/ngx_imap_auth_http_module.c @@ -1291,7 +1291,7 @@ ngx_imap_auth_http(ngx_conf_t *cf, ngx_c ngx_memzero(&u, sizeof(ngx_url_t)); u.url = value[1]; - u.default_portn = 80; + u.default_port = 80; u.uri_part = 1; u.one_addr = 1; @@ -1304,7 +1304,7 @@ ngx_imap_auth_http(ngx_conf_t *cf, ngx_c ahcf->peer = u.addrs; - ahcf->host_header = u.host_header; + ahcf->host_header = u.host; ahcf->uri = u.uri; if (ahcf->uri.len == 0) { diff --git a/src/imap/ngx_imap_core_module.c b/src/imap/ngx_imap_core_module.c --- a/src/imap/ngx_imap_core_module.c +++ b/src/imap/ngx_imap_core_module.c @@ -541,7 +541,7 @@ ngx_imap_core_listen(ngx_conf_t *cf, ngx for (i = 0; i < cmcf->listen.nelts; i++) { - if (imls[i].addr != u.addr.in_addr || imls[i].port != u.portn) { + if (imls[i].addr != u.addr.in_addr || imls[i].port != u.port) { continue; } @@ -558,7 +558,7 @@ ngx_imap_core_listen(ngx_conf_t *cf, ngx ngx_memzero(imls, sizeof(ngx_imap_listen_t)); imls->addr = u.addr.in_addr; - imls->port = u.portn; + imls->port = u.port; imls->family = AF_INET; imls->ctx = cf->ctx; diff --git a/src/mysql/ngx_http_mysql_test.c b/src/mysql/ngx_http_mysql_test.c --- a/src/mysql/ngx_http_mysql_test.c +++ b/src/mysql/ngx_http_mysql_test.c @@ -185,7 +185,7 @@ ngx_http_mysql_test(ngx_conf_t *cf, ngx_ ngx_memzero(&u, sizeof(ngx_url_t)); u.url = value[1]; - u.default_portn = 3306; + u.default_port = 3306; if (ngx_parse_url(cf, &u) != NGX_OK) { if (u.err) {