StaticController: Set a maximum age and honor no-cache for gravatar requests

refs #2917
This commit is contained in:
Johannes Meyer 2017-08-10 13:44:35 +02:00
parent b7639651e8
commit 7e358f3619
1 changed files with 11 additions and 8 deletions

View File

@ -31,19 +31,22 @@ class StaticController extends Controller
public function gravatarAction()
{
$response = $this->getResponse();
$response->setHeader('Pragma', 'cache')
->setHeader('Cache-Control', 'public');
$response->setHeader('Cache-Control', 'public, max-age=1814400, stale-while-revalidate=604800', true);
$noCache = $this->getRequest()->getHeader('Cache-Control') === 'no-cache'
|| $this->getRequest()->getHeader('Pragma') === 'no-cache';
$cache = FileCache::instance();
$filename = md5(strtolower(trim($this->getParam('email'))));
$cacheFile = 'gravatar-' . $filename;
if ($etag = $cache->etagMatchesCachedFile($cacheFile)) {
$response->setHttpResponseCode(304);
return;
}
$response->setHeader('Content-Type', 'image/jpg');
if ($cache->has($cacheFile)) {
if (! $noCache && $cache->has($cacheFile, time() - 1814400)) {
if ($cache->etagMatchesCachedFile($cacheFile)) {
$response->setHttpResponseCode(304);
return;
}
$response->setHeader('Content-Type', 'image/jpg', true);
$response->setHeader('ETag', sprintf('"%s"', $cache->etagForCachedFile($cacheFile)));
$cache->send($cacheFile);
return;