From a0671877111ade392e699588c2efcbeaa139354b Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Thu, 8 Jul 2004 19:22:48 +0000
Subject: [PATCH] - Patch #8509 by Killes and Steven: added support for sending
 compressed   pages.  Useful to reduce your site's bandwidth.

---
 CHANGELOG.txt                |  9 +++++++--
 database/updates.inc         |  7 ++++++-
 includes/bootstrap.inc       | 17 +++++++++++++++++
 modules/system.module        |  2 +-
 modules/system/system.module |  2 +-
 5 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 30bc4331e51a..e180b2fc5d21 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -27,9 +27,13 @@ Drupal x.x.x, xxxx-xx-xx
     * added 'categories' block.
 - blogger API:
     * added support for auto-discovery of blogger API via RSD.
+- performance:
+    * added support for sending gzip compressed pages.
 - accessibility:
     * improved the accessibility of the archive module's calendar.
     * improved form handling and error reporting.
+- documentation:
+    * added PHPDoc/Doxygen comments.
 
 
 Drupal 4.4.2, 2004-07-04
@@ -80,10 +84,11 @@ Drupal 4.4.0, 2004-04-01
     * grouped form elements using '<fieldset>' and '<legend>' tags.
     * added '<label>' tags to form elements.
 - refactored 404 (file not found) handling and added support for custom 404 pages.
-- documentation:
-    * added PHPDoc/Doxygen comments.
 - improved the filter system to prevent conflicts between filters:
     * made it possible to change the order in which filters are applied.
+- documentation:
+    * added PHPDoc/Doxygen comments.
+
 
 Drupal 4.3.2, 2004-01-01
 ------------------------
diff --git a/database/updates.inc b/database/updates.inc
index 1f200ed551f1..172c527f7525 100644
--- a/database/updates.inc
+++ b/database/updates.inc
@@ -62,7 +62,8 @@
   "2004-06-11" => "update_88",
   "2004-06-18" => "update_89",
   "2004-06-27" => "update_90",
-  "2004-06-30" => "update_91"
+  "2004-06-30" => "update_91",
+  "2004-07-07" => "update_92"
 );
 
 function update_32() {
@@ -1142,6 +1143,10 @@ function update_91() {
   return $ret;
 }
 
+function update_92() {
+  $ret[] = update_sql("DELETE FROM {cache}");
+}
+
 function update_sql($sql) {
   $edit = $_POST["edit"];
   $result = db_query($sql);
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 7a4518a51fae..03e7755de19e 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -86,6 +86,14 @@ function page_set_cache() {
   if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET') {
     // This will fail in some cases, see page_get_cache() for the explanation.
     if ($data = ob_get_contents()) {
+      if (function_exists('gzencode')) {
+        if (version_compare(phpversion(), '4.2', '>=')) {
+          $data = gzencode($data, 9, FORCE_GZIP);
+        }
+        else {
+          $data = gzencode($data, FORCE_GZIP);
+        }
+      }
       cache_set(request_uri(), $data, 1, drupal_get_headers());
     }
   }
@@ -145,6 +153,15 @@ function drupal_page_header() {
       header("Last-Modified: $date");
       header("ETag: $etag");
 
+      // Determine if the browser accepts gzipped data
+      if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false && function_exists('gzencode')) {
+        // strip the gzip header and run uncompress
+        $cache->data = gzinflate(substr(substr($cache->data, 10), 0, -8));
+      }
+      elseif (function_exists('gzencode')) {
+        header('Content-Encoding: gzip');
+      }
+
       /*
       ** Send the original request's headers.  We send them one after
       ** another so PHP's header() function can deal with duplicate
diff --git a/modules/system.module b/modules/system.module
index 4fcc4045524c..41dcdcc268b5 100644
--- a/modules/system.module
+++ b/modules/system.module
@@ -26,7 +26,7 @@ function system_help($section) {
       <pre>     00 * * * * /home/www/drupal/scripts/cron-lynx.sh</pre>
       Note that it is essential to access <code>cron.php</code> using a browser on the web site's domain; do not run it using command line PHP and avoid using <code>localhost</code> or <code>127.0.0.1</code> or some of the environment variables will not be set correctly and features may not work as expected.</p>
       <h3><a id=\"cache\">Cache</a></h3>
-      <p>Drupal has a caching mechanism which stores dynamically generated web pages in a database.  By caching a web page, Drupal does not have to create the page each time someone wants to view it, instead it takes only one SQL query to display it, reducing response time and the server's load.  Only pages requested by \"anonymous\" users are cached.</p>", array('%base_url' => $base_url, '%cron-link' => "<a href=\"$base_url/cron.php\">$base_url/cron.php</a>", '%lynx' => 'http://lynx.browser.org', '%wget' => 'http://www.gnu.org/software/wget/wget.html' ));
+      <p>Drupal has a caching mechanism which stores dynamically generated web pages in a database.  By caching a web page, Drupal does not have to create the page each time someone wants to view it, instead it takes only one SQL query to display it, reducing response time and the server's load.  Only pages requested by \"anonymous\" users are cached.  In order to reduce server load and save bandwidth, Drupal stores and sends cached pages compressed.</p>", array('%base_url' => $base_url, '%cron-link' => "<a href=\"$base_url/cron.php\">$base_url/cron.php</a>", '%lynx' => 'http://lynx.browser.org', '%wget' => 'http://www.gnu.org/software/wget/wget.html' ));
     case 'admin/modules#description':
       return t('Configuration system that lets administrators modify the workings of the site.');
   }
diff --git a/modules/system/system.module b/modules/system/system.module
index 4fcc4045524c..41dcdcc268b5 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -26,7 +26,7 @@ function system_help($section) {
       <pre>     00 * * * * /home/www/drupal/scripts/cron-lynx.sh</pre>
       Note that it is essential to access <code>cron.php</code> using a browser on the web site's domain; do not run it using command line PHP and avoid using <code>localhost</code> or <code>127.0.0.1</code> or some of the environment variables will not be set correctly and features may not work as expected.</p>
       <h3><a id=\"cache\">Cache</a></h3>
-      <p>Drupal has a caching mechanism which stores dynamically generated web pages in a database.  By caching a web page, Drupal does not have to create the page each time someone wants to view it, instead it takes only one SQL query to display it, reducing response time and the server's load.  Only pages requested by \"anonymous\" users are cached.</p>", array('%base_url' => $base_url, '%cron-link' => "<a href=\"$base_url/cron.php\">$base_url/cron.php</a>", '%lynx' => 'http://lynx.browser.org', '%wget' => 'http://www.gnu.org/software/wget/wget.html' ));
+      <p>Drupal has a caching mechanism which stores dynamically generated web pages in a database.  By caching a web page, Drupal does not have to create the page each time someone wants to view it, instead it takes only one SQL query to display it, reducing response time and the server's load.  Only pages requested by \"anonymous\" users are cached.  In order to reduce server load and save bandwidth, Drupal stores and sends cached pages compressed.</p>", array('%base_url' => $base_url, '%cron-link' => "<a href=\"$base_url/cron.php\">$base_url/cron.php</a>", '%lynx' => 'http://lynx.browser.org', '%wget' => 'http://www.gnu.org/software/wget/wget.html' ));
     case 'admin/modules#description':
       return t('Configuration system that lets administrators modify the workings of the site.');
   }
-- 
GitLab