From b64a4a291ce7f0700f9e6f7c059e4bec1cd65235 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Wed, 29 Oct 2014 20:16:12 +0100 Subject: [PATCH] Add ETag caching for attachments Closes #1834 --- .../files/lib/page/AttachmentPage.class.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/wcfsetup/install/files/lib/page/AttachmentPage.class.php b/wcfsetup/install/files/lib/page/AttachmentPage.class.php index c6e7de0da8..9fad13d30b 100644 --- a/wcfsetup/install/files/lib/page/AttachmentPage.class.php +++ b/wcfsetup/install/files/lib/page/AttachmentPage.class.php @@ -11,7 +11,7 @@ use wcf\util\FileReader; /** * Shows an attachment. * - * @author Marcel Werk + * @author Marcel Werk, Joshua Rüsweg * @copyright 2001-2015 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf @@ -60,6 +60,12 @@ class AttachmentPage extends AbstractPage { */ public static $inlineMimeTypes = array('image/gif', 'image/jpeg', 'image/png', 'image/x-png', 'application/pdf', 'image/pjpeg'); + /** + * etag for this attachment + * @var string + */ + public $eTag = null; + /** * @see \wcf\page\IPage::readParameters() */ @@ -120,16 +126,19 @@ class AttachmentPage extends AbstractPage { $mimeType = $this->attachment->tinyThumbnailType; $filesize = $this->attachment->tinyThumbnailSize; $location = $this->attachment->getTinyThumbnailLocation(); + $this->eTag = 'TINY_'.$this->attachmentID; } else if ($this->thumbnail) { $mimeType = $this->attachment->thumbnailType; $filesize = $this->attachment->thumbnailSize; $location = $this->attachment->getThumbnailLocation(); + $this->eTag = 'THUMB_'.$this->attachmentID; } else { $mimeType = $this->attachment->fileType; $filesize = $this->attachment->filesize; $location = $this->attachment->getLocation(); + $this->eTag = $this->attachmentID; } // init file reader @@ -144,9 +153,8 @@ class AttachmentPage extends AbstractPage { 'maxAge' => 31536000 )); - // add etag for non-thumbnail - if (!$this->thumbnail && !$this->tiny) { - $this->fileReader->addHeader('ETag', '"'.$this->attachmentID.'"'); + if ($this->eTag !== null) { + $this->fileReader->addHeader('ETag', '"'.$this->eTag.'"'); } } @@ -156,6 +164,12 @@ class AttachmentPage extends AbstractPage { public function show() { parent::show(); + // etag caching + if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == '"'.$this->eTag.'"') { + @header('HTTP/1.1 304 Not Modified'); + exit; + } + if (!$this->tiny && !$this->thumbnail) { // update download count $editor = new AttachmentEditor($this->attachment); -- 2.20.1