From: Benno Rice Date: Mon, 1 Sep 2008 02:36:52 +0000 (+1000) Subject: Rework urlAbsolute to be a little more streamlined. X-Git-Tag: SQUID_3_1_0_1~49^2~62^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bbca1b8269d02b65c5d7cae5326f1a57acb5a39c;p=thirdparty%2Fsquid.git Rework urlAbsolute to be a little more streamlined. 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. --- diff --git a/src/Server.cc b/src/Server.cc index 51df92fa76..2ec77c2efd 100644 --- a/src/Server.cc +++ b/src/Server.cc @@ -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); } } } diff --git a/src/url.cc b/src/url.cc index a066e06835..a9c4aa6b14 100644 --- a/src/url.cc +++ b/src/url.cc @@ -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); } }