diff --git a/doc/Headers.md b/doc/Headers.md index a0ae5bc..dfadabb 100644 --- a/doc/Headers.md +++ b/doc/Headers.md @@ -22,7 +22,7 @@ I hope that at some point this will become popular. While some frameworks do see ## cacheControl ```php -cacheControl(string $string, string $cacheStrat = '', bool $exit = false); +cacheControl(string $string, string $cacheStrat = '', bool $exit = false, string $postfix =''); ``` Allows you to send appropriate `Cache-Control` headers (refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control for explanation on parameters): ```php @@ -53,6 +53,7 @@ switch (strtolower($cacheStrat)) { ``` ETag processing using `eTag()` will happen regardless (unless string is empty: then it will simply make no sense). `$exit` if set to `true` will exit the script right after HTTP 304 is sent (that is we hit the cache). +`$postfix` is an optional string to add to `eTag` string and used mainly for [zEcho](doc/Common.md#zEcho), to comply with recommendations when using compression. ## eTag ```php diff --git a/src/HTTP20/Common.php b/src/HTTP20/Common.php index 37947ac..b03b5e5 100644 --- a/src/HTTP20/Common.php +++ b/src/HTTP20/Common.php @@ -790,7 +790,7 @@ public function zEcho(string $string, string $cacheStrat = '', bool $exit = true if (session_status() === PHP_SESSION_ACTIVE) { session_write_close(); } - (new Headers)->cacheControl($string, $cacheStrat, true); + $postfix = ''; if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { #Attempt brotli compression, if available and client supports it if (extension_loaded('brotli') && str_contains($_SERVER['HTTP_ACCEPT_ENCODING'], 'br')) { @@ -800,6 +800,7 @@ public function zEcho(string $string, string $cacheStrat = '', bool $exit = true $string = brotli_compress($string, 11, BROTLI_TEXT); #Send header with format @header('Content-Encoding: br'); + $postfix = '-br'; #Check that zlib is loaded and client supports GZip. We are ignoring Deflate because of known inconsistencies with how it is handled by browsers depending on whether it is wrapped in Zlib or not. } else if (extension_loaded('zlib') && str_contains($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) { #It is recommended to use ob_gzhandler or zlib.output_compression, but I am getting inconsistent results with headers when using them, thus this "direct" approach. @@ -807,8 +808,10 @@ public function zEcho(string $string, string $cacheStrat = '', bool $exit = true $string = gzcompress($string, 9, FORCE_GZIP); #Send header with format @header('Content-Encoding: gzip'); + $postfix = '-gzip'; } } + (new Headers)->cacheControl($string, $cacheStrat, true, $postfix); #Send header with length @header('Content-Length: '.strlen($string)); #Some HTTP methods do not support body, thus we need to ensure it's not sent. diff --git a/src/HTTP20/Headers.php b/src/HTTP20/Headers.php index 5dcbd7b..7ce7eb5 100644 --- a/src/HTTP20/Headers.php +++ b/src/HTTP20/Headers.php @@ -539,7 +539,7 @@ public function lastModified(int|string $modTime = 0, bool $exit = false): self } #Function to prepare and send cache-related headers - public function cacheControl(string $string = '', string $cacheStrat = '', bool $exit = false): self + public function cacheControl(string $string = '', string $cacheStrat = '', bool $exit = false, string $postfix = ''): self { #Send headers related to cache based on strategy selected #Some strategies are derived from https://csswizardry.com/2019/03/cache-control-for-civilians/ @@ -573,7 +573,7 @@ public function cacheControl(string $string = '', string $cacheStrat = '', bool @header('Vary: Save-Data, Accept-Encoding', false); #Set ETag if (!empty($string)) { - $this->eTag(hash('sha3-256', $string), $exit); + $this->eTag(hash('sha3-256', $string).$postfix, $exit); } return $this; }