diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index e7108e7..aa1cfa4 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1230,14 +1230,14 @@ function drupal_page_header() { * and the conditions match those currently in the cache, a 304 Not Modified * response is sent. */ -function drupal_serve_page_from_cache(stdClass $cache, Response $response) { +function drupal_serve_page_from_cache(stdClass $cache, Response $response, Request $request) { $config = config('system.performance'); // First half: we must determine if we should be returning a 304. // Negotiate whether to use compression. $page_compression = $config->get('response.gzip') && extension_loaded('zlib'); - $return_compressed = $page_compression && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE; + $return_compressed = $page_compression && $request->server->has('HTTP_ACCEPT_ENCODING') && strpos($request->server->get('HTTP_ACCEPT_ENCODING'), 'gzip') !== FALSE; // Get headers. Keys are lower-case. $boot_headers = drupal_get_http_header(); @@ -1256,8 +1256,8 @@ function drupal_serve_page_from_cache(stdClass $cache, Response $response) { $response->setEtag($cache->created); // See if the client has provided the required HTTP headers. - $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE; - $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE; + $if_modified_since = $request->server->has('HTTP_IF_MODIFIED_SINCE') ? strtotime($request->server->get('HTTP_IF_MODIFIED_SINCE')) : FALSE; + $if_none_match = $request->server->has('HTTP_IF_NONE_MATCH') ? stripslashes($request->server->get('HTTP_IF_NONE_MATCH')) : FALSE; if ($if_modified_since && $if_none_match && $if_none_match == $response->headers->get('etag') // etag must match @@ -1272,7 +1272,7 @@ function drupal_serve_page_from_cache(stdClass $cache, Response $response) { // to that one particular client due to Vary: Cookie. Thus, do not set // max-age > 0, allowing the page to be cached by external proxies, when a // session cookie is present unless the Vary header has been replaced. - $max_age = !isset($_COOKIE[session_name()]) || isset($boot_headers['vary']) ? $config->get('cache.page.max_age') : 0; + $max_age = !$request->cookies->has(session_name()) || isset($boot_headers['vary']) ? $config->get('cache.page.max_age') : 0; $response->headers->set('Cache-Control', 'public, max-age=' . $max_age); // Send the remaining headers. @@ -2074,9 +2074,12 @@ function _drupal_bootstrap_page_cache() { $config = config('system.performance'); $cache_enabled = $config->get('cache.page.use_internal'); } + + // @todo this is *criminal*. but, necessary, until we fix bootstrap ordering. + $request = Request::createFromGlobals(); // 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) { + if (!$request->cookies->has(session_name()) && $cache_enabled) { // Make sure there is a user object because its timestamp will be checked. $user = drupal_anonymous_user(); // Get the page from the cache. @@ -2090,10 +2093,9 @@ function _drupal_bootstrap_page_cache() { drupal_set_title($cache->data['title'], PASS_THROUGH); date_default_timezone_set(drupal_get_user_timezone()); - drupal_serve_page_from_cache($cache, $response); + drupal_serve_page_from_cache($cache, $response, $request); // We are done. - $request = drupal_container()->get('request'); $response->prepare($request); $response->send(); exit; diff --git a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php index 1454707..6126cd0 100644 --- a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php @@ -105,7 +105,7 @@ public function onRespond(FilterResponseEvent $event) { $max_age = config('system.performance')->get('cache.page.max_age'); if ($max_age > 0 && ($cache = drupal_page_set_cache($response, $request))) { - drupal_serve_page_from_cache($cache, $response); + drupal_serve_page_from_cache($cache, $response, $request); } else { $response->setExpires(\DateTime::createFromFormat('j-M-Y H:i:s T', '19-Nov-1978 05:00:00 GMT')); diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php index 62244e5..ba6cb4e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php @@ -98,7 +98,7 @@ function testPageCache() { // Fill the cache. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar'))); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); - $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary header was sent.'); + $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary header was sent.'); // Symfony's Response logic determines a specific order for the subvalues // of the Cache-Control header, even if they are explicitly passed in to // the response header bag in a different order. @@ -109,7 +109,7 @@ function testPageCache() { // Check cache. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar'))); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); - $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary: Cookie header was sent.'); + $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary: Cookie header was sent.'); $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.'); $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); @@ -118,14 +118,14 @@ function testPageCache() { $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT'))); $this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.'); $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent'))); - $this->assertEqual($this->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', 'Default header was replaced.'); + $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'user-agent,accept-encoding', 'Default header was replaced.'); // Check that authenticated users bypass the cache. $user = $this->drupalCreateUser(); $this->drupalLogin($user); $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar'))); $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.'); - $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.'); + $this->assertTrue(strpos(strtolower($this->drupalGetHeader('Vary')), 'cookie') === FALSE, 'Vary: Cookie header was not sent.'); $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'must-revalidate, no-cache, post-check=0, pre-check=0, private', 'Cache-Control header was sent.'); $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index ea98db0..e1d55f3 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -131,14 +131,14 @@ function _toolbar_initialize_page_cache() { $cache = drupal_page_get_cache(); if (is_object($cache)) { $response = new Response(); + $request = \Drupal::request(); $response->headers->set('X-Drupal-Cache', 'HIT'); // Restore the metadata cached with the page. $_GET['q'] = $cache->data['path']; date_default_timezone_set(drupal_get_user_timezone()); - drupal_serve_page_from_cache($cache, $response); + drupal_serve_page_from_cache($cache, $response, $request); - $request = drupal_container()->get('request'); $response->prepare($request); $response->send(); // We are done.