HttpCache.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\HttpKernel\HttpKernelInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. /**
  19. * Cache provides HTTP caching.
  20. *
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. */
  23. class HttpCache implements HttpKernelInterface
  24. {
  25. protected $kernel;
  26. protected $traces;
  27. protected $store;
  28. protected $esi;
  29. /**
  30. * Constructor.
  31. *
  32. * The available options are:
  33. *
  34. * * debug: If true, the traces are added as a HTTP header to ease debugging
  35. *
  36. * * default_ttl The number of seconds that a cache entry should be considered
  37. * fresh when no explicit freshness information is provided in
  38. * a response. Explicit Cache-Control or Expires headers
  39. * override this value. (default: 0)
  40. *
  41. * * private_headers Set of request headers that trigger "private" cache-control behavior
  42. * on responses that don't explicitly state whether the response is
  43. * public or private via a Cache-Control directive. (default: Authorization and Cookie)
  44. *
  45. * * allow_reload Specifies whether the client can force a cache reload by including a
  46. * Cache-Control "no-cache" directive in the request. Set it to ``true``
  47. * for compliance with RFC 2616. (default: false)
  48. *
  49. * * allow_revalidate Specifies whether the client can force a cache revalidate by including
  50. * a Cache-Control "max-age=0" directive in the request. Set it to ``true``
  51. * for compliance with RFC 2616. (default: false)
  52. *
  53. * * stale_while_revalidate Specifies the default number of seconds (the granularity is the second as the
  54. * Response TTL precision is a second) during which the cache can immediately return
  55. * a stale response while it revalidates it in the background (default: 2).
  56. * This setting is overridden by the stale-while-revalidate HTTP Cache-Control
  57. * extension (see RFC 5861).
  58. *
  59. * * stale_if_error Specifies the default number of seconds (the granularity is the second) during which
  60. * the cache can serve a stale response when an error is encountered (default: 60).
  61. * This setting is overridden by the stale-if-error HTTP Cache-Control extension
  62. * (see RFC 5861).
  63. *
  64. * @param HttpKernelInterface $kernel An HttpKernelInterface instance
  65. * @param Store $store A Store instance
  66. * @param Esi $esi An Esi instance
  67. * @param array $options An array of options
  68. */
  69. public function __construct(HttpKernelInterface $kernel, Store $store, Esi $esi = null, array $options = array())
  70. {
  71. $this->store = $store;
  72. $this->kernel = $kernel;
  73. // needed in case there is a fatal error because the backend is too slow to respond
  74. register_shutdown_function(array($this->store, '__destruct'));
  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. $this->esi = $esi;
  85. }
  86. /**
  87. * Returns an array of events that took place during processing of the last request.
  88. *
  89. * @return array An array of events
  90. */
  91. public function getTraces()
  92. {
  93. return $this->traces;
  94. }
  95. /**
  96. * Returns a log message for the events of the last request processing.
  97. *
  98. * @return string A log message
  99. */
  100. public function getLog()
  101. {
  102. $log = array();
  103. foreach ($this->traces as $request => $traces) {
  104. $log[] = sprintf('%s: %s', $request, implode(', ', $traces));
  105. }
  106. return implode('; ', $log);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  112. {
  113. // FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
  114. if (HttpKernelInterface::MASTER_REQUEST === $type) {
  115. $this->traces = array();
  116. }
  117. $path = $request->getPathInfo();
  118. if ($qs = $request->getQueryString()) {
  119. $path .= '?'.$qs;
  120. }
  121. $this->traces[$request->getMethod().' '.$path] = array();
  122. if (!$request->isMethodSafe($request)) {
  123. $response = $this->invalidate($request, $catch);
  124. } elseif ($request->headers->has('expect')) {
  125. $response = $this->pass($request, $catch);
  126. } else {
  127. $response = $this->lookup($request, $catch);
  128. }
  129. $response->isNotModified($request);
  130. $this->restoreResponseBody($request, $response);
  131. if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
  132. $response->headers->set('X-Symfony-Cache', $this->getLog());
  133. }
  134. return $response;
  135. }
  136. /**
  137. * Forwards the Request to the backend without storing the Response in the cache.
  138. *
  139. * @param Request $request A Request instance
  140. * @param Boolean $catch whether to process exceptions
  141. *
  142. * @return Response A Response instance
  143. */
  144. protected function pass(Request $request, $catch = false)
  145. {
  146. $this->record($request, 'pass');
  147. return $this->forward($request, $catch);
  148. }
  149. /**
  150. * Invalidates non-safe methods (like POST, PUT, and DELETE).
  151. *
  152. * @param Request $request A Request instance
  153. * @param Boolean $catch whether to process exceptions
  154. *
  155. * @return Response A Response instance
  156. *
  157. * @see RFC2616 13.10
  158. */
  159. protected function invalidate(Request $request, $catch = false)
  160. {
  161. $response = $this->pass($request, $catch);
  162. // invalidate only when the response is successful
  163. if ($response->isSuccessful() || $response->isRedirect()) {
  164. try {
  165. $this->store->invalidate($request, $catch);
  166. $this->record($request, 'invalidate');
  167. } catch (\Exception $e) {
  168. $this->record($request, 'invalidate-failed');
  169. if ($this->options['debug']) {
  170. throw $e;
  171. }
  172. }
  173. }
  174. return $response;
  175. }
  176. /**
  177. * Lookups a Response from the cache for the given Request.
  178. *
  179. * When a matching cache entry is found and is fresh, it uses it as the
  180. * response without forwarding any request to the backend. When a matching
  181. * cache entry is found but is stale, it attempts to "validate" the entry with
  182. * the backend using conditional GET. When no matching cache entry is found,
  183. * it triggers "miss" processing.
  184. *
  185. * @param Request $request A Request instance
  186. * @param Boolean $catch whether to process exceptions
  187. *
  188. * @return Response A Response instance
  189. */
  190. protected function lookup(Request $request, $catch = false)
  191. {
  192. // if allow_reload and no-cache Cache-Control, allow a cache reload
  193. if ($this->options['allow_reload'] && $request->isNoCache()) {
  194. $this->record($request, 'reload');
  195. return $this->fetch($request);
  196. }
  197. try {
  198. $entry = $this->store->lookup($request);
  199. } catch (\Exception $e) {
  200. $this->record($request, 'lookup-failed');
  201. if ($this->options['debug']) {
  202. throw $e;
  203. }
  204. return $this->pass($request, $catch);
  205. }
  206. if (null === $entry) {
  207. $this->record($request, 'miss');
  208. return $this->fetch($request, $catch);
  209. }
  210. if (!$this->isFreshEnough($request, $entry)) {
  211. $this->record($request, 'stale');
  212. return $this->validate($request, $entry);
  213. }
  214. $this->record($request, 'fresh');
  215. $entry->headers->set('Age', $entry->getAge());
  216. return $entry;
  217. }
  218. /**
  219. * Validates that a cache entry is fresh.
  220. *
  221. * The original request is used as a template for a conditional
  222. * GET request with the backend.
  223. *
  224. * @param Request $request A Request instance
  225. * @param Response $entry A Response instance to validate
  226. *
  227. * @return Response A Response instance
  228. */
  229. protected function validate(Request $request, Response $entry)
  230. {
  231. $subRequest = clone $request;
  232. // send no head requests because we want content
  233. $subRequest->setMethod('get');
  234. // add our cached last-modified validator
  235. $subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified'));
  236. // Add our cached etag validator to the environment.
  237. // We keep the etags from the client to handle the case when the client
  238. // has a different private valid entry which is not cached here.
  239. $cachedEtags = array($entry->getEtag());
  240. $requestEtags = $request->getEtags();
  241. $etags = array_unique(array_merge($cachedEtags, $requestEtags));
  242. $subRequest->headers->set('if_none_match', $etags ? implode(', ', $etags) : '');
  243. $response = $this->forward($subRequest, false, $entry);
  244. if (304 == $response->getStatusCode()) {
  245. $this->record($request, 'valid');
  246. // return the response and not the cache entry if the response is valid but not cached
  247. $etag = $response->getEtag();
  248. if ($etag && in_array($etag, $requestEtags) && !in_array($etag, $cachedEtags)) {
  249. return $response;
  250. }
  251. $entry = clone $entry;
  252. $entry->headers->remove('Date');
  253. foreach (array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) {
  254. if ($response->headers->has($name)) {
  255. $entry->headers->set($name, $response->headers->get($name));
  256. }
  257. }
  258. $response = $entry;
  259. } else {
  260. $this->record($request, 'invalid');
  261. }
  262. if ($response->isCacheable()) {
  263. $this->store($request, $response);
  264. }
  265. return $response;
  266. }
  267. /**
  268. * Forwards the Request to the backend and determines whether the response should be stored.
  269. *
  270. * This methods is trigered when the cache missed or a reload is required.
  271. *
  272. * @param Request $request A Request instance
  273. * @param Boolean $catch whether to process exceptions
  274. *
  275. * @return Response A Response instance
  276. */
  277. protected function fetch(Request $request, $catch = false)
  278. {
  279. $subRequest = clone $request;
  280. // send no head requests because we want content
  281. $subRequest->setMethod('get');
  282. // avoid that the backend sends no content
  283. $subRequest->headers->remove('if_modified_since');
  284. $subRequest->headers->remove('if_none_match');
  285. $response = $this->forward($subRequest, $catch);
  286. if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
  287. $response->setPrivate(true);
  288. } elseif ($this->options['default_ttl'] > 0 && null === $response->getTtl() && !$response->headers->getCacheControlDirective('must-revalidate')) {
  289. $response->setTtl($this->options['default_ttl']);
  290. }
  291. if ($response->isCacheable()) {
  292. $this->store($request, $response);
  293. }
  294. return $response;
  295. }
  296. /**
  297. * Forwards the Request to the backend and returns the Response.
  298. *
  299. * @param Request $request A Request instance
  300. * @param Boolean $catch Whether to catch exceptions or not
  301. * @param Response $response A Response instance (the stale entry if present, null otherwise)
  302. *
  303. * @return Response A Response instance
  304. */
  305. protected function forward(Request $request, $catch = false, Response $entry = null)
  306. {
  307. if ($this->esi) {
  308. $this->esi->addSurrogateEsiCapability($request);
  309. }
  310. // always a "master" request (as the real master request can be in cache)
  311. $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch);
  312. // FIXME: we probably need to also catch exceptions if raw === true
  313. // we don't implement the stale-if-error on Requests, which is nonetheless part of the RFC
  314. if (null !== $entry && in_array($response->getStatusCode(), array(500, 502, 503, 504))) {
  315. if (null === $age = $entry->headers->getCacheControlDirective('stale-if-error')) {
  316. $age = $this->options['stale_if_error'];
  317. }
  318. if (abs($entry->getTtl()) < $age) {
  319. $this->record($request, 'stale-if-error');
  320. return $entry;
  321. }
  322. }
  323. $this->processResponseBody($request, $response);
  324. return $response;
  325. }
  326. /**
  327. * Checks whether the cache entry is "fresh enough" to satisfy the Request.
  328. *
  329. * @param Request $request A Request instance
  330. * @param Response $entry A Response instance
  331. *
  332. * @return Boolean true if the cache entry if fresh enough, false otherwise
  333. */
  334. protected function isFreshEnough(Request $request, Response $entry)
  335. {
  336. if (!$entry->isFresh()) {
  337. return $this->lock($request, $entry);
  338. }
  339. if ($this->options['allow_revalidate'] && null !== $maxAge = $request->headers->getCacheControlDirective('max-age')) {
  340. return $maxAge > 0 && $maxAge >= $entry->getAge();
  341. }
  342. return true;
  343. }
  344. /**
  345. * Locks a Request during the call to the backend.
  346. *
  347. * @param Request $request A Request instance
  348. * @param Response $entry A Response instance
  349. *
  350. * @return Boolean true if the cache entry can be returned even if it is staled, false otherwise
  351. */
  352. protected function lock(Request $request, Response $entry)
  353. {
  354. // try to acquire a lock to call the backend
  355. $lock = $this->store->lock($request, $entry);
  356. // there is already another process calling the backend
  357. if (true !== $lock) {
  358. // check if we can serve the stale entry
  359. if (null === $age = $entry->headers->getCacheControlDirective('stale-while-revalidate')) {
  360. $age = $this->options['stale_while_revalidate'];
  361. }
  362. if (abs($entry->getTtl()) < $age) {
  363. $this->record($request, 'stale-while-revalidate');
  364. // server the stale response while there is a revalidation
  365. return true;
  366. } else {
  367. // wait for the lock to be released
  368. $wait = 0;
  369. while (file_exists($lock) && $wait < 5000000) {
  370. usleep($wait += 50000);
  371. }
  372. if ($wait < 2000000) {
  373. // replace the current entry with the fresh one
  374. $new = $this->lookup($request);
  375. $entry->headers = $new->headers;
  376. $entry->setContent($new->getContent());
  377. $entry->setStatusCode($new->getStatusCode());
  378. $entry->setProtocolVersion($new->getProtocolVersion());
  379. $entry->setCookies($new->getCookies());
  380. return true;
  381. } else {
  382. // backend is slow as hell, send a 503 response (to avoid the dog pile effect)
  383. $entry->setStatusCode(503);
  384. $entry->setContent('503 Service Unavailable');
  385. $entry->headers->set('Retry-After', 10);
  386. return true;
  387. }
  388. }
  389. }
  390. // we have the lock, call the backend
  391. return false;
  392. }
  393. /**
  394. * Writes the Response to the cache.
  395. *
  396. * @param Request $request A Request instance
  397. * @param Response $response A Response instance
  398. */
  399. protected function store(Request $request, Response $response)
  400. {
  401. try {
  402. $this->store->write($request, $response);
  403. $this->record($request, 'store');
  404. $response->headers->set('Age', $response->getAge());
  405. } catch (\Exception $e) {
  406. $this->record($request, 'store-failed');
  407. if ($this->options['debug']) {
  408. throw $e;
  409. }
  410. }
  411. // now that the response is cached, release the lock
  412. $this->store->unlock($request);
  413. }
  414. /**
  415. * Restores the Response body.
  416. *
  417. * @param Response $response A Response instance
  418. *
  419. * @return Response A Response instance
  420. */
  421. protected function restoreResponseBody(Request $request, Response $response)
  422. {
  423. if ('head' === strtolower($request->getMethod())) {
  424. $response->setContent('');
  425. $response->headers->remove('X-Body-Eval');
  426. $response->headers->remove('X-Body-File');
  427. return;
  428. }
  429. if ($response->headers->has('X-Body-Eval')) {
  430. ob_start();
  431. if ($response->headers->has('X-Body-File')) {
  432. include $response->headers->get('X-Body-File');
  433. } else {
  434. eval('; ?>'.$response->getContent().'<?php ;');
  435. }
  436. $response->setContent(ob_get_clean());
  437. $response->headers->remove('X-Body-Eval');
  438. } elseif ($response->headers->has('X-Body-File')) {
  439. $response->setContent(file_get_contents($response->headers->get('X-Body-File')));
  440. } else {
  441. return;
  442. }
  443. $response->headers->remove('X-Body-File');
  444. if (!$response->headers->has('Transfer-Encoding')) {
  445. $response->headers->set('Content-Length', strlen($response->getContent()));
  446. }
  447. }
  448. protected function processResponseBody(Request $request, Response $response)
  449. {
  450. if (null !== $this->esi && $this->esi->needsEsiParsing($response)) {
  451. $this->esi->process($request, $response);
  452. }
  453. }
  454. /**
  455. * Checks if the Request includes authorization or other sensitive information
  456. * that should cause the Response to be considered private by default.
  457. *
  458. * @param Request $request A Request instance
  459. *
  460. * @return Boolean true if the Request is private, false otherwise
  461. */
  462. protected function isPrivateRequest(Request $request)
  463. {
  464. foreach ($this->options['private_headers'] as $key) {
  465. $key = strtolower(str_replace('HTTP_', '', $key));
  466. if ('cookie' === $key) {
  467. if (count($request->cookies->all())) {
  468. return true;
  469. }
  470. } elseif ($request->headers->has($key)) {
  471. return true;
  472. }
  473. }
  474. return false;
  475. }
  476. /**
  477. * Records that an event took place.
  478. *
  479. * @param string $event The event name
  480. */
  481. protected function record(Request $request, $event)
  482. {
  483. $path = $request->getPathInfo();
  484. if ($qs = $request->getQueryString()) {
  485. $path .= '?'.$qs;
  486. }
  487. $this->traces[$request->getMethod().' '.$path][] = $event;
  488. }
  489. }