Skip to content

[5.x] Fix static caching of requests with trailing dot in host #11714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/StaticCaching/Cachers/FileCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,22 @@ public function getFilePath($url, $site = null)
$pathParts = pathinfo($urlParts['path']);
$slug = $pathParts['basename'];
$query = $this->config('ignore_query_strings') ? '' : Arr::get($urlParts, 'query', '');
$sitePath = $this->getCachePath($site);

if ($this->isBasenameTooLong($basename = $slug.'_'.$query.'.html')) {
$basename = $slug.'_lqs_'.md5($query).'.html';
}

return $this->getCachePath($site).$pathParts['dirname'].'/'.$basename;
if ($this->hasTrailingDot($urlParts['host'] ?? '')) {
$sitePath .= '.';
}

return $sitePath.$pathParts['dirname'].'/'.$basename;
}

private function hasTrailingDot($hostname)
{
return str_ends_with($hostname, '.');
}

private function isBasenameTooLong($basename)
Expand Down
13 changes: 13 additions & 0 deletions tests/StaticCaching/FileCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ public function gets_file_path_with_multiple_locations()
);
}

#[Test]
public function gets_file_path_with_trailing_dot()
{
$cacher = $this->fileCacher([
'path' => 'test/path',
]);

$this->assertEquals(
'test/path./foo/bar_.html',
$cacher->getFilePath('http://domain.com./foo/bar')
);
}

#[Test]
public function flushing_the_cache_deletes_from_all_cache_locations()
{
Expand Down