Skip to content

Commit

Permalink
FastCGI: Use Apache's mod_deflate if Grav's builtin gzip compression …
Browse files Browse the repository at this point in the history
…was turned off (#548)
  • Loading branch information
mahagr committed Dec 22, 2015
1 parent 37dd107 commit 8e1b2bc
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit 8e1b2bc

Please sign in to comment.