1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Start on URL normalizer

This commit is contained in:
J. King 2019-08-26 22:13:30 -04:00
parent f688155ca4
commit 13c27c2536
3 changed files with 98 additions and 0 deletions

66
lib/Misc/URL.php Normal file
View file

@ -0,0 +1,66 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Misc;
class URL {
public static function normalize(string $url, string $u = null, string $p = null): string {
extract(parse_url($url));
if (!isset($scheme) || !isset($host) || !strlen($host)) {
return $url;
}
$out = strtolower($scheme)."://";
if (strlen($u ?? "")) {
$out .= self::normalizePart($u, self::P_USER, false);
if (strlen($p ?? "")) {
$out .= ":".self::normalizePart($p, self::P_PASS, false);
}
$out .= "@";
} elseif (strlen($username ?? "")) {
$out .= self::normalizePart($username, self::P_USER);
if (strlen($password ?? "")) {
$out .= ":".self::normalizePart($username, self::P_PASS);
}
$out .= "@";
}
if ($host[0] === "[") {
$out .= self::normalizeIPv6($host);
} else {
$out .= self::normalizeHost($host);
}
if (isset($path)) {
$out .= self::normalizePath($path);
} else {
$out .= "/";
}
if (isset($query) && strlen($query)) {
$out .= "?".self::normalizePart($query, self::P_QUERY);
}
return $out;
}
protected static function normalizePart(string $part, int $type, bool $passthrough_encoded = true): string {
// stub
return $part;
}
protected static function normalizeHost(string $host): string {
// stub
return $host;
}
protected static function normalizeIPv6(string $addr): string {
// stub
return $addr;
}
protected static function normalizePath(string $path): string {
// stub
return $path;
}
}

View file

@ -0,0 +1,31 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Misc;
use JKingWeb\Arsse\Misc\URL;
/** @covers \JKingWeb\Arsse\Misc\URL */
class TestURL extends \JKingWeb\Arsse\Test\AbstractTest {
public function setUp() {
self::clearData();
}
/** @dataProvider provideNormalizations */
public function testNormalizeAUrl(string $in, string $exp) {
$this->assertSame($exp, URL::normalize($in));
}
public function provideNormalizations() {
return [
["/", "/"],
["//example.com/", "//example.com/"],
["http://example.com/", "http://example.com/"],
["http://[::1]/", "http://[::1]/"],
["HTTP://example.com/", "http://example.com/"],
];
}
}

View file

@ -46,6 +46,7 @@
<file>cases/Misc/TestValueInfo.php</file>
<file>cases/Misc/TestDate.php</file>
<file>cases/Misc/TestContext.php</file>
<file>cases/Misc/TestURL.php</file>
</testsuite>
<testsuite name="User management">
<file>cases/User/TestInternal.php</file>