Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.206.2.7
diff -u -p -r1.206.2.7 bootstrap.inc
--- includes/bootstrap.inc	8 Dec 2008 11:49:48 -0000	1.206.2.7
+++ includes/bootstrap.inc	23 Dec 2008 06:24:08 -0000
@@ -528,9 +528,14 @@ function variable_del($name) {
  * Note: we do not serve cached pages when status messages are waiting (from
  * a redirected form submission which was completed).
  *
+ * @see page_cache_allowed()
+ *
  * @param $status_only
  *   When set to TRUE, retrieve the status of the page cache only
  *   (whether it was started in this request or not).
+ * @return
+ *   A string of the saved page from the cache, or NULL if the page has not
+ *   been cached or is not allowed to be cached.
  */
 function page_get_cache($status_only = FALSE) {
   static $status = FALSE;
@@ -541,12 +546,13 @@ function page_get_cache($status_only = F
   }
   $cache = NULL;
 
-  if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_set_message()) == 0) {
+  if (page_cache_allowed()) {
     $cache = cache_get($base_root . request_uri(), 'cache_page');
 
     if (empty($cache)) {
       ob_start();
       $status = TRUE;
+      page_cache_allowed(TRUE);
     }
   }
 
@@ -554,6 +560,36 @@ function page_get_cache($status_only = F
 }
 
 /**
+ * Retrieves and optionally sets the current page caching status.
+ *
+ * This function is useful if page caching has been started but needs to be
+ * disabled for the current page if something unique is being displayed on the
+ * page, like a message with drupal_set_message().
+ *
+ * @param $cache_status
+ *   (optional) A boolean that sets the current page caching status.
+ * @return
+ *   TRUE if the current page should be cached, FALSE if otherwise.
+ */
+function page_cache_allowed($cache_status = NULL) {
+  global $user;
+  static $status = NULL;
+
+  if (!isset($status)) {
+    // Do not allow caching when status messages are waiting (from a redirected
+    // form submission which was completed).
+    $status = count(drupal_set_message()) == 0;
+  }
+
+  if (isset($cache_status)) {
+    $status = $cache_status;
+  }
+
+  // Always check the request method and that the user is logged out.
+  return $status && !$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
+}
+
+/**
  * Call all init or exit hooks without including all modules.
  *
  * @param $hook
@@ -848,6 +884,8 @@ function drupal_set_message($message = N
 
     if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
       $_SESSION['messages'][$type][] = $message;
+      // Disable the current request from being cached.
+      page_cache_allowed(FALSE);
     }
   }
 
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.756.2.38
diff -u -p -r1.756.2.38 common.inc
--- includes/common.inc	11 Dec 2008 18:43:24 -0000	1.756.2.38
+++ includes/common.inc	23 Dec 2008 06:24:11 -0000
@@ -2575,31 +2575,28 @@ function _drupal_bootstrap_full() {
  * We thus only deal with the gzip variant and unzip the cache in case
  * the browser does not accept gzip encoding.
  *
- * @see drupal_page_header
+ * @see drupal_page_header()
+ * @see page_cache_allowed()
  */
 function page_set_cache() {
-  global $user, $base_root;
+  global $base_root;
 
-  if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && page_get_cache(TRUE)) {
-    // This will fail in some cases, see page_get_cache() for the explanation.
-    if ($data = ob_get_contents()) {
-      $cache = TRUE;
-      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 = FALSE;
-        }
-        else if (zlib_get_coding_type() == FALSE) {
-          $data = gzencode($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 (page_get_cache(TRUE) && ob_get_level() && ($data = ob_get_contents())) {
+    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') {
+        page_cache_allowed(FALSE);
       }
-      ob_end_flush();
-      if ($cache && $data) {
-        cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers());
+      else if (zlib_get_coding_type() == FALSE) {
+        $data = gzencode($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.
+    }
+    ob_end_flush();
+    if (page_cache_allowed() && $data) {
+      cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers());
     }
   }
 }
