From 8e1b2bc6e47bf7c845fb3dc59b9661f318b30ae3 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 22 Dec 2015 11:54:20 +0200 Subject: [PATCH] FastCGI: Use Apache's mod_deflate if Grav's builtin gzip compression was turned off (#548) --- system/src/Grav/Common/Grav.php | 62 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index bb987f7519..74e973f1d4 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -417,41 +417,49 @@ public function fireEvent($eventName, Event $event = null) */ public function shutdown() { - if ($this['config']->get('system.debugger.shutdown.close_connection')) { - // Prevent user abort. - if (function_exists('ignore_user_abort')) { - @ignore_user_abort(true); - } + // Prevent user abort allowing onShutdown event to run without interruptions. + if (function_exists('ignore_user_abort')) { + @ignore_user_abort(true); + } - // Close the session. - if (isset($this['session'])) { - $this['session']->close(); - } + // Close the session allowing new requests to be handled. + if (isset($this['session'])) { + $this['session']->close(); + } - if ($this['config']->get('system.cache.gzip')) { - // Flush gzhandler buffer if gzip was enabled. - ob_end_flush(); - } else { - // Otherwise prevent server from compressing the output. - header('Content-Encoding: none'); - } + if ($this['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. - // Get length and close the connection. - header('Content-Length: ' . ob_get_length()); - header("Connection: close"); + // 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; - // flush the regular buffer - ob_end_flush(); - @ob_flush(); - flush(); + if (!$success) { + // Unfortunately without FastCGI there is no way to close the connection. We need to ask browser to + // close the connection for us. - // Fix for fastcgi close connection issue. - if (function_exists('fastcgi_finish_request')) { - @fastcgi_finish_request(); - } + if ($this['config']->get('system.cache.gzip')) { + // Flush gzhandler buffer if gzip setting was enabled. + ob_end_flush(); + + } else { + // 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. + header('Content-Encoding: none'); + } + + // Get length and close the connection. + header('Content-Length: ' . ob_get_length()); + header("Connection: close"); + // Finally flush the regular buffer. + ob_end_flush(); + @ob_flush(); + flush(); + } } + // Run any time consuming tasks. $this->fireEvent('onShutdown'); }