vendor/symfony/http-kernel/HttpCache/HttpCache.php line 332

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /*
  11. * This code is partially based on the Rack-Cache library by Ryan Tomayko,
  12. * which is released under the MIT license.
  13. * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
  14. */
  15. namespace Symfony\Component\HttpKernel\HttpCache;
  16. use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\HttpKernelInterface;
  20. use Symfony\Component\HttpKernel\TerminableInterface;
  21. /**
  22. * Cache provides HTTP caching.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class HttpCache implements HttpKernelInterface, TerminableInterface
  27. {
  28. public const BODY_EVAL_BOUNDARY_LENGTH = 24;
  29. private $kernel;
  30. private $store;
  31. private $request;
  32. private $surrogate;
  33. private $surrogateCacheStrategy;
  34. private $options = [];
  35. private $traces = [];
  36. /**
  37. * Constructor.
  38. *
  39. * The available options are:
  40. *
  41. * * debug If true, exceptions are thrown when things go wrong. Otherwise, the cache
  42. * will try to carry on and deliver a meaningful response.
  43. *
  44. * * trace_level May be one of 'none', 'short' and 'full'. For 'short', a concise trace of the
  45. * main request will be added as an HTTP header. 'full' will add traces for all
  46. * requests (including ESI subrequests). (default: 'full' if in debug; 'none' otherwise)
  47. *
  48. * * trace_header Header name to use for traces. (default: X-Symfony-Cache)
  49. *
  50. * * default_ttl The number of seconds that a cache entry should be considered
  51. * fresh when no explicit freshness information is provided in
  52. * a response. Explicit Cache-Control or Expires headers
  53. * override this value. (default: 0)
  54. *
  55. * * private_headers Set of request headers that trigger "private" cache-control behavior
  56. * on responses that don't explicitly state whether the response is
  57. * public or private via a Cache-Control directive. (default: Authorization and Cookie)
  58. *
  59. * * allow_reload Specifies whether the client can force a cache reload by including a
  60. * Cache-Control "no-cache" directive in the request. Set it to ``true``
  61. * for compliance with RFC 2616. (default: false)
  62. *
  63. * * allow_revalidate Specifies whether the client can force a cache revalidate by including
  64. * a Cache-Control "max-age=0" directive in the request. Set it to ``true``
  65. * for compliance with RFC 2616. (default: false)
  66. *
  67. * * stale_while_revalidate Specifies the default number of seconds (the granularity is the second as the
  68. * Response TTL precision is a second) during which the cache can immediately return
  69. * a stale response while it revalidates it in the background (default: 2).
  70. * This setting is overridden by the stale-while-revalidate HTTP Cache-Control
  71. * extension (see RFC 5861).
  72. *
  73. * * stale_if_error Specifies the default number of seconds (the granularity is the second) during which
  74. * the cache can serve a stale response when an error is encountered (default: 60).
  75. * This setting is overridden by the stale-if-error HTTP Cache-Control extension
  76. * (see RFC 5861).
  77. */
  78. public function __construct(HttpKernelInterface $kernel, StoreInterface $store, ?SurrogateInterface $surrogate = null, array $options = [])
  79. {
  80. $this->store = $store;
  81. $this->kernel = $kernel;
  82. $this->surrogate = $surrogate;
  83. // needed in case there is a fatal error because the backend is too slow to respond
  84. register_shutdown_function([$this->store, 'cleanup']);
  85. $this->options = array_merge([
  86. 'debug' => false,
  87. 'default_ttl' => 0,
  88. 'private_headers' => ['Authorization', 'Cookie'],
  89. 'allow_reload' => false,
  90. 'allow_revalidate' => false,
  91. 'stale_while_revalidate' => 2,
  92. 'stale_if_error' => 60,
  93. 'trace_level' => 'none',
  94. 'trace_header' => 'X-Symfony-Cache',
  95. ], $options);
  96. if (!isset($options['trace_level'])) {
  97. $this->options['trace_level'] = $this->options['debug'] ? 'full' : 'none';
  98. }
  99. }
  100. /**
  101. * Gets the current store.
  102. *
  103. * @return StoreInterface
  104. */
  105. public function getStore()
  106. {
  107. return $this->store;
  108. }
  109. /**
  110. * Returns an array of events that took place during processing of the last request.
  111. *
  112. * @return array
  113. */
  114. public function getTraces()
  115. {
  116. return $this->traces;
  117. }
  118. private function addTraces(Response $response)
  119. {
  120. $traceString = null;
  121. if ('full' === $this->options['trace_level']) {
  122. $traceString = $this->getLog();
  123. }
  124. if ('short' === $this->options['trace_level'] && $masterId = array_key_first($this->traces)) {
  125. $traceString = implode('/', $this->traces[$masterId]);
  126. }
  127. if (null !== $traceString) {
  128. $response->headers->add([$this->options['trace_header'] => $traceString]);
  129. }
  130. }
  131. /**
  132. * Returns a log message for the events of the last request processing.
  133. *
  134. * @return string
  135. */
  136. public function getLog()
  137. {
  138. $log = [];
  139. foreach ($this->traces as $request => $traces) {
  140. $log[] = sprintf('%s: %s', $request, implode(', ', $traces));
  141. }
  142. return implode('; ', $log);
  143. }
  144. /**
  145. * Gets the Request instance associated with the main request.
  146. *
  147. * @return Request
  148. */
  149. public function getRequest()
  150. {
  151. return $this->request;
  152. }
  153. /**
  154. * Gets the Kernel instance.
  155. *
  156. * @return HttpKernelInterface
  157. */
  158. public function getKernel()
  159. {
  160. return $this->kernel;
  161. }
  162. /**
  163. * Gets the Surrogate instance.
  164. *
  165. * @return SurrogateInterface
  166. *
  167. * @throws \LogicException
  168. */
  169. public function getSurrogate()
  170. {
  171. return $this->surrogate;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true)
  177. {
  178. // FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
  179. if (HttpKernelInterface::MAIN_REQUEST === $type) {
  180. $this->traces = [];
  181. // Keep a clone of the original request for surrogates so they can access it.
  182. // We must clone here to get a separate instance because the application will modify the request during
  183. // the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1
  184. // and adding the X-Forwarded-For header, see HttpCache::forward()).
  185. $this->request = clone $request;
  186. if (null !== $this->surrogate) {
  187. $this->surrogateCacheStrategy = $this->surrogate->createCacheStrategy();
  188. }
  189. }
  190. $this->traces[$this->getTraceKey($request)] = [];
  191. if (!$request->isMethodSafe()) {
  192. $response = $this->invalidate($request, $catch);
  193. } elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
  194. $response = $this->pass($request, $catch);
  195. } elseif ($this->options['allow_reload'] && $request->isNoCache()) {
  196. /*
  197. If allow_reload is configured and the client requests "Cache-Control: no-cache",
  198. reload the cache by fetching a fresh response and caching it (if possible).
  199. */
  200. $this->record($request, 'reload');
  201. $response = $this->fetch($request, $catch);
  202. } else {
  203. $response = $this->lookup($request, $catch);
  204. }
  205. $this->restoreResponseBody($request, $response);
  206. if (HttpKernelInterface::MAIN_REQUEST === $type) {
  207. $this->addTraces($response);
  208. }
  209. if (null !== $this->surrogate) {
  210. if (HttpKernelInterface::MAIN_REQUEST === $type) {
  211. $this->surrogateCacheStrategy->update($response);
  212. } else {
  213. $this->surrogateCacheStrategy->add($response);
  214. }
  215. }
  216. $response->prepare($request);
  217. $response->isNotModified($request);
  218. return $response;
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function terminate(Request $request, Response $response)
  224. {
  225. if ($this->getKernel() instanceof TerminableInterface) {
  226. $this->getKernel()->terminate($request, $response);
  227. }
  228. }
  229. /**
  230. * Forwards the Request to the backend without storing the Response in the cache.
  231. *
  232. * @param bool $catch Whether to process exceptions
  233. *
  234. * @return Response
  235. */
  236. protected function pass(Request $request, bool $catch = false)
  237. {
  238. $this->record($request, 'pass');
  239. return $this->forward($request, $catch);
  240. }
  241. /**
  242. * Invalidates non-safe methods (like POST, PUT, and DELETE).
  243. *
  244. * @param bool $catch Whether to process exceptions
  245. *
  246. * @return Response
  247. *
  248. * @throws \Exception
  249. *
  250. * @see RFC2616 13.10
  251. */
  252. protected function invalidate(Request $request, bool $catch = false)
  253. {
  254. $response = $this->pass($request, $catch);
  255. // invalidate only when the response is successful
  256. if ($response->isSuccessful() || $response->isRedirect()) {
  257. try {
  258. $this->store->invalidate($request);
  259. // As per the RFC, invalidate Location and Content-Location URLs if present
  260. foreach (['Location', 'Content-Location'] as $header) {
  261. if ($uri = $response->headers->get($header)) {
  262. $subRequest = Request::create($uri, 'get', [], [], [], $request->server->all());
  263. $this->store->invalidate($subRequest);
  264. }
  265. }
  266. $this->record($request, 'invalidate');
  267. } catch (\Exception $e) {
  268. $this->record($request, 'invalidate-failed');
  269. if ($this->options['debug']) {
  270. throw $e;
  271. }
  272. }
  273. }
  274. return $response;
  275. }
  276. /**
  277. * Lookups a Response from the cache for the given Request.
  278. *
  279. * When a matching cache entry is found and is fresh, it uses it as the
  280. * response without forwarding any request to the backend. When a matching
  281. * cache entry is found but is stale, it attempts to "validate" the entry with
  282. * the backend using conditional GET. When no matching cache entry is found,
  283. * it triggers "miss" processing.
  284. *
  285. * @param bool $catch Whether to process exceptions
  286. *
  287. * @return Response
  288. *
  289. * @throws \Exception
  290. */
  291. protected function lookup(Request $request, bool $catch = false)
  292. {
  293. try {
  294. $entry = $this->store->lookup($request);
  295. } catch (\Exception $e) {
  296. $this->record($request, 'lookup-failed');
  297. if ($this->options['debug']) {
  298. throw $e;
  299. }
  300. return $this->pass($request, $catch);
  301. }
  302. if (null === $entry) {
  303. $this->record($request, 'miss');
  304. return $this->fetch($request, $catch);
  305. }
  306. if (!$this->isFreshEnough($request, $entry)) {
  307. $this->record($request, 'stale');
  308. return $this->validate($request, $entry, $catch);
  309. }
  310. if ($entry->headers->hasCacheControlDirective('no-cache')) {
  311. return $this->validate($request, $entry, $catch);
  312. }
  313. $this->record($request, 'fresh');
  314. $entry->headers->set('Age', $entry->getAge());
  315. return $entry;
  316. }
  317. /**
  318. * Validates that a cache entry is fresh.
  319. *
  320. * The original request is used as a template for a conditional
  321. * GET request with the backend.
  322. *
  323. * @param bool $catch Whether to process exceptions
  324. *
  325. * @return Response
  326. */
  327. protected function validate(Request $request, Response $entry, bool $catch = false)
  328. {
  329. $subRequest = clone $request;
  330. // send no head requests because we want content
  331. if ('HEAD' === $request->getMethod()) {
  332. $subRequest->setMethod('GET');
  333. }
  334. // add our cached last-modified validator
  335. if ($entry->headers->has('Last-Modified')) {
  336. $subRequest->headers->set('If-Modified-Since', $entry->headers->get('Last-Modified'));
  337. }
  338. // Add our cached etag validator to the environment.
  339. // We keep the etags from the client to handle the case when the client
  340. // has a different private valid entry which is not cached here.
  341. $cachedEtags = $entry->getEtag() ? [$entry->getEtag()] : [];
  342. $requestEtags = $request->getETags();
  343. if ($etags = array_unique(array_merge($cachedEtags, $requestEtags))) {
  344. $subRequest->headers->set('If-None-Match', implode(', ', $etags));
  345. }
  346. $response = $this->forward($subRequest, $catch, $entry);
  347. if (304 == $response->getStatusCode()) {
  348. $this->record($request, 'valid');
  349. // return the response and not the cache entry if the response is valid but not cached
  350. $etag = $response->getEtag();
  351. if ($etag && \in_array($etag, $requestEtags) && !\in_array($etag, $cachedEtags)) {
  352. return $response;
  353. }
  354. $entry = clone $entry;
  355. $entry->headers->remove('Date');
  356. foreach (['Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified'] as $name) {
  357. if ($response->headers->has($name)) {
  358. $entry->headers->set($name, $response->headers->get($name));
  359. }
  360. }
  361. $response = $entry;
  362. } else {
  363. $this->record($request, 'invalid');
  364. }
  365. if ($response->isCacheable()) {
  366. $this->store($request, $response);
  367. }
  368. return $response;
  369. }
  370. /**
  371. * Unconditionally fetches a fresh response from the backend and
  372. * stores it in the cache if is cacheable.
  373. *
  374. * @param bool $catch Whether to process exceptions
  375. *
  376. * @return Response
  377. */
  378. protected function fetch(Request $request, bool $catch = false)
  379. {
  380. $subRequest = clone $request;
  381. // send no head requests because we want content
  382. if ('HEAD' === $request->getMethod()) {
  383. $subRequest->setMethod('GET');
  384. }
  385. // avoid that the backend sends no content
  386. $subRequest->headers->remove('If-Modified-Since');
  387. $subRequest->headers->remove('If-None-Match');
  388. $response = $this->forward($subRequest, $catch);
  389. if ($response->isCacheable()) {
  390. $this->store($request, $response);
  391. }
  392. return $response;
  393. }
  394. /**
  395. * Forwards the Request to the backend and returns the Response.
  396. *
  397. * All backend requests (cache passes, fetches, cache validations)
  398. * run through this method.
  399. *
  400. * @param bool $catch Whether to catch exceptions or not
  401. * @param Response|null $entry A Response instance (the stale entry if present, null otherwise)
  402. *
  403. * @return Response
  404. */
  405. protected function forward(Request $request, bool $catch = false, ?Response $entry = null)
  406. {
  407. if ($this->surrogate) {
  408. $this->surrogate->addSurrogateCapability($request);
  409. }
  410. // always a "master" request (as the real master request can be in cache)
  411. $response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $catch);
  412. /*
  413. * Support stale-if-error given on Responses or as a config option.
  414. * RFC 7234 summarizes in Section 4.2.4 (but also mentions with the individual
  415. * Cache-Control directives) that
  416. *
  417. * A cache MUST NOT generate a stale response if it is prohibited by an
  418. * explicit in-protocol directive (e.g., by a "no-store" or "no-cache"
  419. * cache directive, a "must-revalidate" cache-response-directive, or an
  420. * applicable "s-maxage" or "proxy-revalidate" cache-response-directive;
  421. * see Section 5.2.2).
  422. *
  423. * https://tools.ietf.org/html/rfc7234#section-4.2.4
  424. *
  425. * We deviate from this in one detail, namely that we *do* serve entries in the
  426. * stale-if-error case even if they have a `s-maxage` Cache-Control directive.
  427. */
  428. if (null !== $entry
  429. && \in_array($response->getStatusCode(), [500, 502, 503, 504])
  430. && !$entry->headers->hasCacheControlDirective('no-cache')
  431. && !$entry->mustRevalidate()
  432. ) {
  433. if (null === $age = $entry->headers->getCacheControlDirective('stale-if-error')) {
  434. $age = $this->options['stale_if_error'];
  435. }
  436. /*
  437. * stale-if-error gives the (extra) time that the Response may be used *after* it has become stale.
  438. * So we compare the time the $entry has been sitting in the cache already with the
  439. * time it was fresh plus the allowed grace period.
  440. */
  441. if ($entry->getAge() <= $entry->getMaxAge() + $age) {
  442. $this->record($request, 'stale-if-error');
  443. return $entry;
  444. }
  445. }
  446. /*
  447. RFC 7231 Sect. 7.1.1.2 says that a server that does not have a reasonably accurate
  448. clock MUST NOT send a "Date" header, although it MUST send one in most other cases
  449. except for 1xx or 5xx responses where it MAY do so.
  450. Anyway, a client that received a message without a "Date" header MUST add it.
  451. */
  452. if (!$response->headers->has('Date')) {
  453. $response->setDate(\DateTime::createFromFormat('U', time()));
  454. }
  455. $this->processResponseBody($request, $response);
  456. if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
  457. $response->setPrivate();
  458. } elseif ($this->options['default_ttl'] > 0 && null === $response->getTtl() && !$response->headers->getCacheControlDirective('must-revalidate')) {
  459. $response->setTtl($this->options['default_ttl']);
  460. }
  461. return $response;
  462. }
  463. /**
  464. * Checks whether the cache entry is "fresh enough" to satisfy the Request.
  465. *
  466. * @return bool
  467. */
  468. protected function isFreshEnough(Request $request, Response $entry)
  469. {
  470. if (!$entry->isFresh()) {
  471. return $this->lock($request, $entry);
  472. }
  473. if ($this->options['allow_revalidate'] && null !== $maxAge = $request->headers->getCacheControlDirective('max-age')) {
  474. return $maxAge > 0 && $maxAge >= $entry->getAge();
  475. }
  476. return true;
  477. }
  478. /**
  479. * Locks a Request during the call to the backend.
  480. *
  481. * @return bool true if the cache entry can be returned even if it is staled, false otherwise
  482. */
  483. protected function lock(Request $request, Response $entry)
  484. {
  485. // try to acquire a lock to call the backend
  486. $lock = $this->store->lock($request);
  487. if (true === $lock) {
  488. // we have the lock, call the backend
  489. return false;
  490. }
  491. // there is already another process calling the backend
  492. // May we serve a stale response?
  493. if ($this->mayServeStaleWhileRevalidate($entry)) {
  494. $this->record($request, 'stale-while-revalidate');
  495. return true;
  496. }
  497. // wait for the lock to be released
  498. if ($this->waitForLock($request)) {
  499. // replace the current entry with the fresh one
  500. $new = $this->lookup($request);
  501. $entry->headers = $new->headers;
  502. $entry->setContent($new->getContent());
  503. $entry->setStatusCode($new->getStatusCode());
  504. $entry->setProtocolVersion($new->getProtocolVersion());
  505. foreach ($new->headers->getCookies() as $cookie) {
  506. $entry->headers->setCookie($cookie);
  507. }
  508. } else {
  509. // backend is slow as hell, send a 503 response (to avoid the dog pile effect)
  510. $entry->setStatusCode(503);
  511. $entry->setContent('503 Service Unavailable');
  512. $entry->headers->set('Retry-After', 10);
  513. }
  514. return true;
  515. }
  516. /**
  517. * Writes the Response to the cache.
  518. *
  519. * @throws \Exception
  520. */
  521. protected function store(Request $request, Response $response)
  522. {
  523. try {
  524. $this->store->write($request, $response);
  525. $this->record($request, 'store');
  526. $response->headers->set('Age', $response->getAge());
  527. } catch (\Exception $e) {
  528. $this->record($request, 'store-failed');
  529. if ($this->options['debug']) {
  530. throw $e;
  531. }
  532. }
  533. // now that the response is cached, release the lock
  534. $this->store->unlock($request);
  535. }
  536. /**
  537. * Restores the Response body.
  538. */
  539. private function restoreResponseBody(Request $request, Response $response)
  540. {
  541. if ($response->headers->has('X-Body-Eval')) {
  542. \assert(self::BODY_EVAL_BOUNDARY_LENGTH === 24);
  543. ob_start();
  544. $content = $response->getContent();
  545. $boundary = substr($content, 0, 24);
  546. $j = strpos($content, $boundary, 24);
  547. echo substr($content, 24, $j - 24);
  548. $i = $j + 24;
  549. while (false !== $j = strpos($content, $boundary, $i)) {
  550. [$uri, $alt, $ignoreErrors, $part] = explode("\n", substr($content, $i, $j - $i), 4);
  551. $i = $j + 24;
  552. echo $this->surrogate->handle($this, $uri, $alt, $ignoreErrors);
  553. echo $part;
  554. }
  555. $response->setContent(ob_get_clean());
  556. $response->headers->remove('X-Body-Eval');
  557. if (!$response->headers->has('Transfer-Encoding')) {
  558. $response->headers->set('Content-Length', \strlen($response->getContent()));
  559. }
  560. } elseif ($response->headers->has('X-Body-File')) {
  561. // Response does not include possibly dynamic content (ESI, SSI), so we need
  562. // not handle the content for HEAD requests
  563. if (!$request->isMethod('HEAD')) {
  564. $response->setContent(file_get_contents($response->headers->get('X-Body-File')));
  565. }
  566. } else {
  567. return;
  568. }
  569. $response->headers->remove('X-Body-File');
  570. }
  571. protected function processResponseBody(Request $request, Response $response)
  572. {
  573. if (null !== $this->surrogate && $this->surrogate->needsParsing($response)) {
  574. $this->surrogate->process($request, $response);
  575. }
  576. }
  577. /**
  578. * Checks if the Request includes authorization or other sensitive information
  579. * that should cause the Response to be considered private by default.
  580. */
  581. private function isPrivateRequest(Request $request): bool
  582. {
  583. foreach ($this->options['private_headers'] as $key) {
  584. $key = strtolower(str_replace('HTTP_', '', $key));
  585. if ('cookie' === $key) {
  586. if (\count($request->cookies->all())) {
  587. return true;
  588. }
  589. } elseif ($request->headers->has($key)) {
  590. return true;
  591. }
  592. }
  593. return false;
  594. }
  595. /**
  596. * Records that an event took place.
  597. */
  598. private function record(Request $request, string $event)
  599. {
  600. $this->traces[$this->getTraceKey($request)][] = $event;
  601. }
  602. /**
  603. * Calculates the key we use in the "trace" array for a given request.
  604. */
  605. private function getTraceKey(Request $request): string
  606. {
  607. $path = $request->getPathInfo();
  608. if ($qs = $request->getQueryString()) {
  609. $path .= '?'.$qs;
  610. }
  611. try {
  612. return $request->getMethod().' '.$path;
  613. } catch (SuspiciousOperationException $e) {
  614. return '_BAD_METHOD_ '.$path;
  615. }
  616. }
  617. /**
  618. * Checks whether the given (cached) response may be served as "stale" when a revalidation
  619. * is currently in progress.
  620. */
  621. private function mayServeStaleWhileRevalidate(Response $entry): bool
  622. {
  623. $timeout = $entry->headers->getCacheControlDirective('stale-while-revalidate');
  624. if (null === $timeout) {
  625. $timeout = $this->options['stale_while_revalidate'];
  626. }
  627. $age = $entry->getAge();
  628. $maxAge = $entry->getMaxAge() ?? 0;
  629. $ttl = $maxAge - $age;
  630. return abs($ttl) < $timeout;
  631. }
  632. /**
  633. * Waits for the store to release a locked entry.
  634. */
  635. private function waitForLock(Request $request): bool
  636. {
  637. $wait = 0;
  638. while ($this->store->isLocked($request) && $wait < 100) {
  639. usleep(50000);
  640. ++$wait;
  641. }
  642. return $wait < 100;
  643. }
  644. }