Skip to content

Commit

Permalink
zEcho now sends string to cacheControl after compression and also…
Browse files Browse the repository at this point in the history
… adds a postfix, if compression was applied
  • Loading branch information
Simbiat committed Jul 23, 2022
1 parent 0c240d2 commit 702ac11
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion doc/Headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/HTTP20/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -800,15 +800,18 @@ 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.
#GZipping the string
$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.
Expand Down
4 changes: 2 additions & 2 deletions src/HTTP20/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 702ac11

Please sign in to comment.