use Friendica\Util\Network;
use Friendica\Util\Strings;
use Friendica\Util\XML;
+use GuzzleHttp\Psr7\Uri;
/**
* This class contain functions for probing URL
/**
* Remove stuff from an URI that doesn't belong there
*
- * @param string $URI
+ * @param string $rawUri
* @return string Cleaned URI
*/
- public static function cleanURI(string $URI)
+ public static function cleanURI(string $rawUri): string
{
// At first remove leading and trailing junk
- $URI = trim($URI, "@#?:/ \t\n\r\0\x0B");
+ $rawUri = trim($rawUri, "@#?:/ \t\n\r\0\x0B");
- $parts = parse_url($URI);
-
- if (empty($parts['scheme'])) {
- return $URI;
+ $uri = new Uri($rawUri);
+ if (!$uri->getScheme()) {
+ return $uri->__toString();
}
// Remove the URL fragment, since these shouldn't be part of any profile URL
- unset($parts['fragment']);
-
- $URI = Network::unparseURL($parts);
+ $uri = $uri->withFragment('');
- return $URI;
+ return $uri->__toString();
}
/**
}
}
- public function dataUri()
+ public function dataCleanUri(): array
{
return [
'@-first' => [
- 'uri' => '@Artists4Future_Muenchen@climatejustice.global',
- 'assertUri' => 'Artists4Future_Muenchen@climatejustice.global',
+ 'expected' => 'Artists4Future_Muenchen@climatejustice.global',
+ 'uri' => '@Artists4Future_Muenchen@climatejustice.global',
+ ],
+ 'no-scheme-no-fragment' => [
+ 'expected' => 'example.com/path?arg=value',
+ 'uri' => 'example.com/path?arg=value',
+ ],
+ /* This case makes little sense, both in our expectation of receiving it in any context and in the way we
+ * do not change it in Probe::cleanUri, but it doesn't seem to be the source of any terrible security hole.
+ */
+ 'no-scheme-fragment' => [
+ 'expected' => 'example.com/path?arg=value#fragment',
+ 'uri' => 'example.com/path?arg=value#fragment',
+ ],
+ 'scheme-no-fragment' => [
+ 'expected' => 'https://example.com/path?arg=value',
+ 'uri' => 'https://example.com/path?arg=value#fragment',
+ ],
+ 'scheme-fragment' => [
+ 'expected' => 'https://example.com/path?arg=value',
+ 'uri' => 'https://example.com/path?arg=value#fragment',
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataCleanUri
+ */
+ public function testCleanUri(string $expected, string $uri)
+ {
+ self::assertEquals($expected, Probe::cleanURI($uri));
+ }
+
+ public function dataUri(): array
+ {
+ return [
+ 'Artists4Future_Muenchen@climatejustice.global' => [
+ 'uri' => 'Artists4Future_Muenchen@climatejustice.global',
'assertInfos' => [
'name' => 'Artists4Future München',
'nick' => 'Artists4Future_Muenchen',
/**
* @dataProvider dataUri
*/
- public function testCleanUri(string $uri, string $assertUri, array $assertInfos)
+ public function testProbeUri(string $uri, array $assertInfos)
{
self::markTestIncomplete('hard work due mocking 19 different http-requests');
$this->httpRequestHandler->push($history);
- $cleaned = Probe::cleanURI($uri);
- self::assertEquals($assertUri, $cleaned);
- self::assertArraySubset($assertInfos, Probe::uri($cleaned, '', 0));
-
+ self::assertArraySubset($assertInfos, Probe::uri($uri, '', 0));
// Iterate over the requests and responses
foreach ($container as $transaction) {