]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Refactor and fix edge cases in switch_split_user_domain
authorTravis Cross <tc@traviscross.com>
Sun, 2 Mar 2014 09:13:05 +0000 (09:13 +0000)
committerTravis Cross <tc@traviscross.com>
Sun, 2 Mar 2014 09:29:23 +0000 (09:29 +0000)
We were incorrectly parsing usernames and domains starting with "sip"
if there was no sip: or sips: scheme in the string.

We were also incorrectly parsing usernames containing a colon even if
a scheme was given.

This also refactors the function for hopefully greater clarity.

src/switch_utils.c

index 602cbc7d0ddf44890473cb4ef4ce507c928599c3..77c7bd2c1fd07e1ffde09bb04983b609281b3c70 100644 (file)
@@ -3104,55 +3104,24 @@ SWITCH_DECLARE(int) switch_tod_cmp(const char *exp, int val)
 
 SWITCH_DECLARE(int) switch_split_user_domain(char *in, char **user, char **domain)
 {
-       char *p = NULL, *h = NULL, *u;
+       char *p = NULL, *h = NULL, *u = NULL;
 
-       if (!in) {
-               return 0;
-       }
-
-       if (!strncasecmp(in, "sip", 3)) {
-               in += 3;
-               if (*in == 's') in++;
-               if (*in == ':') in++;
-       }
+       if (!in) return 0;
 
-       u = in;
+       /* Remove URL scheme */
+       if (!strncasecmp(in, "sip:", 4)) in += 4;
+       if (!strncasecmp(in, "sips:", 5)) in += 5;
 
-       /* First isolate the host part from the user part */
-       if ((h = strchr(u, '@'))) {
-               *h++ = '\0';
-       } else {
-               u = NULL;
-               h = in;
-       }
-
-       /* Clean out the user part of its protocol prefix (if any) */
-       if (u && (p = strchr(u, ':'))) {
-               *p++ = '\0';
-               u = p;
-       }
+       /* Isolate the host part from the user part */
+       if ((h = in, p = strchr(h, '@'))) *p = '\0', u = in, h = p+1;
 
        /* Clean out the host part of any suffix */
-       if (h) {
-               if ((p = strchr(h, ':'))) {
-                       *p = '\0';
-               }
-
-               if ((p = strchr(h, ';'))) {
-                       *p = '\0';
-               }
-
-               if ((p = strchr(h, ' '))) {
-                       *p = '\0';
-               }
-       }
-       if (user) {
-               *user = u;
-       }
-       if (domain) {
-               *domain = h;
-       }
+       if ((p = strchr(h, ':'))) *p = '\0';
+       if ((p = strchr(h, ';'))) *p = '\0';
+       if ((p = strchr(h, ' '))) *p = '\0';
 
+       if (user) *user = u;
+       if (domain) *domain = h;
        return 1;
 }