]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Summary: Refactor urlHostname.
authorrobertc <>
Mon, 9 Jun 2003 10:41:36 +0000 (10:41 +0000)
committerrobertc <>
Mon, 9 Jun 2003 10:41:36 +0000 (10:41 +0000)
Keywords:

urlHostname reused the same variable for both constant and non-constant operations. This leads to warnings on some compilers. Refactored urlHostname to be a class, allowing greater flexability, and to use appropriate const corrent variables for accessing strchr() results.

src/url.cc

index 22b1840b712361075a884706b0c8551a818569d7..f44815610e63d6bcb9d35a842d3b73a31653152b 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: url.cc,v 1.144 2003/03/09 12:29:41 robertc Exp $
+ * $Id: url.cc,v 1.145 2003/06/09 04:41:36 robertc Exp $
  *
  * DEBUG: section 23    URL Parsing
  * AUTHOR: Duane Wessels
@@ -706,35 +706,89 @@ urlCheckRequest(const request_t * r)
  *      Look for an ending '/' or ':' and terminate
  *      Look for login info preceeded by '@'
  */
+
+class URLHostName
+{
+
+public:
+    char * extract(char const *url);
+
+private:
+    static char Host [SQUIDHOSTNAMELEN];
+    void init(char const *);
+    void findHostStart();
+    void trimTrailingChars();
+    void trimAuth();
+    char const *hostStart;
+    char const *url;
+};
+
 char *
 urlHostname(const char *url)
 {
-    LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN);
-    char *t;
-    host[0] = '\0';
+    return URLHostName().extract(url);
+}
 
-    if (NULL == (t = strchr(url, ':')))
-        return NULL;
+char URLHostName::Host[SQUIDHOSTNAMELEN];
+
+void
+URLHostName::init(char const *aUrl)
+{
+    Host[0] = '\0';
+    url = url;
+}
 
-    t++;
+void
+URLHostName::findHostStart()
+{
+    if (NULL == (hostStart = strchr(url, ':')))
+        return;
 
-    while (*t != '\0' && *t == '/')
-        t++;
+    ++hostStart;
 
-    xstrncpy(host, t, SQUIDHOSTNAMELEN);
+    while (*hostStart != '\0' && *hostStart == '/')
+        ++hostStart;
+}
 
-    if ((t = strchr(host, '/')))
+void
+URLHostName::trimTrailingChars()
+{
+    char *t;
+
+    if ((t = strchr(Host, '/')))
         *t = '\0';
 
-    if ((t = strchr(host, ':')))
+    if ((t = strchr(Host, ':')))
         *t = '\0';
+}
 
-    if ((t = strrchr(host, '@'))) {
+void
+URLHostName::trimAuth()
+{
+    char *t;
+
+    if ((t = strrchr(Host, '@'))) {
         t++;
-        xmemmove(host, t, strlen(t) + 1);
+        xmemmove(Host, t, strlen(t) + 1);
     }
+}
+
+char *
+URLHostName::extract(char const *aUrl)
+{
+    init(aUrl);
+    findHostStart();
+
+    if (hostStart == NULL)
+        return NULL;
+
+    xstrncpy(Host, hostStart, SQUIDHOSTNAMELEN);
+
+    trimTrailingChars();
+
+    trimAuth();
 
-    return host;
+    return Host;
 }
 
 static void