Commit 0d371742 authored by webchick's avatar webchick

#43462 by c960657 and andypost: Fixed issues with certain configurations of compressed pages.

parent bfedf56f
......@@ -1104,6 +1104,9 @@ function drupal_serve_page_from_cache(stdClass $cache) {
header('Vary: Accept-Encoding', FALSE);
// If page_compression is enabled, the cache contains gzipped data.
if ($return_compressed) {
// $cache->data is already gzip'ed, so make sure zlib.output_compression
// does not compress it once more.
ini_set('zlib.output_compression', '0');
header('Content-Encoding: gzip');
}
else {
......
......@@ -4322,13 +4322,13 @@ function _drupal_bootstrap_full() {
/**
* Store the current page in the cache.
*
* We try to store a gzipped version of the cache. This requires the
* PHP zlib extension (http://php.net/manual/en/ref.zlib.php).
* Presence of the extension is checked by testing for the function
* gzencode. There are two compression algorithms: gzip and deflate.
* The majority of all modern browsers support gzip or both of them.
* We thus only deal with the gzip variant and unzip the cache in case
* the browser does not accept gzip encoding.
* If page_compression is enabled, a gzipped version of the page is stored in
* the cache to avoid compressing the output on each request. The cache entry
* is unzipped in the relatively rare event that the page is requested by a
* client without gzip support.
*
* Page compression requires the PHP zlib extension
* (http://php.net/manual/en/ref.zlib.php).
*
* @see drupal_page_header
*/
......@@ -4336,8 +4336,6 @@ function drupal_page_set_cache() {
global $base_root;
if (drupal_page_is_cacheable()) {
$cache_page = TRUE;
$cache = (object) array(
'cid' => $base_root . request_uri(),
'data' => ob_get_clean(),
......@@ -4353,19 +4351,10 @@ function drupal_page_set_cache() {
$cache->headers[$header_names[$name_lower]] = $value;
}
if (variable_get('page_compression', TRUE) && function_exists('gzencode')) {
// We do not store the data in case the zlib mode is deflate. This should
// be rarely happening.
if (zlib_get_coding_type() == 'deflate') {
$cache_page = FALSE;
}
elseif (zlib_get_coding_type() == FALSE) {
if ($cache->data) {
if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
$cache->data = gzencode($cache->data, 9, FORCE_GZIP);
}
// The remaining case is 'gzip' which means the data is already
// compressed and nothing left to do but to store it.
}
if ($cache_page && $cache->data) {
cache_set($cache->cid, $cache->data, 'cache_page', $cache->expire, $cache->headers);
}
return $cache;
......
......@@ -1516,7 +1516,7 @@ protected function cronRun() {
* Either the new page content or FALSE.
*/
protected function checkForMetaRefresh() {
if ($this->drupalGetContent() != '' && $this->parse()) {
if (strpos($this->drupalGetContent(), '<meta ') && $this->parse()) {
$refresh = $this->xpath('//meta[@http-equiv="Refresh"]');
if (!empty($refresh)) {
// Parse the content attribute of the meta tag for the format:
......
......@@ -91,7 +91,7 @@ class BootstrapPageCacheTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Page cache test',
'description' => 'Enable the page cache and test it with conditional HTTP requests.',
'description' => 'Enable the page cache and test it with various HTTP requests.',
'group' => 'Bootstrap'
);
}
......@@ -177,6 +177,37 @@ class BootstrapPageCacheTestCase extends DrupalWebTestCase {
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
}
/**
* Test page compression.
*
* The test should pass even if zlib.output_compression is enabled in php.ini,
* .htaccess or similar, or if compression is done outside PHP, e.g. by the
* mod_deflate Apache module.
*/
function testPageCompression() {
variable_set('cache', CACHE_NORMAL);
// Fill the cache and verify that output is compressed.
$this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Page was not cached.'));
$this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
$this->assertRaw('</html>', t('Page was gzip compressed.'));
// Verify that cached output is compressed.
$this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
$this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', t('A Content-Encoding header was sent.'));
$this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
$this->assertRaw('</html>', t('Page was gzip compressed.'));
// Verify that a client without compression support gets an uncompressed page.
$this->drupalGet('');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
$this->assertFalse($this->drupalGetHeader('Content-Encoding'), t('A Content-Encoding header was not sent.'));
$this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), t('Site title matches.'));
$this->assertRaw('</html>', t('Page was not compressed.'));
}
}
class BootstrapVariableTestCase extends DrupalWebTestCase {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment