From 23c32f094c66fe554bc6938e15f812c5a59a793b Mon Sep 17 00:00:00 2001
From: Dave Hall <dave.hall@skwashd.com>
Date: Wed, 26 Oct 2011 19:17:39 +1100
Subject: [PATCH] Issue #1321918 by skwashd: Remove page_compression variable
 and associated behaviour

---
 CHANGELOG.txt                           |    2 ++
 includes/bootstrap.inc                  |   21 +--------------------
 includes/common.inc                     |   29 -----------------------------
 modules/simpletest/tests/bootstrap.test |   31 -------------------------------
 modules/system/system.admin.inc         |   10 ----------
 modules/system/system.install           |    2 +-
 modules/system/system.module            |    2 +-
 sites/default/default.settings.php      |   15 ---------------
 8 files changed, 5 insertions(+), 107 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 431574b..deb973b 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -4,6 +4,8 @@ Drupal 8.0, xxxx-xx-xx (development version)
 - Included the following Symfony2 components:
     * ClassLoader - PSR-0-compatible autoload routines.
     * HttpFoundation - Abstraction objects for HTTP requests and responses.
+- Performance:
+    * Removed support for sending gzip compressed pages.
 - Removed modules from core
     * The following modules have been removed from core, because contributed
       modules with similar functionality are available:
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index ebeb4ab..a3789d9 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -1297,9 +1297,6 @@ function drupal_page_header() {
  * response is sent.
  */
 function drupal_serve_page_from_cache(stdClass $cache) {
-  // Negotiate whether to use compression.
-  $page_compression = variable_get('page_compression', TRUE) && extension_loaded('zlib');
-  $return_compressed = $page_compression && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE;
 
   // Get headers set in hook_boot(). Keys are lower-case.
   $hook_boot_headers = drupal_get_http_header();
@@ -1328,7 +1325,7 @@ function drupal_serve_page_from_cache(stdClass $cache) {
   $default_headers['Cache-Control'] = 'public, max-age=' . $max_age;
 
   // Entity tag should change if the output changes.
-  $etag = '"' . $cache->created . '-' . intval($return_compressed) . '"';
+  $etag = $cache->created;
   header('Etag: ' . $etag);
 
   // See if the client has provided the required HTTP headers.
@@ -1368,22 +1365,6 @@ function drupal_serve_page_from_cache(stdClass $cache) {
     header('Vary: Cookie');
   }
 
-  if ($page_compression) {
-    header('Vary: Accept-Encoding', FALSE);
-    // If page_compression is enabled, the cache contains gzipped data.
-    if ($return_compressed) {
-      // $cache->data['body'] 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 {
-      // The client does not support compression, so unzip the data in the
-      // cache. Strip the gzip header and run uncompress.
-      $cache->data['body'] = gzinflate(substr(substr($cache->data['body'], 10), 0, -8));
-    }
-  }
-
   // Print the page.
   print $cache->data['body'];
 }
diff --git a/includes/common.inc b/includes/common.inc
index 5f7cdb8..c10790b 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -3439,15 +3439,6 @@ function drupal_build_css_cache($css) {
     if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
       return FALSE;
     }
-    // If CSS gzip compression is enabled, clean URLs are enabled (which means
-    // that rewrite rules are working) and the zlib extension is available then
-    // create a gzipped version of this file. This file is served conditionally
-    // to browsers that accept gzip using .htaccess rules.
-    if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
-      if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
-        return FALSE;
-      }
-    }
     // Save the updated map.
     $map[$key] = $uri;
     variable_set('drupal_css_cache_files', $map);
@@ -4774,15 +4765,6 @@ function drupal_build_js_cache($files) {
     if (!file_exists($uri) && !file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) {
       return FALSE;
     }
-    // If JS gzip compression is enabled, clean URLs are enabled (which means
-    // that rewrite rules are working) and the zlib extension is available then
-    // create a gzipped version of this file. This file is served conditionally
-    // to browsers that accept gzip using .htaccess rules.
-    if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
-      if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
-        return FALSE;
-      }
-    }
     $map[$key] = $uri;
     variable_set('drupal_js_cache_files', $map);
   }
