public function serveCachedPage() { $cache_enabled = $this->getCacheMode(); // If there is no session cookie and cache is enabled (or forced), try // to serve a cached page. if (!isset($_COOKIE[session_name()]) && $cache_enabled) { global $user; // Make sure there is a user object because its timestamp will be // checked, hook_boot might check for anonymous user etc. $user = drupal_anonymous_user(); // Get the page from the cache. $cache = drupal_page_get_cache(); // If there is a cached page, display it. if (is_object($cache)) { header('X-Drupal-Cache: HIT'); // Restore the metadata cached with the page. $_GET['q'] = $cache->data['path']; drupal_set_title($cache->data['title'], PASS_THROUGH); date_default_timezone_set(drupal_get_user_timezone()); // If the skipping of the bootstrap hooks is not enforced, call // hook_boot. if (variable_get('page_cache_invoke_hooks', TRUE)) { bootstrap_invoke_all('boot'); } drupal_serve_page_from_cache($cache); // If the skipping of the bootstrap hooks is not enforced, call // hook_exit. if (variable_get('page_cache_invoke_hooks', TRUE)) { bootstrap_invoke_all('exit'); } // We are done. exit; } else { header('X-Drupal-Cache: MISS'); } } }
/** * Sets extra headers on successful responses. * * @param Symfony\Component\HttpKernel\Event\FilterResponseEvent $event * The event to process. */ public function onRespond(FilterResponseEvent $event) { if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { return; } $request = $event->getRequest(); $response = $event->getResponse(); // Set the X-UA-Compatible HTTP header to force IE to use the most recent // rendering engine or use Chrome's frame rendering engine if available. $response->headers->set('X-UA-Compatible', 'IE=edge,chrome=1', FALSE); // Set the Content-language header. $response->headers->set('Content-language', $this->languageManager->getCurrentLanguage()->id); // Attach globally-declared headers to the response object so that Symfony // can send them for us correctly. // @todo remove this once we have removed all drupal_add_http_header() // calls. $headers = drupal_get_http_header(); foreach ($headers as $name => $value) { $response->headers->set($name, $value, FALSE); } $is_cacheable = drupal_page_is_cacheable(); // Add headers necessary to specify whether the response should be cached by // proxies and/or the browser. if ($is_cacheable && $this->config->get('cache.page.max_age') > 0) { if (!$this->isCacheControlCustomized($response)) { $this->setResponseCacheable($response, $request); } } else { $this->setResponseNotCacheable($response, $request); } // Currently it is not possible to cache some types of responses. Therefore // exclude binary file responses (generated files, e.g. images with image // styles) and streamed responses (files directly read from the disk). // see: https://github.com/symfony/symfony/issues/9128#issuecomment-25088678 if ($is_cacheable && $this->config->get('cache.page.use_internal') && !$response instanceof BinaryFileResponse && !$response instanceof StreamedResponse) { // Store the response in the internal page cache. drupal_page_set_cache($response, $request); $response->headers->set('X-Drupal-Cache', 'MISS'); drupal_serve_page_from_cache($response, $request); } }
/** * Sets extra headers on successful responses. * * @param Symfony\Component\HttpKernel\Event\FilterResponseEvent $event * The event to process. */ public function onRespond(FilterResponseEvent $event) { if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { return; } $request = $event->getRequest(); $response = $event->getResponse(); // Set the X-UA-Compatible HTTP header to force IE to use the most recent // rendering engine or use Chrome's frame rendering engine if available. $response->headers->set('X-UA-Compatible', 'IE=edge,chrome=1', FALSE); // Set the Content-language header. $response->headers->set('Content-language', $this->languageManager->getCurrentLanguage()->getId()); // Attach globally-declared headers to the response object so that Symfony // can send them for us correctly. // @todo Remove this once drupal_process_attached() no longer calls // _drupal_add_http_header(), which has its own static. Instead, // _drupal_process_attached() should use // \Symfony\Component\HttpFoundation\Response->headers->set(), which is // already documented on the (deprecated) _drupal_process_attached() to // become the final, intended mechanism. $headers = drupal_get_http_header(); foreach ($headers as $name => $value) { // Symfony special-cases the 'Status' header. if ($name === 'status') { $response->setStatusCode($value); } $response->headers->set($name, $value, FALSE); } $is_cacheable = $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW && $this->responsePolicy->check($response, $request) !== ResponsePolicyInterface::DENY; // Add headers necessary to specify whether the response should be cached by // proxies and/or the browser. if ($is_cacheable && $this->config->get('cache.page.max_age') > 0) { if (!$this->isCacheControlCustomized($response)) { // Only add the default Cache-Control header if the controller did not // specify one on the response. $this->setResponseCacheable($response, $request); } } else { // If either the policy forbids caching or the sites configuration does // not allow to add a max-age directive, then enforce a Cache-Control // header declaring the response as not cacheable. $this->setResponseNotCacheable($response, $request); } // Currently it is not possible to cache some types of responses. Therefore // exclude binary file responses (generated files, e.g. images with image // styles) and streamed responses (files directly read from the disk). // see: https://github.com/symfony/symfony/issues/9128#issuecomment-25088678 if ($is_cacheable && $this->config->get('cache.page.use_internal') && !$response instanceof BinaryFileResponse && !$response instanceof StreamedResponse) { // Store the response in the internal page cache. drupal_page_set_cache($response, $request); $response->headers->set('X-Drupal-Cache', 'MISS'); drupal_serve_page_from_cache($response, $request); } }
/** * {@inheritdoc} * * @todo Invoke proper request/response/terminate events. */ public function handlePageCache(Request $request) { $this->boot(); $this->initializeCookieGlobals($request); // Check for a cache mode force from settings.php. if (Settings::get('page_cache_without_database')) { $cache_enabled = TRUE; } else { $config = $this->getContainer()->get('config.factory')->get('system.performance'); $cache_enabled = $config->get('cache.page.use_internal'); } $request_policy = \Drupal::service('page_cache_request_policy'); if ($cache_enabled && $request_policy->check($request) === RequestPolicyInterface::ALLOW) { // Get the page from the cache. $response = drupal_page_get_cache($request); // If there is a cached page, display it. if ($response) { $response->headers->set('X-Drupal-Cache', 'HIT'); drupal_serve_page_from_cache($response, $request); // We are done. $response->prepare($request); $response->send(); exit; } } return $this; }
/** * {@inheritdoc} * * @todo Invoke proper request/response/terminate events. */ public function handlePageCache(Request $request) { $this->boot(); $this->initializeCookieGlobals($request); // Check for a cache mode force from settings.php. if (Settings::get('page_cache_without_database')) { $cache_enabled = TRUE; } else { $config = $this->getContainer()->get('config.factory')->get('system.performance'); $cache_enabled = $config->get('cache.page.use_internal'); } // If there is no session cookie and cache is enabled (or forced), try to // serve a cached page. if (!$request->cookies->has(session_name()) && $cache_enabled && drupal_page_is_cacheable()) { // Get the page from the cache. $response = drupal_page_get_cache($request); // If there is a cached page, display it. if ($response) { $response->headers->set('X-Drupal-Cache', 'HIT'); drupal_serve_page_from_cache($response, $request); // We are done. $response->prepare($request); $response->send(); exit; } } return $this; }