From df3d57ab4171a83c889a396d1d44badc34f45c83 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 30 Oct 2013 15:52:57 +0100 Subject: [PATCH] Improved getFont.php to send ETag --- wcfsetup/install/files/font/getFont.php | 29 +++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/wcfsetup/install/files/font/getFont.php b/wcfsetup/install/files/font/getFont.php index ac9e0b96c9..b85027aac6 100644 --- a/wcfsetup/install/files/font/getFont.php +++ b/wcfsetup/install/files/font/getFont.php @@ -11,15 +11,6 @@ * @license GNU Lesser General Public License */ -// allow font fetching from all domains (CORS) -header('Access-Control-Allow-Origin: *'); - -// ignore request if client seems to already have fetched this file -if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) || !empty($_SERVER['HTTP_IF_NONE_MATCH'])) { - header("HTTP/1.1 304 Not Modified"); - exit; -} - // list of known font types $types = array( 'eot' => 'application/vnd.ms-fontobject', @@ -30,13 +21,29 @@ $types = array( if (!empty($_GET['type'])) { if (isset($types[$_GET['type']])) { $filename = 'fontawesome-webfont.' . $_GET['type']; + $filemtime = filemtime($filename); + + $etag = '"' . md5($filemtime . $filename) . '"'; + $clientEtag = (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : ''; + $clientLastModified = (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) ? trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) : 0; + $clientLastModified = @strtotime($clientLastModified); + + // ignore request if client seems to already have fetched this file + if (($clientLastModified && $clientEtag) ? (($clientLastModified == $filemtime) && ($clientEtag == $etag)) : ($clientLastModified == $filemtime) ) { + header("HTTP/1.1 304 Not Modified"); + exit; + } + $data = file_get_contents($filename); // send cache and type headers + // allow font fetching from all domains (CORS) + header('Access-Control-Allow-Origin: *'); header('Content-Type: ' . $types[$_GET['type']]); - header('Cache-control: max-age=31536000, private'); + header('Cache-Control: max-age=31536000, private'); + header('ETag: ' . $etag); header('Expires: ' . gmdate("D, d M Y H:i:s", time() + 31536000) . ' GMT'); - header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $filemtime) . ' GMT'); header('Content-Length: ' . strlen($data)); die($data); -- 2.20.1