Example #1
0
 /**
  * Adds the needed headers and prepares the content
  * 
  * @param \Zepi\Turbo\Response\Response $response
  * @param string $type
  * @param string $hash
  * @param string $version
  * @param string $content
  */
 protected function deliverContent(Response $response, $type, $hash, $version, $content)
 {
     // Define the if modified since timestamp
     $cachedAssetTimestamp = $this->assetCacheManager->getCachedAssetTimestamp($type, $hash, $version);
     $ifModifiedSince = -1;
     if ($this->isHeaderSetAndNotEmpty('HTTP_IF_MODIFIED_SINCE')) {
         $ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
     }
     // Define the etag
     $eTag = md5($content);
     $eTagHeader = -1;
     if ($this->isHeaderSetAndNotEmpty('HTTP_IF_NONE_MATCH')) {
         $eTagHeader = $_SERVER['HTTP_IF_NONE_MATCH'];
     }
     // Set the cache headers
     $cacheTtl = 86400 * 365;
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $cachedAssetTimestamp) . ' GMT');
     header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $cacheTtl) . ' GMT');
     header('Pragma: cache');
     header('Etag: ' . $eTag);
     header('Cache-Control: max-age=' . $cacheTtl);
     // Verify the cached timestamp and the eTag
     if ($cachedAssetTimestamp === $ifModifiedSince || $eTag === $eTagHeader) {
         header('HTTP/1.1 304 Not Modified');
         exit;
     }
     // Set the content type
     $contentType = $this->getContentType($type, $version);
     if ($contentType !== '') {
         header('Content-type: ' . $contentType, true);
     }
     // Display the content
     $response->setOutput($content);
 }