From: robertc <> Date: Mon, 9 Jun 2003 10:41:36 +0000 (+0000) Subject: Summary: Refactor urlHostname. X-Git-Tag: SQUID_3_0_PRE1~141 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77bfc32480b7561102d80c3dfa038f791ad61a5a;p=thirdparty%2Fsquid.git Summary: Refactor urlHostname. 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. --- diff --git a/src/url.cc b/src/url.cc index 22b1840b71..f44815610e 100644 --- a/src/url.cc +++ b/src/url.cc @@ -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