Skip to content

Commit

Permalink
FIX Do not suffix trailing slash to external links
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 6, 2024
1 parent 1ac3428 commit 645502c
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 74 deletions.
19 changes: 13 additions & 6 deletions src/Control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,18 @@ public static function join_links($arg = null)
*/
public static function normaliseTrailingSlash(string $url): string
{
// Do not normalise external urls
// Note that urls without a scheme such as www.example.com will be counted as a relative file
if (!Director::is_site_url($url)) {
return $url;
}

// Do not modify files e.g. some/path/myfile.txt (exclude if $url is absolute_base_url)
$extension = pathinfo(rtrim($url), PATHINFO_EXTENSION);
if ($extension && $url !== Director::config()->get('alternate_base_url')) {
return $url;
}

$querystring = null;
$fragmentIdentifier = null;

Expand All @@ -694,14 +706,9 @@ public static function normaliseTrailingSlash(string $url): string

// Normlise trailing slash
$shouldHaveTrailingSlash = Controller::config()->uninherited('add_trailing_slash');
if ($shouldHaveTrailingSlash
&& !str_ends_with($url, '/')
&& !preg_match('/^(.*)\.([^\/]*)$/', Director::makeRelative($url))
) {
// Add trailing slash if enabled and url does not end with a file extension
if ($shouldHaveTrailingSlash && !str_ends_with($url, '/')) {
$url .= '/';
} elseif (!$shouldHaveTrailingSlash) {
// Remove trailing slash if it shouldn't be there
$url = rtrim($url, '/');
}

Expand Down
6 changes: 5 additions & 1 deletion src/Control/Director.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,11 @@ public static function is_site_url($url)

// Allow extensions to weigh in
$isSiteUrl = false;
static::singleton()->extend('updateIsSiteUrl', $isSiteUrl, $url);
// not using static::singleton() here because it can, for unknown reasons, break
// functional tests such as those in HTTPCacheControlIntegrationTest
// extension hooks will still be called as expected
$director = new static();
$director->extend('updateIsSiteUrl', $isSiteUrl, $url);
if ($isSiteUrl) {
return true;
}
Expand Down
Loading

0 comments on commit 645502c

Please sign in to comment.