PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/page_cache/src/StackMiddleware/PageCache.php

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