Commit 5a01a26f authored by catch's avatar catch

Issue #2257709 by znerol, Wim Leers: Remove the interdependence between the...

Issue #2257709 by znerol, Wim Leers: Remove the interdependence between the internal page cache and management of the Cache-Control header for external caches.
parent 4b68f9af
......@@ -581,7 +581,7 @@ services:
class: Drupal\Core\EventSubscriber\FinishResponseSubscriber
tags:
- { name: event_subscriber }
arguments: ['@language_manager']
arguments: ['@language_manager', '@config.factory']
scope: request
redirect_response_subscriber:
class: Drupal\Core\EventSubscriber\RedirectResponseSubscriber
......
This diff is collapsed.
......@@ -2750,55 +2750,37 @@ function _drupal_bootstrap_full($skip = FALSE) {
* Page compression requires the PHP zlib extension
* (http://php.net/manual/ref.zlib.php).
*
* @param $body
* The response body.
* @return
* The cached object or NULL if the page cache was not set.
* @param \Symfony\Component\HttpFoundation\Response $response
* The fully populated response.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request for this page.
*
* @see drupal_page_header()
*/
function drupal_page_set_cache(Response $response, Request $request) {
if (drupal_page_is_cacheable()) {
// Check if the current page may be compressed.
$page_compressed = \Drupal::config('system.performance')->get('response.gzip') && extension_loaded('zlib');
$cache = (object) array(
'cid' => drupal_page_cache_get_cid($request),
'data' => array(
'body' => $response->getContent(),
'headers' => array(),
// We need to store whether page was compressed or not,
// because by the time it is read, the configuration might change.
'page_compressed' => $page_compressed,
),
'tags' => HtmlViewSubscriber::convertHeaderToCacheTags($response->headers->get('X-Drupal-Cache-Tags')),
'expire' => Cache::PERMANENT,
'created' => REQUEST_TIME,
);
$cache->data['headers'] = $response->headers->all();
// Hack: exclude the x-drupal-cache header; it may make it in here because
// of awkwardness in how we defer sending it over in _drupal_page_get_cache.
if (isset($cache->data['headers']['x-drupal-cache'])) {
unset($cache->data['headers']['x-drupal-cache']);
}
// Use the actual timestamp from an Expires header, if available.
if ($date = $response->getExpires()) {
$date = DrupalDateTime::createFromDateTime($date);
$cache->expire = $date->getTimestamp();
// Check if the current page may be compressed.
if (\Drupal::config('system.performance')->get('response.gzip') &&
!$response->headers->get('Content-Encoding') && extension_loaded('zlib')) {
$content = $response->getContent();
if ($content) {
$response->setContent(gzencode($content, 9, FORCE_GZIP));
$response->headers->set('Content-Encoding', 'gzip');
}
if ($cache->data['body']) {
if ($page_compressed) {
$cache->data['body'] = gzencode($cache->data['body'], 9, FORCE_GZIP);
}
\Drupal::cache('render')->set($cache->cid, $cache->data, $cache->expire, $cache->tags);
}
return $cache;
// When page compression is enabled, ensure that proxy caches will record
// and deliver different versions of a page depending on whether the
// client supports gzip or not.
$response->setVary('Accept-Encoding', FALSE);
}
// Use the actual timestamp from an Expires header, if available.
$date = $response->getExpires();
$expire = ($date > (new DateTime())) ? $date->getTimestamp() : Cache::PERMANENT;
$cid = drupal_page_cache_get_cid($request);
$tags = HtmlViewSubscriber::convertHeaderToCacheTags($response->headers->get('X-Drupal-Cache-Tags'));
\Drupal::cache('render')->set($cid, $response, $expire, $tags);
}
/**
......
......@@ -196,15 +196,31 @@ function testPageCache() {
$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.');
}
/**
* Tests the omit_vary_cookie setting.
*/
public function testPageCacheWithoutVaryCookie() {
$config = \Drupal::config('system.performance');
$config->set('cache.page.use_internal', 1);
$config->set('cache.page.max_age', 300);
$config->save();
// Check the omit_vary_cookie setting.
$this->drupalLogout();
$settings['settings']['omit_vary_cookie'] = (object) array(
'value' => TRUE,
'required' => TRUE,
);
$this->writeSettings($settings);
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
// Fill the cache.
$this->drupalGet('');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
$this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
// Check cache.
$this->drupalGet('');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
}
......
......@@ -121,13 +121,11 @@ function _toolbar_initialize_page_cache() {
// If we have a cache, serve it.
// @see _drupal_bootstrap_page_cache()
$request = \Drupal::request();
$cache = drupal_page_get_cache($request);
if (is_object($cache)) {
$response = new Response();
$response = drupal_page_get_cache($request);
if ($response) {
$response->headers->set('X-Drupal-Cache', 'HIT');
date_default_timezone_set(drupal_get_user_timezone());
drupal_serve_page_from_cache($cache, $response, $request);
drupal_serve_page_from_cache($response, $request);
$response->prepare($request);
$response->send();
......@@ -135,9 +133,6 @@ function _toolbar_initialize_page_cache() {
exit;
}
// Otherwise, create a new page response (that will be cached).
drupal_add_http_header('X-Drupal-Cache', 'MISS');
// The Expires HTTP header is the heart of the client-side HTTP caching. The
// additional server-side page cache only takes effect when the client
// accesses the callback URL again (e.g., after clearing the browser cache or
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment