Weedpacket;11062021 wrote:Well, since you mentioned it... here's an experimental thing to see what sort of work it would entail.
Amazing!
Trying to use it (PHP 7.0.15-0ubuntu0.16.04.4) like so:
require_once "weedpacket.php";
$urls = array(
"",
"Buy It Now",
"localhost/foo/bar",
"blarg",
"blarg/",
"blarg/some/path/file.ext",
"http://google.com",
"http://google.com/",
"http://google.com/some/path.ext",
"http://google.com/some/path.ext?foo=bar",
"example.com",
"example.com/",
"example.com/some/path/file.ext",
"example.com/some/path/file.ext?foo=bar",
"example.com:1234",
"example.com:1234/",
"example.com:1234/some/path/file.ext",
"example.com:1234/some/path/file.ext?foo=bar",
"//foobar.com",
"//foobar.com/",
"//foobar.com/path/file.txt",
"//cdn.example.com/js_file.js"
);
foreach($urls as $u) {
echo "url: $u\n";
try {
$v = URI\URI::parse($u);
echo "OK?\n";
} catch (Exception $e) {
echo "EXCEPTION: " . $e->getMessage() . "\n";
}
echo "\n\n";
}
yields some errors:
PHP Parse error: syntax error, unexpected '=' in /home/jaith/2017-05-16-url-check/weedpacket.php on line 179
Change that line to this:
list($userinfo, $host, $port) = self::parse_authority($authority);
solves the problem (at least temporarily -- not sure my change preserves the operation intended?
But then I get this error:
PHP Fatal error: Cannot make static method URI\URI::parse() non static in class URI\HttpUri in /home/jaith/2017-05-16-url-check/weedpacket.php on line 275
The problem seems to be that URI::parse is a static function and called statically, whereas HttpUri::parse is not static and refers to $this so it cannot be static.
I modified the HttpUri class to try and remedy the situation:
class HttpUri extends URI
{
// http-URI = "http:" "//" authority path-abempty [ "?" query ] [ "#"
// fragment ]
public static function parse(string $str): HttpUri
{
$v = parent::parse($str);
// We have to have an authority (rather, a host, because we've parsed
// userinfo and port already).
if($v->scheme !== 'http' || $v->scheme !== 'https')
{
throw new URIException('scheme part does not identify http(s) URI');
}
if($v->host === null)
{
throw new UriException('http(s) URIs require an authority part');
}
}
}
But this leads to complaints about the type hinting:
PHP Fatal error: Declaration of URI\HttpUri::parse(string $str): URI\HttpUri must be compatible with URI\URI::parse(string $str): URI\URI in /home/jaith/2017-05-16-url-check/weedpacket.php on line 275
remove type hint from URI::parse and things seem better.
The output:
url:
EXCEPTION: Invalid URI. Preliminary regex failed.
url: Buy It Now
EXCEPTION: Invalid URI. Preliminary regex failed.
url: localhost/foo/bar
EXCEPTION: Invalid URI. Preliminary regex failed.
url: blarg
EXCEPTION: Invalid URI. Preliminary regex failed.
url: blarg/
EXCEPTION: Invalid URI. Preliminary regex failed.
url: blarg/some/path/file.ext
EXCEPTION: Invalid URI. Preliminary regex failed.
url: http://google.com
OK?
url: http://google.com/
OK?
url: http://google.com/some/path.ext
OK?
url: http://google.com/some/path.ext?foo=bar
OK?
url: example.com
EXCEPTION: Invalid URI. Preliminary regex failed.
url: example.com/
EXCEPTION: Invalid URI. Preliminary regex failed.
url: example.com/some/path/file.ext
EXCEPTION: Invalid URI. Preliminary regex failed.
url: example.com/some/path/file.ext?foo=bar
EXCEPTION: Invalid URI. Preliminary regex failed.
url: example.com:1234
OK?
url: example.com:1234/
OK?
url: example.com:1234/some/path/file.ext
OK?
url: example.com:1234/some/path/file.ext?foo=bar
OK?
url: //foobar.com
EXCEPTION: Invalid URI. Preliminary regex failed.
url: //foobar.com/
EXCEPTION: Invalid URI. Preliminary regex failed.
url: //foobar.com/path/file.txt
EXCEPTION: Invalid URI. Preliminary regex failed.
url: //cdn.example.com/js_file.js
EXCEPTION: Invalid URI. Preliminary regex failed.