diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 85d7faa09..fbf9c8b45 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -273,9 +273,21 @@ class Grav extends Container $debugger = $this['debugger']; $response = $debugger->logRequest($request, $response); - // Send the response and terminate. + $body = $response->getBody(); + + // Handle ETag and If-None-Match headers. + if ($response->getHeaderLine('ETag') === '1') { + $etag = md5($body); + $response = $response->withHeader('ETag', $etag); + + if ($request->getHeaderLine('If-None-Match') === $etag) { + $response = $response->withStatus(304); + $body = ''; + } + } + $this->header($response); - echo $response->getBody(); + echo $body; exit(); } diff --git a/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php b/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php index 0ace98b4a..aacc8f773 100644 --- a/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php +++ b/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php @@ -281,7 +281,7 @@ interface PageLegacyInterface * * @return bool show etag header */ - public function eTag($var = null); + public function eTag($var = null): bool; /** * Gets and sets the path to the .md file for this Page object. diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 093a95990..e397e2f51 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -1950,7 +1950,7 @@ class Page implements PageInterface * * @return bool show etag header */ - public function eTag($var = null) + public function eTag($var = null): bool { if ($var !== null) { $this->etag = $var; @@ -1959,7 +1959,7 @@ class Page implements PageInterface $this->etag = (bool)Grav::instance()['config']->get('system.pages.etag'); } - return $this->etag; + return $this->etag ?? false; } /** diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php index c9fb1d834..b545afb43 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php @@ -151,7 +151,7 @@ trait PageLegacyTrait // Calculate ETag based on the serialized page and modified time. if ($this->eTag()) { - $headers['ETag'] = '"' . md5(json_encode($this) . $this->modified()).'"'; + $headers['ETag'] = '1'; } // Set Vary: Accept-Encoding header. @@ -675,7 +675,7 @@ trait PageLegacyTrait 'etag', $var, static function ($value) { - return (bool)($value ?? Grav::instance()['config']->get('system.pages.last_modified')); + return (bool)($value ?? Grav::instance()['config']->get('system.pages.etag')); } ); }