Add HeaderUtil::withNoCacheHeaders()
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 6 Aug 2021 13:36:30 +0000 (15:36 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 11 Aug 2021 14:29:52 +0000 (16:29 +0200)
wcfsetup/install/files/lib/util/HeaderUtil.class.php

index 8c62084ba2ee4ca547872fb1bad2d948829f6c59..c67db8f972496ed523ead551fc5bb723e2f93a65 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\util;
 
+use Psr\Http\Message\ResponseInterface;
 use wcf\system\application\ApplicationHandler;
 use wcf\system\event\EventHandler;
 use wcf\system\request\RequestHandler;
@@ -106,6 +107,30 @@ final class HeaderUtil
         @\header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
     }
 
+    /**
+     * Returns an response matching the given response with headers preventing in-browser
+     * caching attached.
+     *
+     * @since 5.5
+     */
+    public static function withNoCacheHeaders(ResponseInterface $response): ResponseInterface
+    {
+        return $response
+            ->withHeader(
+                'cache-control',
+                [
+                    'max-age=0',
+                    'no-cache',
+                    'no-store',
+                    'must-revalidate',
+                ]
+            )
+            ->withHeader(
+                'last-modified',
+                \gmdate('D, d M Y H:i:s') . ' GMT'
+            );
+    }
+
     /**
      * @deprecated 5.4 - This method is a no-op, as gzip support was removed.
      */