You are here

function esi_block_set_http_headers in ESI: Edge Side Includes 7.3

Set HTTP headers to control caching of ESI fragments.

1 call to esi_block_set_http_headers()
esi_block__esi_block_render in modules/esi_block/esi_block.esi.inc
Render the HTML for a single block. Defined in hook_esi_component_info().

File

modules/esi_block/esi_block.esi.inc, line 126
ESI handlers for ESI Block.

Code

function esi_block_set_http_headers($block) {

  // Nginx follows rfc2616 section 14.9.1 correctly and will not cache if
  // header is set to private.
  // The example Varnish VCL does not follow the RFC, and for ajax private
  // should be used.
  // $user_cache_control_header = variable_get('esi_mode', ESI_MODE) == ESI__CONFIG_SSI ? 'public' : 'private';
  $user_cache_control_header = 'private';
  $ttl = $block->esi_ttl;
  $headers = array();
  if ($block->cache == DRUPAL_NO_CACHE) {
    $headers[] = array(
      'Cache-Control',
      'must-revalidate, max-age=0',
    );
  }
  elseif ($block->cache & DRUPAL_CACHE_PER_ROLE || $block->cache & DRUPAL_CACHE_PER_USER) {
    $headers[] = array(
      'Cache-Control',
      "{$user_cache_control_header}, max-age={$ttl}",
    );
    $headers[] = array(
      'X-BLOCK-CACHE',
      $block->cache & DRUPAL_CACHE_PER_USER ? 'USER' : 'ROLE',
    );
  }
  else {
    $headers[] = array(
      'Cache-Control',
      "public, max-age={$ttl}",
    );
  }

  // Allow other modules to alter the headers.
  // @see hook_esi_block_cache_headers_alter().
  drupal_alter('esi_block_cache_headers', $headers, $block);
  foreach ($headers as $header) {
    drupal_add_http_header($header[0], $header[1]);
  }
}