Skip to content

Commit f0f68c7

Browse files
committed
Cleanup parse_url() query/fragment handling
The query/fragment handling was pretty convoluted, with many parts being duplicated. Simplify by checking for fragment, then for query, then for path.
1 parent 9befad6 commit f0f68c7

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

ext/standard/url.c

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -289,51 +289,32 @@ PHPAPI php_url *php_url_parse_ex(char const *str, int length)
289289

290290
just_path:
291291

292-
if ((p = memchr(s, '?', (ue - s)))) {
293-
pp = memchr(s, '#', (ue - s));
294-
295-
if (pp && pp < p) {
296-
if (pp - s) {
297-
ret->path = estrndup(s, (pp-s));
298-
php_replace_controlchars_ex(ret->path, (pp - s));
299-
}
300-
p = pp;
301-
goto label_parse;
302-
}
303-
304-
if (p - s) {
305-
ret->path = estrndup(s, (p-s));
306-
php_replace_controlchars_ex(ret->path, (p - s));
307-
}
308-
309-
if (pp) {
310-
if (pp - ++p) {
311-
ret->query = estrndup(p, (pp-p));
312-
php_replace_controlchars_ex(ret->query, (pp - p));
313-
}
314-
p = pp;
315-
goto label_parse;
316-
} else if (++p - ue) {
317-
ret->query = estrndup(p, (ue-p));
318-
php_replace_controlchars_ex(ret->query, (ue - p));
319-
}
320-
} else if ((p = memchr(s, '#', (ue - s)))) {
321-
if (p - s) {
322-
ret->path = estrndup(s, (p-s));
323-
php_replace_controlchars_ex(ret->path, (p - s));
292+
e = ue;
293+
p = memchr(s, '#', (e - s));
294+
if (p) {
295+
p++;
296+
if (p < e) {
297+
ret->fragment = estrndup(p, (e - p));
298+
php_replace_controlchars_ex(ret->fragment, (e - p));
324299
}
300+
e = p-1;
301+
}
325302

326-
label_parse:
303+
p = memchr(s, '?', (e - s));
304+
if (p) {
327305
p++;
328-
329-
if (ue - p) {
330-
ret->fragment = estrndup(p, (ue-p));
331-
php_replace_controlchars_ex(ret->fragment, (ue - p));
306+
if (p < e) {
307+
ret->query = estrndup(p, (e - p));
308+
php_replace_controlchars_ex(ret->query, (e - p));
332309
}
333-
} else {
334-
ret->path = estrndup(s, (ue-s));
335-
php_replace_controlchars_ex(ret->path, (ue - s));
310+
e = p-1;
336311
}
312+
313+
if (s < e || s == ue) {
314+
ret->path = estrndup(s, (e - s));
315+
php_replace_controlchars_ex(ret->path, (e - s));
316+
}
317+
337318
return ret;
338319
}
339320
/* }}} */

0 commit comments

Comments
 (0)