@@ -4955,14 +4937,6 @@ function _drupal_bootstrap_full() {
 /**
  * Store the current page in the cache.
  *
- * 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()
  */
 function drupal_page_set_cache() {
@@ -4993,9 +4967,6 @@ function drupal_page_set_cache() {
     }
 
     if ($cache->data['body']) {
-      if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
-        $cache->data['body'] = gzencode($cache->data['body'], 9, FORCE_GZIP);
-      }
       cache('page')->set($cache->cid, $cache->data, $cache->expire);
     }
     return $cache;
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index fcf52b9..a49a4ec 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -189,37 +189,6 @@ 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', 1);
-
-    // 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 {
diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc
index b08f418..06d1623 100644
--- a/modules/system/system.admin.inc
+++ b/modules/system/system.admin.inc
@@ -1689,13 +1689,6 @@ function system_performance_settings() {
   );
 
   $js_hide = $cache ? '' : ' class="js-hide"';
-  $form['bandwidth_optimization']['page_compression'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Compress cached pages.'),
-    '#default_value' => variable_get('page_compression', TRUE),
-    '#prefix' => '<div id="page-compression-wrapper"' . $js_hide . '>',
-    '#suffix' => '</div>',
-  );
   $form['bandwidth_optimization']['preprocess_css'] = array(
     '#type' => 'checkbox',
     '#title' => t('Aggregate and compress CSS files.'),
@@ -1711,9 +1704,6 @@ function system_performance_settings() {
 
   $form['#submit'][] = 'drupal_clear_css_cache';
   $form['#submit'][] = 'drupal_clear_js_cache';
-  // This form allows page compression settings to be changed, which can
-  // invalidate the page cache, so it needs to be cleared on form submit.
-  $form['#submit'][] = 'system_clear_page_cache_submit';
 
   return system_settings_form($form);
 }
diff --git a/modules/system/system.install b/modules/system/system.install
index 24933e2..9d38729 100644
--- a/modules/system/system.install
+++ b/modules/system/system.install
@@ -684,7 +684,7 @@ function system_schema() {
   $schema['cache_form'] = $schema['cache'];
   $schema['cache_form']['description'] = 'Cache table for the form system to store recently built forms and their storage data, to be used in subsequent page requests.';
   $schema['cache_page'] = $schema['cache'];
-  $schema['cache_page']['description'] = 'Cache table used to store compressed pages for anonymous users, if page caching is enabled.';
+  $schema['cache_page']['description'] = 'Cache table used to store generated pages for anonymous users, if page caching is enabled.';
   $schema['cache_menu'] = $schema['cache'];
   $schema['cache_menu']['description'] = 'Cache table for the menu system to store router information as well as generated link trees for various menu/page/user combinations.';
   $schema['cache_path'] = $schema['cache'];
diff --git a/modules/system/system.module b/modules/system/system.module
index 0ef688e..f768b5e 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -77,7 +77,7 @@ function system_help($path, $arg) {
       $output .= '<dt>' . t('Managing themes') . '</dt>';
       $output .= '<dd>' . t('The System module allows users with the appropriate permissions to enable and disable themes on the <a href="@themes">Appearance administration page</a>. Themes determine the design and presentation of your site. Drupal comes packaged with several core themes, and additional contributed themes are available at the <a href="@drupal-themes">Drupal.org theme page</a>.', array('@themes' => url('admin/appearance'), '@drupal-themes' => 'http://drupal.org/project/themes')) . '</dd>';
       $output .= '<dt>' . t('Managing caching') . '</dt>';
-      $output .= '<dd>' . t("The System module allows users with the appropriate permissions to manage caching on the <a href='@cache-settings'>Performance settings page</a>. Drupal has a robust caching system that allows the efficient re-use of previously-constructed web pages and web page components. Pages requested by anonymous users are stored in a compressed format; depending on your site configuration and the amount of your web traffic tied to anonymous visitors, the caching system may significantly increase the speed of your site.", array('@cache-settings' => url('admin/config/development/performance'))) . '</dd>';
+      $output .= '<dd>' . t("The System module allows users with the appropriate permissions to manage caching on the <a href='@cache-settings'>Performance settings page</a>. Drupal has a robust caching system that allows the efficient re-use of previously-constructed web pages and web page components. Depending on your site configuration and the amount of your web traffic tied to anonymous visitors, the caching system may significantly increase the speed of your site.", array('@cache-settings' => url('admin/config/development/performance'))) . '</dd>';
       $output .= '<dt>' . t('Performing system maintenance') . '</dt>';
       $output .= '<dd>' . t('In order for the site and its modules to continue to operate well, a set of routine administrative operations must run on a regular basis. The System module manages this task by making use of a system cron job. You can verify the status of cron tasks by visiting the <a href="@status">Status report page</a>. For more information, see the online handbook entry for <a href="@handbook">configuring cron jobs</a>. You can set up cron job by visiting <a href="@cron">Cron configuration</a> page', array('@status' => url('admin/reports/status'), '@handbook' => 'http://drupal.org/cron', '@cron' => url('admin/config/system/cron'))) . '</dd>';
       $output .= '<dt>' . t('Configuring basic site settings') . '</dt>';
diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php
index 4bf525c..7f59741 100644
--- a/sites/default/default.settings.php
+++ b/sites/default/default.settings.php
@@ -385,21 +385,6 @@ ini_set('session.cookie_lifetime', 2000000);
 # $conf['omit_vary_cookie'] = TRUE;
 
 /**
- * CSS/JS aggregated file gzip compression:
- *
- * By default, when CSS or JS aggregation and clean URLs are enabled Drupal will
- * store a gzip compressed (.gz) copy of the aggregated files. If this file is
- * available then rewrite rules in the default .htaccess file will serve these
- * files to browsers that accept gzip encoded content. This allows pages to load
- * faster for these users and has minimal impact on server load. If you are
- * using a webserver other than Apache httpd, or a caching reverse proxy that is
- * configured to cache and compress these files itself you may want to uncomment
- * one or both of the below lines, which will prevent gzip files being stored.
- */
-# $conf['css_gzip_compression'] = FALSE;
-# $conf['js_gzip_compression'] = FALSE;
-
-/**
  * String overrides:
  *
  * To override specific strings on your site with or without enabling locale
-- 
1.7.5.4

