HttpCache.php 23 KB

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