From 6ba54d2b3d1a69ac8f60313bf9a4c6e56c65c765 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 19 Mar 2020 11:31:24 +0200 Subject: [PATCH] Better `Content-Encoding` handling in Apache when content compression is disabled [#2619] --- CHANGELOG.md | 1 + system/src/Grav/Common/Grav.php | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38ac9ddd9..976d247bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Added `Folder::hasChildren()` method to determine if a folder has child folders 1. [](#improved) * Save memory when updating large flex indexes + * Better `Content-Encoding` handling in Apache when content compression is disabled [#2619](https://github.com/getgrav/grav/issues/2619) 1. [](#bugfix) * Fixed creating new `Flex User` when folder storage has been selected * Fixed some bugs in Flex root page methods diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 40ad87d35..d2133cb8a 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -493,30 +493,33 @@ public function shutdown() $this['session']->close(); } - if ($this['config']->get('system.debugger.shutdown.close_connection', true)) { + /** @var Config $config */ + $config = $this['config']; + if ($config->get('system.debugger.shutdown.close_connection', true)) { // Flush the response and close the connection to allow time consuming tasks to be performed without leaving // the connection to the client open. This will make page loads to feel much faster. // FastCGI allows us to flush all response data to the client and finish the request. $success = \function_exists('fastcgi_finish_request') ? @fastcgi_finish_request() : false; - if (!$success) { // Unfortunately without FastCGI there is no way to force close the connection. // We need to ask browser to close the connection for us. - if ($this['config']->get('system.cache.gzip')) { - // Flush gzhandler buffer if gzip setting was enabled. + + if ($config->get('system.cache.gzip')) { + // Flush gzhandler buffer if gzip setting was enabled to get the size of the compressed output. ob_end_flush(); - } else { + } elseif ($config->get('system.cache.allow_webserver_gzip')) { + // Let web server to do the hard work. + header('Content-Encoding: identity'); + } elseif (function_exists('apache_setenv')) { // Without gzip we have no other choice than to prevent server from compressing the output. // This action turns off mod_deflate which would prevent us from closing the connection. - if ($this['config']->get('system.cache.allow_webserver_gzip')) { - header('Content-Encoding: identity'); - } else { - header('Content-Encoding: none'); - } + @apache_setenv('no-gzip', '1'); + } else { + // Fall back to unknown content encoding, it prevents most servers from deflating the content. + header('Content-Encoding: none'); } - // Get length and close the connection. header('Content-Length: ' . ob_get_length()); header('Connection: close');