You are here

function esi_get_page_cache in ESI: Edge Side Includes 6.2

1 call to esi_get_page_cache()
esi_boot in ./esi.module

File

./esi.module, line 803
Adds support for ESI (Edge-Side-Include) integration, allowing blocks to be\ delivered by ESI, with support for per-block cache times.

Code

function esi_get_page_cache() {
  global $base_root;
  if (!variable_get('esi_page_cache', ESI_PAGE_CACHE)) {
    return NULL;
  }
  if (!variable_get('esi_mode', ESI_MODE)) {
    return NULL;
  }
  $cid = 'esi:' . $base_root . request_uri();

  // Only try the cache for these paths.
  if (strpos($cid, '/esi/block/') === FALSE && strpos($cid, '/esi/panels_pane/') === FALSE) {
    return NULL;
  }

  // See if this is cached.
  $cache = cache_get($cid, 'cache_page');
  if (empty($cache)) {
    header('X-Drupal-ESI-Cache: MISS');
    return NULL;
  }

  // Unserialize headers if they are serialized.
  $headers = @unserialize($cache->headers);
  if ($headers === FALSE && $cache->headers !== 'b:0;') {
    $headers = $cache->headers;
  }
  if (!is_array($headers)) {
    $headers = explode("\n", $headers);
  }

  // Parse headers.
  $max_age = -1;
  foreach ($headers as $key => $header) {

    // Get the max age.
    if (stripos($header, 'Cache-Control: ') === 0 && stripos($header, 'max-age=') !== FALSE || strcasecmp($key, 'Cache-Control') == 0 && stripos($header, 'max-age=') !== FALSE) {
      $max_age = (int) substr($header, strpos($header, 'max-age=') + 8);
    }

    // Set the server protocol.
    if (strpos($header, ':status: ') === 0) {
      $header = str_replace(':status:', $_SERVER['SERVER_PROTOCOL'], $header);
    }
    if (strcasecmp($key, ':status') == 0) {
      $headers[$_SERVER['SERVER_PROTOCOL']] = $header;
      unset($headers[$key]);
    }
  }

  // Check the max age.
  $time = (int) isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
  if (empty($max_age) || $time - (int) $cache->created > $max_age) {
    header('X-Drupal-ESI-Cache: EXPIRED' . $max_age);
    return NULL;
  }

  // Deal with compression.
  $page_compression = variable_get('page_compression', TRUE) && extension_loaded('zlib');
  $return_compressed = variable_get('page_compression', TRUE) && extension_loaded('zlib') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE;
  $headers[] = 'Etag: "' . $cache->created . '-' . intval($return_compressed) . '"';
  if ($page_compression) {
    header('Vary: Accept-Encoding', FALSE);

    // If page_compression is enabled, the cache contains gzipped data.
    if ($return_compressed) {
      ini_set('zlib.output_compression', '0');
      $headers[] = '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 = gzinflate(substr(substr($cache->data, 10), 0, -8));
      $headers[] = 'X-PF-Uncompressing: 1';
    }
  }

  // Send contents if everything works out.
  $headers[] = 'X-Drupal-ESI-Cache: HIT';
  foreach ($headers as $key => $header) {
    if (is_numeric($key)) {
      header($header);
    }
    else {
      header($key . ': ' . $header);
    }
  }
  return $cache->data;
}