HttpCache.php 25 KB

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