]> granicus.if.org Git - php/commitdiff
Cleanup parse_url() query/fragment handling
authorNikita Popov <nikic@php.net>
Fri, 18 Nov 2016 16:00:56 +0000 (17:00 +0100)
committerNikita Popov <nikic@php.net>
Tue, 22 Nov 2016 18:24:23 +0000 (19:24 +0100)
The query/fragment handling was pretty convoluted, with many parts
being duplicated. Simplify by checking for fragment, then for query,
then for path.

ext/standard/url.c

index 94b9f20a16d800d363c17e5efcd1621ca7332974..aa1b0312fecca0c04bd6b284159112ccf778adf9 100644 (file)
@@ -289,51 +289,32 @@ PHPAPI php_url *php_url_parse_ex(char const *str, int length)
 
        just_path:
 
-       if ((p = memchr(s, '?', (ue - s)))) {
-               pp = memchr(s, '#', (ue - s));
-
-               if (pp && pp < p) {
-                       if (pp - s) {
-                               ret->path = estrndup(s, (pp-s));
-                               php_replace_controlchars_ex(ret->path, (pp - s));
-                       }
-                       p = pp;
-                       goto label_parse;
-               }
-
-               if (p - s) {
-                       ret->path = estrndup(s, (p-s));
-                       php_replace_controlchars_ex(ret->path, (p - s));
-               }
-
-               if (pp) {
-                       if (pp - ++p) {
-                               ret->query = estrndup(p, (pp-p));
-                               php_replace_controlchars_ex(ret->query, (pp - p));
-                       }
-                       p = pp;
-                       goto label_parse;
-               } else if (++p - ue) {
-                       ret->query = estrndup(p, (ue-p));
-                       php_replace_controlchars_ex(ret->query, (ue - p));
-               }
-       } else if ((p = memchr(s, '#', (ue - s)))) {
-               if (p - s) {
-                       ret->path = estrndup(s, (p-s));
-                       php_replace_controlchars_ex(ret->path, (p - s));
+       e = ue;
+       p = memchr(s, '#', (e - s));
+       if (p) {
+               p++;
+               if (p < e) {
+                       ret->fragment = estrndup(p, (e - p));
+                       php_replace_controlchars_ex(ret->fragment, (e - p));
                }
+               e = p-1;
+       }
 
-               label_parse:
+       p = memchr(s, '?', (e - s));
+       if (p) {
                p++;
-
-               if (ue - p) {
-                       ret->fragment = estrndup(p, (ue-p));
-                       php_replace_controlchars_ex(ret->fragment, (ue - p));
+               if (p < e) {
+                       ret->query = estrndup(p, (e - p));
+                       php_replace_controlchars_ex(ret->query, (e - p));
                }
-       } else {
-               ret->path = estrndup(s, (ue-s));
-               php_replace_controlchars_ex(ret->path, (ue - s));
+               e = p-1;
        }
+
+       if (s < e || s == ue) {
+               ret->path = estrndup(s, (e - s));
+               php_replace_controlchars_ex(ret->path, (e - s));
+       }
+
        return ret;
 }
 /* }}} */