diff --git a/src/Http/Url.php b/src/Http/Url.php index 02f1c56a..2f560234 100644 --- a/src/Http/Url.php +++ b/src/Http/Url.php @@ -413,4 +413,34 @@ public static function parseQuery(string $s): array parse_str($s, $res); return $res[0] ?? []; } + + + public static function isAbsolute(string $url): bool + { + return (bool) preg_match('#^[a-z][a-z0-9+.-]*:#i', $url); + } + + + public static function removeDotSegments(string $path): string + { + $prefix = $segment = ''; + if (str_starts_with($path, '/')) { + $prefix = '/'; + $path = substr($path, 1); + } + $segments = explode('/', $path); + $res = []; + foreach ($segments as $segment) { + if ($segment === '..') { + array_pop($res); + } elseif ($segment !== '.') { + $res[] = $segment; + } + } + + if ($segment === '.' || $segment === '..') { + $res[] = ''; + } + return $prefix . implode('/', $res); + } } diff --git a/tests/Http/Url.isAbsolute.phpt b/tests/Http/Url.isAbsolute.phpt new file mode 100644 index 00000000..fbdb409e --- /dev/null +++ b/tests/Http/Url.isAbsolute.phpt @@ -0,0 +1,15 @@ +