diff options
author | Andrew Dolgov <[email protected]> | 2022-03-22 12:24:31 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2022-03-22 12:24:31 +0300 |
commit | 1c4f7ab3b838b23afb2ee4dab14acbf75956e952 (patch) | |
tree | 0a19274107d717efe92d2c0376cd3105fead5a11 /classes/urlhelper.php | |
parent | 711662948768492e8d05b778a7d80eacaec368d2 (diff) |
* add phpunit as a dev dependency
* add some basic tests for UrlHelper::rewrite_relative()
* fix UrlHelper::rewrite_relative() to work better on non-absolute
relative URL paths
Diffstat (limited to 'classes/urlhelper.php')
-rw-r--r-- | classes/urlhelper.php | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/classes/urlhelper.php b/classes/urlhelper.php index 2dfb22a5d..29a9528a8 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -109,15 +109,21 @@ class UrlHelper { if (isset($rel_parts['path'])) { - // experimental: if relative url path is not absolute (i.e. starting with /) concatenate it using base url path - // (i'm not sure if it's a good idea) + // we append dirname() of base path to relative URL path as per RFC 3986 section 5.2.2 + $base_path = with_trailing_slash(dirname($base_parts['path'])); - if (strpos($rel_parts['path'], '/') !== 0) { - $rel_parts['path'] = with_trailing_slash($base_parts['path'] ?? "") . $rel_parts['path']; + // 1. absolute relative path (/test.html) = no-op, proceed as is + + // 2. dotslash relative URI (./test.html) - strip "./", append base path + if (strpos($rel_parts['path'], './') === 0) { + $rel_parts['path'] = $base_path . substr($rel_parts['path'], 2); + // 3. anything else relative (test.html) - append dirname() of base path + } else if (strpos($rel_parts['path'], '/') !== 0) { + $rel_parts['path'] = $base_path . $rel_parts['path']; } - $rel_parts['path'] = str_replace("/./", "/", $rel_parts['path']); - $rel_parts['path'] = str_replace("//", "/", $rel_parts['path']); + //$rel_parts['path'] = str_replace("/./", "/", $rel_parts['path']); + //$rel_parts['path'] = str_replace("//", "/", $rel_parts['path']); } return self::validate(self::build_url($rel_parts)); |