Skip to content

Commit

Permalink
feat: Use origin host name & path for cache name to prevent serving s…
Browse files Browse the repository at this point in the history
…ame file on different host
  • Loading branch information
istiak-tridip committed Feb 25, 2021
1 parent 2d15f10 commit b17a4e3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function send(string $path): void
$this->checkIfPathIsAllowed($path);

try {
if (!$this->cache()->contains($path)) {
$key = $this->getCacheKey($path);
if (!$this->cache()->contains($key)) {
$this->fetchFileContent($path);
}

Expand All @@ -44,7 +45,8 @@ protected function fetchFileContent(string $path): void
$client = $this->client();
$response = $client->get($path);

if (!$this->cacheFileContent($path, $response->getBody())) {
$key = $this->getCacheKey($path);
if (!$this->cacheFileContent($key, $response->getBody())) {
throw new Exception("Failed to cache file content.");
}
}
Expand Down Expand Up @@ -96,6 +98,14 @@ protected function pathMatches(string $pattern, string $path): bool
}

$path = trim($path, "/");

return preg_match('#^' . $pattern . '\z#u', $path) === 1;
}

protected function getCacheKey(string $path): string
{
$host = parse_url($this->config["origin"]["base_uri"], PHP_URL_HOST);

return $host . "::" . $path;
}
}

0 comments on commit b17a4e3

Please sign in to comment.