annotate core/modules/page_cache/src/StackMiddleware/PageCache.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\page_cache\StackMiddleware;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\Cache;
Chris@0 6 use Drupal\Core\Cache\CacheableResponseInterface;
Chris@0 7 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 8 use Drupal\Core\PageCache\RequestPolicyInterface;
Chris@0 9 use Drupal\Core\PageCache\ResponsePolicyInterface;
Chris@0 10 use Drupal\Core\Site\Settings;
Chris@0 11 use Symfony\Component\HttpFoundation\BinaryFileResponse;
Chris@0 12 use Symfony\Component\HttpFoundation\Request;
Chris@0 13 use Symfony\Component\HttpFoundation\Response;
Chris@0 14 use Symfony\Component\HttpFoundation\StreamedResponse;
Chris@0 15 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Executes the page caching before the main kernel takes over the request.
Chris@0 19 */
Chris@0 20 class PageCache implements HttpKernelInterface {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The wrapped HTTP kernel.
Chris@0 24 *
Chris@0 25 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
Chris@0 26 */
Chris@0 27 protected $httpKernel;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The cache bin.
Chris@0 31 *
Chris@17 32 * @var \Drupal\Core\Cache\CacheBackendInterface
Chris@0 33 */
Chris@0 34 protected $cache;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * A policy rule determining the cacheability of a request.
Chris@0 38 *
Chris@0 39 * @var \Drupal\Core\PageCache\RequestPolicyInterface
Chris@0 40 */
Chris@0 41 protected $requestPolicy;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * A policy rule determining the cacheability of the response.
Chris@0 45 *
Chris@0 46 * @var \Drupal\Core\PageCache\ResponsePolicyInterface
Chris@0 47 */
Chris@0 48 protected $responsePolicy;
Chris@0 49
Chris@0 50 /**
Chris@18 51 * The cache ID for the (master) request.
Chris@18 52 *
Chris@18 53 * @var string
Chris@18 54 */
Chris@18 55 protected $cid;
Chris@18 56
Chris@18 57 /**
Chris@0 58 * Constructs a PageCache object.
Chris@0 59 *
Chris@0 60 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
Chris@0 61 * The decorated kernel.
Chris@0 62 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
Chris@0 63 * The cache bin.
Chris@0 64 * @param \Drupal\Core\PageCache\RequestPolicyInterface $request_policy
Chris@0 65 * A policy rule determining the cacheability of a request.
Chris@0 66 * @param \Drupal\Core\PageCache\ResponsePolicyInterface $response_policy
Chris@0 67 * A policy rule determining the cacheability of the response.
Chris@0 68 */
Chris@0 69 public function __construct(HttpKernelInterface $http_kernel, CacheBackendInterface $cache, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy) {
Chris@0 70 $this->httpKernel = $http_kernel;
Chris@0 71 $this->cache = $cache;
Chris@0 72 $this->requestPolicy = $request_policy;
Chris@0 73 $this->responsePolicy = $response_policy;
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
Chris@0 80 // Only allow page caching on master request.
Chris@0 81 if ($type === static::MASTER_REQUEST && $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) {
Chris@0 82 $response = $this->lookup($request, $type, $catch);
Chris@0 83 }
Chris@0 84 else {
Chris@0 85 $response = $this->pass($request, $type, $catch);
Chris@0 86 }
Chris@0 87
Chris@0 88 return $response;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Sidesteps the page cache and directly forwards a request to the backend.
Chris@0 93 *
Chris@0 94 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 95 * A request object.
Chris@0 96 * @param int $type
Chris@0 97 * The type of the request (one of HttpKernelInterface::MASTER_REQUEST or
Chris@0 98 * HttpKernelInterface::SUB_REQUEST)
Chris@0 99 * @param bool $catch
Chris@0 100 * Whether to catch exceptions or not
Chris@0 101 *
Chris@0 102 * @returns \Symfony\Component\HttpFoundation\Response $response
Chris@0 103 * A response object.
Chris@0 104 */
Chris@0 105 protected function pass(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
Chris@0 106 return $this->httpKernel->handle($request, $type, $catch);
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Retrieves a response from the cache or fetches it from the backend.
Chris@0 111 *
Chris@0 112 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 113 * A request object.
Chris@0 114 * @param int $type
Chris@0 115 * The type of the request (one of HttpKernelInterface::MASTER_REQUEST or
Chris@0 116 * HttpKernelInterface::SUB_REQUEST)
Chris@0 117 * @param bool $catch
Chris@0 118 * Whether to catch exceptions or not
Chris@0 119 *
Chris@0 120 * @returns \Symfony\Component\HttpFoundation\Response $response
Chris@0 121 * A response object.
Chris@0 122 */
Chris@0 123 protected function lookup(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
Chris@0 124 if ($response = $this->get($request)) {
Chris@0 125 $response->headers->set('X-Drupal-Cache', 'HIT');
Chris@0 126 }
Chris@0 127 else {
Chris@0 128 $response = $this->fetch($request, $type, $catch);
Chris@0 129 }
Chris@0 130
Chris@0 131 // Only allow caching in the browser and prevent that the response is stored
Chris@0 132 // by an external proxy server when the following conditions apply:
Chris@0 133 // 1. There is a session cookie on the request.
Chris@0 134 // 2. The Vary: Cookie header is on the response.
Chris@0 135 // 3. The Cache-Control header does not contain the no-cache directive.
Chris@0 136 if ($request->cookies->has(session_name()) &&
Chris@0 137 in_array('Cookie', $response->getVary()) &&
Chris@0 138 !$response->headers->hasCacheControlDirective('no-cache')) {
Chris@0 139
Chris@0 140 $response->setPrivate();
Chris@0 141 }
Chris@0 142
Chris@0 143 // Perform HTTP revalidation.
Chris@0 144 // @todo Use Response::isNotModified() as
Chris@0 145 // per https://www.drupal.org/node/2259489.
Chris@0 146 $last_modified = $response->getLastModified();
Chris@0 147 if ($last_modified) {
Chris@0 148 // See if the client has provided the required HTTP headers.
Chris@0 149 $if_modified_since = $request->server->has('HTTP_IF_MODIFIED_SINCE') ? strtotime($request->server->get('HTTP_IF_MODIFIED_SINCE')) : FALSE;
Chris@0 150 $if_none_match = $request->server->has('HTTP_IF_NONE_MATCH') ? stripslashes($request->server->get('HTTP_IF_NONE_MATCH')) : FALSE;
Chris@0 151
Chris@0 152 if ($if_modified_since && $if_none_match
Chris@0 153 // etag must match.
Chris@0 154 && $if_none_match == $response->getEtag()
Chris@0 155 // if-modified-since must match.
Chris@0 156 && $if_modified_since == $last_modified->getTimestamp()) {
Chris@0 157 $response->setStatusCode(304);
Chris@0 158 $response->setContent(NULL);
Chris@0 159
Chris@0 160 // In the case of a 304 response, certain headers must be sent, and the
Chris@0 161 // remaining may not (see RFC 2616, section 10.3.5).
Chris@0 162 foreach (array_keys($response->headers->all()) as $name) {
Chris@0 163 if (!in_array($name, ['content-location', 'expires', 'cache-control', 'vary'])) {
Chris@0 164 $response->headers->remove($name);
Chris@0 165 }
Chris@0 166 }
Chris@0 167 }
Chris@0 168 }
Chris@0 169
Chris@0 170 return $response;
Chris@0 171 }
Chris@0 172
Chris@0 173 /**
Chris@0 174 * Fetches a response from the backend and stores it in the cache.
Chris@0 175 *
Chris@0 176 * @see drupal_page_header()
Chris@0 177 *
Chris@0 178 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 179 * A request object.
Chris@0 180 * @param int $type
Chris@0 181 * The type of the request (one of HttpKernelInterface::MASTER_REQUEST or
Chris@0 182 * HttpKernelInterface::SUB_REQUEST)
Chris@0 183 * @param bool $catch
Chris@0 184 * Whether to catch exceptions or not
Chris@0 185 *
Chris@0 186 * @returns \Symfony\Component\HttpFoundation\Response $response
Chris@0 187 * A response object.
Chris@0 188 */
Chris@0 189 protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
Chris@0 190 /** @var \Symfony\Component\HttpFoundation\Response $response */
Chris@0 191 $response = $this->httpKernel->handle($request, $type, $catch);
Chris@0 192
Chris@0 193 // Only set the 'X-Drupal-Cache' header if caching is allowed for this
Chris@0 194 // response.
Chris@0 195 if ($this->storeResponse($request, $response)) {
Chris@0 196 $response->headers->set('X-Drupal-Cache', 'MISS');
Chris@0 197 }
Chris@0 198
Chris@0 199 return $response;
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Stores a response in the page cache.
Chris@0 204 *
Chris@0 205 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 206 * A request object.
Chris@0 207 * @param \Symfony\Component\HttpFoundation\Response $response
Chris@0 208 * A response object that should be stored in the page cache.
Chris@0 209 *
Chris@0 210 * @returns bool
Chris@0 211 */
Chris@0 212 protected function storeResponse(Request $request, Response $response) {
Chris@0 213 // Drupal's primary cache invalidation architecture is cache tags: any
Chris@0 214 // response that varies by a configuration value or data in a content
Chris@0 215 // entity should have cache tags, to allow for instant cache invalidation
Chris@0 216 // when that data is updated. However, HTTP does not standardize how to
Chris@0 217 // encode cache tags in a response. Different CDNs implement their own
Chris@0 218 // approaches, and configurable reverse proxies (e.g., Varnish) allow for
Chris@0 219 // custom implementations. To keep Drupal's internal page cache simple, we
Chris@0 220 // only cache CacheableResponseInterface responses, since those provide a
Chris@0 221 // defined API for retrieving cache tags. For responses that do not
Chris@0 222 // implement CacheableResponseInterface, there's no easy way to distinguish
Chris@0 223 // responses that truly don't depend on any site data from responses that
Chris@0 224 // contain invalidation information customized to a particular proxy or
Chris@0 225 // CDN.
Chris@0 226 // - Drupal modules are encouraged to use CacheableResponseInterface
Chris@0 227 // responses where possible and to leave the encoding of that information
Chris@0 228 // into response headers to the corresponding proxy/CDN integration
Chris@0 229 // modules.
Chris@0 230 // - Custom applications that wish to provide internal page cache support
Chris@0 231 // for responses that do not implement CacheableResponseInterface may do
Chris@0 232 // so by replacing/extending this middleware service or adding another
Chris@0 233 // one.
Chris@0 234 if (!$response instanceof CacheableResponseInterface) {
Chris@0 235 return FALSE;
Chris@0 236 }
Chris@0 237
Chris@0 238 // Currently it is not possible to cache binary file or streamed responses:
Chris@0 239 // https://github.com/symfony/symfony/issues/9128#issuecomment-25088678.
Chris@0 240 // Therefore exclude them, even for subclasses that implement
Chris@0 241 // CacheableResponseInterface.
Chris@0 242 if ($response instanceof BinaryFileResponse || $response instanceof StreamedResponse) {
Chris@0 243 return FALSE;
Chris@0 244 }
Chris@0 245
Chris@0 246 // Allow policy rules to further restrict which responses to cache.
Chris@0 247 if ($this->responsePolicy->check($response, $request) === ResponsePolicyInterface::DENY) {
Chris@0 248 return FALSE;
Chris@0 249 }
Chris@0 250
Chris@0 251 $request_time = $request->server->get('REQUEST_TIME');
Chris@0 252 // The response passes all of the above checks, so cache it. Page cache
Chris@0 253 // entries default to Cache::PERMANENT since they will be expired via cache
Chris@0 254 // tags locally. Because of this, page cache ignores max age.
Chris@0 255 // - Get the tags from CacheableResponseInterface per the earlier comments.
Chris@0 256 // - Get the time expiration from the Expires header, rather than the
Chris@0 257 // interface, but see https://www.drupal.org/node/2352009 about possibly
Chris@0 258 // changing that.
Chris@0 259 $expire = 0;
Chris@0 260 // 403 and 404 responses can fill non-LRU cache backends and generally are
Chris@0 261 // likely to have a low cache hit rate. So do not cache them permanently.
Chris@0 262 if ($response->isClientError()) {
Chris@0 263 // Cache for an hour by default. If the 'cache_ttl_4xx' setting is
Chris@0 264 // set to 0 then do not cache the response.
Chris@0 265 $cache_ttl_4xx = Settings::get('cache_ttl_4xx', 3600);
Chris@0 266 if ($cache_ttl_4xx > 0) {
Chris@0 267 $expire = $request_time + $cache_ttl_4xx;
Chris@0 268 }
Chris@0 269 }
Chris@0 270 // The getExpires method could return NULL if Expires header is not set, so
Chris@0 271 // the returned value needs to be checked before calling getTimestamp.
Chris@0 272 elseif ($expires = $response->getExpires()) {
Chris@0 273 $date = $expires->getTimestamp();
Chris@0 274 $expire = ($date > $request_time) ? $date : Cache::PERMANENT;
Chris@0 275 }
Chris@0 276 else {
Chris@0 277 $expire = Cache::PERMANENT;
Chris@0 278 }
Chris@0 279
Chris@0 280 if ($expire === Cache::PERMANENT || $expire > $request_time) {
Chris@0 281 $tags = $response->getCacheableMetadata()->getCacheTags();
Chris@0 282 $this->set($request, $response, $expire, $tags);
Chris@0 283 }
Chris@0 284
Chris@0 285 return TRUE;
Chris@0 286 }
Chris@0 287
Chris@0 288 /**
Chris@0 289 * Returns a response object from the page cache.
Chris@0 290 *
Chris@0 291 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 292 * A request object.
Chris@0 293 * @param bool $allow_invalid
Chris@0 294 * (optional) If TRUE, a cache item may be returned even if it is expired or
Chris@0 295 * has been invalidated. Such items may sometimes be preferred, if the
Chris@0 296 * alternative is recalculating the value stored in the cache, especially
Chris@0 297 * if another concurrent request is already recalculating the same value.
Chris@0 298 * The "valid" property of the returned object indicates whether the item is
Chris@0 299 * valid or not. Defaults to FALSE.
Chris@0 300 *
Chris@0 301 * @return \Symfony\Component\HttpFoundation\Response|false
Chris@0 302 * The cached response or FALSE on failure.
Chris@0 303 */
Chris@0 304 protected function get(Request $request, $allow_invalid = FALSE) {
Chris@0 305 $cid = $this->getCacheId($request);
Chris@0 306 if ($cache = $this->cache->get($cid, $allow_invalid)) {
Chris@0 307 return $cache->data;
Chris@0 308 }
Chris@0 309 return FALSE;
Chris@0 310 }
Chris@0 311
Chris@0 312 /**
Chris@0 313 * Stores a response object in the page cache.
Chris@0 314 *
Chris@0 315 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 316 * A request object.
Chris@0 317 * @param \Symfony\Component\HttpFoundation\Response $response
Chris@0 318 * The response to store in the cache.
Chris@0 319 * @param int $expire
Chris@0 320 * One of the following values:
Chris@0 321 * - CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should
Chris@0 322 * not be removed unless it is deleted explicitly.
Chris@0 323 * - A Unix timestamp: Indicates that the item will be considered invalid
Chris@0 324 * after this time, i.e. it will not be returned by get() unless
Chris@0 325 * $allow_invalid has been set to TRUE. When the item has expired, it may
Chris@0 326 * be permanently deleted by the garbage collector at any time.
Chris@0 327 * @param array $tags
Chris@0 328 * An array of tags to be stored with the cache item. These should normally
Chris@0 329 * identify objects used to build the cache item, which should trigger
Chris@0 330 * cache invalidation when updated. For example if a cached item represents
Chris@0 331 * a node, both the node ID and the author's user ID might be passed in as
Chris@0 332 * tags. For example array('node' => array(123), 'user' => array(92)).
Chris@0 333 */
Chris@0 334 protected function set(Request $request, Response $response, $expire, array $tags) {
Chris@0 335 $cid = $this->getCacheId($request);
Chris@0 336 $this->cache->set($cid, $response, $expire, $tags);
Chris@0 337 }
Chris@0 338
Chris@0 339 /**
Chris@0 340 * Gets the page cache ID for this request.
Chris@0 341 *
Chris@0 342 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 343 * A request object.
Chris@0 344 *
Chris@0 345 * @return string
Chris@0 346 * The cache ID for this request.
Chris@0 347 */
Chris@0 348 protected function getCacheId(Request $request) {
Chris@18 349 // Once a cache ID is determined for the request, reuse it for the duration
Chris@18 350 // of the request. This ensures that when the cache is written, it is only
Chris@18 351 // keyed on request data that was available when it was read. For example,
Chris@18 352 // the request format might be NULL during cache lookup and then set during
Chris@18 353 // routing, in which case we want to key on NULL during writing, since that
Chris@18 354 // will be the value during lookups for subsequent requests.
Chris@18 355 if (!isset($this->cid)) {
Chris@18 356 $cid_parts = [
Chris@18 357 $request->getSchemeAndHttpHost() . $request->getRequestUri(),
Chris@18 358 $request->getRequestFormat(NULL),
Chris@18 359 ];
Chris@18 360 $this->cid = implode(':', $cid_parts);
Chris@18 361 }
Chris@18 362 return $this->cid;
Chris@0 363 }
Chris@0 364
Chris@0 365 }