[php-src] Issue #7890: parse_url() and incorrect port definition
| From: | KapitanOczywisty | Date: | Thu, 06 Jan 2022 22:39:37 +0000 |
| Subject: | [php-src] Issue #7890: parse_url() and incorrect port definition | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238828@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7890
Comment Author: KapitanOczywisty
Regex is crazy fast if properly written (backtracking is the common performance killer; debug with
https://regex101.com/ or https://www.debuggex.com/), and even 20 times is nothing,
unless the only job of your app is parsing urls. Do not optimize prematurely.
parse_url is ok-ish for known urls (e.g. from config file), anything from user should
go through packages like [league/uri](https://packagist.org/packages/league/uri) anyway.
As for this specific url: it is technically valid as a relative url, so if
parse_url can parse relative paths, should also parse that url. OP is probably
referring to use of //... in 3rd-party scripts in the past, but at this point https://` should be used instead. And this works because url is treated
as relative to page origin. If you try to parse url in JS you will get error, but if you also
provide base url, the parsing is successful.
```js
new URL('//www.examle.com/ddd:65535/')
// TypeError: URL constructor: //www.examle.com/ddd:65535/ is not a valid URL.
new URL('//www.examle.com/ddd:65535/', 'http://foo.com')
// URL { href: "http://www.examle.com/ddd:65535/", origin:
"http://www.examle.com", protocol: "http:", username: "", password: "", host:
"www.examle.com", hostname: "www.examle.com", port: "", pathname:
"/ddd:65535/", search: "" }
```