[php-src] Issue #7890: parse_url() and incorrect port definition
| From: | MioVisman | Date: | Thu, 06 Jan 2022 13:42:40 +0000 |
| Subject: | [php-src] Issue #7890: parse_url() and incorrect port definition | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238818@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7890
Comment Author: MioVisman
I believe the parse_url() function should be fixed. url starting with // are used everywhere on the
web. And regular expressions are slower + require additional checks if they are made less complex.
This simple regex
```
%^
(?:([a-z][a-z0-9+.-]+):)?+
(?://
(?:([^@/?#]*)@)?+
(\[[^/?#\[\]]+\]|[^:/?#\[\]]+)
(?::(\d+))?+
(?=[/?#]|$)
)?
((?!(?(3)^|//))(?!\s)[^?#]+)?
(?:\?([^#]*))?
(?:\#([^\s]*))?
(?<!\s)$%ix
```
is 2-3 times slower than parse_url() on my computer.
So far I made such a bike:
```
function url(string $url): string
{
if (! isset($url[2])) {
return '';
} elseif ('#' === $url[0]) {
$schemeOn = false;
$hostOn = false;
$url = 'http://a.a' . $url;
} elseif ('/' === $url[0]) {
if ('/' === $url[1]) {
$schemeOn = false;
$hostOn = true;
$url = 'http:' . $url;
} else {
$schemeOn = false;
$hostOn = false;
$url = 'http://a.a' . $url;
}
} elseif (! \preg_match('%^[a-z][a-z0-9+.-]{0,9}:%i', $url)) {
return '';
} else {
$schemeOn = true;
$hostOn = true;
}
$p = \parse_url($url);
if (! \is_array($p)) {
return '';
}
...
```