]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Regression fix: vhost and defaultsite causing vport to be ignored
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 22 Jul 2011 13:36:58 +0000 (01:36 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 22 Jul 2011 13:36:58 +0000 (01:36 +1200)
Instead of dropping it completely we should be sanely combining them like
Squid-2 does for most cases. This appears to have been lost while removing
the getmyHostname() from the process and reducing the
prepareTransparentUrl code.

This fix makes vport apply even if vhost was used. It will modify the
Host: header contents according to the documented vport semantics.

This fix makes vport apply even if defaultsite= was used. It will append
the specified port to the domain name given. Domains with port attached
are not supported and will produce invalid URLs.
TODO: detect this case while parsing the initial config and warn.

src/cf.data.pre
src/client_side.cc

index fe406af88fd9774f8acbfd78189f6b1798eb2968..2448409763198d8bbea1ad6e3ef09ae1a8215317 100644 (file)
@@ -1320,13 +1320,14 @@ DOC_START
                        Implies accel.
 
           vhost        Using the Host header for virtual domain support.
-                       Also uses the port as specified in Host: header.
+                       Also uses the port as specified in Host: header unless
+                       overridden by the vport option.
 
-          vport        IP based virtual host support. Using the http_port number
-                       in passed on Host: headers.
+          vport        Virtual host port support. Using the http_port number
+                       instead of the port passed on Host: headers.
 
-          vport=NN     Uses the specified port number rather than the
-                       http_port number.
+          vport=NN     Virtual host port support. Using the specified port
+                       number instead of the port passed on Host: headers.
 
           protocol=    Protocol to reconstruct accelerated requests with.
                        Defaults to http://.
index 8c41af18ca6fb1ad9a6eded7d9821693d0d47ef5..f6ef921dcf603491039cab8516a458caefd36817 100644 (file)
@@ -2006,9 +2006,26 @@ prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url,
         return;
     }
 
+    if (vport < 0)
+        vport = http->getConn()->clientConnection->local.GetPort();
+
     const bool switchedToHttps = conn->switchedToHttps();
     const bool tryHostHeader = vhost || switchedToHttps;
     if (tryHostHeader && (host = mime_get_header(req_hdr, "Host")) != NULL) {
+        debugs(33, 5, "ACCEL VHOST REWRITE: vhost=" << host << " + vport=" << vport);
+        char thost[256];
+        if (vport > 0) {
+            thost[0] = '\0';
+            char *t = NULL;
+            if (host[strlen(host)] != ']' && (t = strrchr(host,':')) != NULL) {
+                strncpy(thost, host, (t-host));
+                snprintf(thost+(t-host), sizeof(thost)-(t-host), ":%d", vport);
+                host = thost;
+            } else if (!t) {
+                snprintf(thost, sizeof(thost), "%s:%d",host, vport);
+                host = thost;
+            }
+        } // else nothing to alter port-wise.
         int url_sz = strlen(url) + 32 + Config.appendDomainLen +
                      strlen(host);
         http->uri = (char *)xcalloc(url_sz, 1);
@@ -2016,24 +2033,22 @@ prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url,
                                "https" : conn->port->protocol;
         snprintf(http->uri, url_sz, "%s://%s%s", protocol, host, url);
         debugs(33, 5, "ACCEL VHOST REWRITE: '" << http->uri << "'");
-    } else if (conn->port->defaultsite) {
+    } else if (conn->port->defaultsite /* && !vhost */) {
+        debugs(33, 5, "ACCEL DEFAULTSITE REWRITE: defaultsite=" << conn->port->defaultsite << " + vport=" << vport);
         int url_sz = strlen(url) + 32 + Config.appendDomainLen +
                      strlen(conn->port->defaultsite);
         http->uri = (char *)xcalloc(url_sz, 1);
-        snprintf(http->uri, url_sz, "%s://%s%s",
-                 conn->port->protocol, conn->port->defaultsite, url);
+        char vportStr[32];
+        vportStr[0] = '\0';
+        if (vport > 0) {
+            snprintf(vportStr, sizeof(vportStr),":%d",vport);
+        }
+        snprintf(http->uri, url_sz, "%s://%s%s%s",
+                 conn->port->protocol, conn->port->defaultsite, vportStr, url);
         debugs(33, 5, "ACCEL DEFAULTSITE REWRITE: '" << http->uri <<"'");
-    } else if (vport == -1) {
-        /* Put the local socket IP address as the hostname.  */
-        int url_sz = strlen(url) + 32 + Config.appendDomainLen;
-        http->uri = (char *)xcalloc(url_sz, 1);
-        http->getConn()->clientConnection->local.ToHostname(ipbuf,MAX_IPSTRLEN);
-        snprintf(http->uri, url_sz, "%s://%s:%d%s",
-                 http->getConn()->port->protocol,
-                 ipbuf, http->getConn()->clientConnection->local.GetPort(), url);
-        debugs(33, 5, "ACCEL VPORT REWRITE: '" << http->uri << "'");
-    } else if (vport > 0) {
-        /* Put the local socket IP address as the hostname, but static port  */
+    } else if (vport > 0 /* && (!vhost || no Host:) */) {
+        debugs(33, 5, "ACCEL VPORT REWRITE: http_port IP + vport=" << vport);
+        /* Put the local socket IP address as the hostname, with whatever vport we found  */
         int url_sz = strlen(url) + 32 + Config.appendDomainLen;
         http->uri = (char *)xcalloc(url_sz, 1);
         http->getConn()->clientConnection->local.ToHostname(ipbuf,MAX_IPSTRLEN);
@@ -2223,6 +2238,7 @@ parseHttpRequest(ConnStateData *csd, HttpParser *hp, HttpRequestMethod * method_
 
 #endif
 
+    debugs(33,5, HERE << "repare absolute URL from " << (csd->transparent()?"intercept":(csd->port->accel ? "accel":"")));
     /* Rewrite the URL in transparent or accelerator mode */
     /* NP: there are several cases to traverse here:
      *  - standard mode (forward proxy)