summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2009-11-02 03:12:05 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2009-11-02 03:12:05 +0000
commit0d371742b6498de344c35378c19238b13ef13b26 (patch)
tree2d621fdde4cbca2da495362cb3743f35ae567833
parentbfedf56f9d9e41eee879221adeb302fb09c072ac (diff)
downloadbrdo-0d371742b6498de344c35378c19238b13ef13b26.tar.gz
brdo-0d371742b6498de344c35378c19238b13ef13b26.tar.bz2
#43462 by c960657 and andypost: Fixed issues with certain configurations of compressed pages.
-rw-r--r--includes/bootstrap.inc3
-rw-r--r--includes/common.inc29
-rw-r--r--modules/simpletest/drupal_web_test_case.php2
-rw-r--r--modules/simpletest/tests/bootstrap.test33
4 files changed, 45 insertions, 22 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 746e99947..7ab4f2b65 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -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 {
diff --git a/includes/common.inc b/includes/common.inc
index fc1e71d29..ff2c81776 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -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;
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index 985d0c0bd..9b0bf7275 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -1516,7 +1516,7 @@ class DrupalWebTestCase extends DrupalTestCase {
* 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:
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index b6bfd54e8..cac805897 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -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 {