]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Rework urlAbsolute to be a little more streamlined.
authorBenno Rice <benno@squid-cache.org>
Mon, 1 Sep 2008 02:36:52 +0000 (12:36 +1000)
committerBenno Rice <benno@squid-cache.org>
Mon, 1 Sep 2008 02:36:52 +0000 (12:36 +1000)
The primary aim of this is to cut down on the number of ways snprintf was
called in the original version.  The idea here is to build the common base
portion of the url using snprintf and then construct the rest using str[n]cpy.
snprintf is still used as the alternative (using only POSIX routines) involves
a much longer run of code that, at least in my estimation, gains us very little
over snprintf.

src/Server.cc
src/url.cc

index 51df92fa766b7528868d6628013af510d971b342..2ec77c2efdf4af5c3d6705e45f67e087d562118c 100644 (file)
@@ -404,8 +404,10 @@ sameUrlHosts(const char *url1, const char *url2)
 static void
 purgeEntriesByHeader(const HttpRequest *req, const char *reqUrl, HttpMsg *rep, http_hdr_type hdr)
 {
-    if (const char *url = rep->header.getStr(hdr)) {
-           const char *absUrl = urlAbsolute(req, url);
+    const char *url, *absUrl;
+    
+    if ((url = rep->header.getStr(hdr)) != NULL) {
+           absUrl = urlAbsolute(req, url);
            if (absUrl != NULL) {
                url = absUrl;
            }
@@ -415,7 +417,7 @@ purgeEntriesByHeader(const HttpRequest *req, const char *reqUrl, HttpMsg *rep, h
             purgeEntriesByUrl(url);
         }
         if (absUrl != NULL) {
-            safe_free((void *)absUrl);
+            safe_free(absUrl);
         }
     }
 }
index a066e06835cee4b15b6e3ae9b8138f766cf5b5a2..a9c4aa6b1487bdb0ba5f1d1684c9abcdf249074d 100644 (file)
@@ -535,8 +535,9 @@ urlCanonicalClean(const HttpRequest * request)
 const char *
 urlAbsolute(const HttpRequest * req, const char *relUrl)
 {
-    char portbuf[32], urlbuf[MAX_URL];
-    char *path, *last_slash;
+    char urlbuf[MAX_URL];
+    const char *path, *last_slash;
+    size_t urllen, pathlen;
 
     if (relUrl == NULL) {
         return (NULL);
@@ -550,45 +551,43 @@ urlAbsolute(const HttpRequest * req, const char *relUrl)
     if (req->protocol == PROTO_URN) {
         snprintf(urlbuf, MAX_URL, "urn:%s", req->urlpath.buf());
     } else {
-       portbuf[0] = '\0';
        if (req->port != urlDefaultPort(req->protocol)) {
-           snprintf(portbuf, 32, ":%d", req->port);
+           urllen = snprintf(urlbuf, MAX_URL, "%s://%s%s%s:%d",
+                       ProtocolStr[req->protocol],
+                       req->login,
+                       *req->login ? "@" : null_string,
+                       req->GetHost(),
+                       req->port
+           );
+       } else {
+           urllen = snprintf(urlbuf, MAX_URL, "%s://%s%s%s",
+                       ProtocolStr[req->protocol],
+                       req->login,
+                       *req->login ? "@" : null_string,
+                       req->GetHost()
+           );      
        }
+       
        if (relUrl[0] == '/') {
-           snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s%s",
-                ProtocolStr[req->protocol],
-                req->login,
-                *req->login ? "@" : null_string,
-                req->GetHost(),
-                portbuf,
-                relUrl
-            );
+           strncpy(&urlbuf[urllen], relUrl, MAX_URL - urllen - 1);
        } else {
-           path = xstrdup(req->urlpath.buf());
+           path = req->urlpath.buf();
            last_slash = strrchr(path, '/');
            if (last_slash == NULL) {
-                snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s/%s",
-                    ProtocolStr[req->protocol],
-                    req->login,
-                    *req->login ? "@" : null_string,
-                    req->GetHost(),
-                    portbuf,
-                    relUrl
-                );
+                       urlbuf[urllen++] = '/';
+                       strncpy(&urlbuf[urllen], relUrl, MAX_URL - urllen - 1);
            } else {
-                last_slash++;
-                *last_slash = '\0';
-                snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s%s%s",
-                    ProtocolStr[req->protocol],
-                    req->login,
-                    *req->login ? "@" : null_string,
-                    req->GetHost(),
-                    portbuf,
-                    path,
-                    relUrl
-                );
+                       last_slash++;
+                       pathlen = last_slash - path;
+                       if (pathlen > MAX_URL - urllen - 1) {
+                           pathlen = MAX_URL - urllen - 1;
+                       }
+                       strncpy(&urlbuf[urllen], path, pathlen);
+                       urllen += pathlen;
+                       if (urllen + 1 < MAX_URL) {
+                           strncpy(&urlbuf[urllen], relUrl, MAX_URL - urllen - 1);
+                       }
            }
-           xfree(path);
        }
     }