Store.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Symfony\Component\HttpKernel\HttpCache;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. /**
  17. * Store implements all the logic for storing cache metadata (Request and Response headers).
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class Store implements StoreInterface
  22. {
  23. /** @var \SplObjectStorage<Request, string> */
  24. private \SplObjectStorage $keyCache;
  25. /** @var array<string, resource> */
  26. private array $locks = [];
  27. /**
  28. * Constructor.
  29. *
  30. * The available options are:
  31. *
  32. * * private_headers Set of response headers that should not be stored
  33. * when a response is cached. (default: Set-Cookie)
  34. *
  35. * @throws \RuntimeException
  36. */
  37. public function __construct(
  38. protected string $root,
  39. private array $options = [],
  40. ) {
  41. if (!is_dir($this->root) && !@mkdir($this->root, 0o777, true) && !is_dir($this->root)) {
  42. throw new \RuntimeException(\sprintf('Unable to create the store directory (%s).', $this->root));
  43. }
  44. $this->keyCache = new \SplObjectStorage();
  45. $this->options['private_headers'] ??= ['Set-Cookie'];
  46. }
  47. /**
  48. * Cleanups storage.
  49. */
  50. public function cleanup(): void
  51. {
  52. // unlock everything
  53. foreach ($this->locks as $lock) {
  54. flock($lock, \LOCK_UN);
  55. fclose($lock);
  56. }
  57. $this->locks = [];
  58. }
  59. /**
  60. * Tries to lock the cache for a given Request, without blocking.
  61. *
  62. * @return bool|string true if the lock is acquired, the path to the current lock otherwise
  63. */
  64. public function lock(Request $request): bool|string
  65. {
  66. $key = $this->getCacheKey($request);
  67. if (!isset($this->locks[$key])) {
  68. $path = $this->getPath($key);
  69. if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0o777, true) && !is_dir(\dirname($path))) {
  70. return $path;
  71. }
  72. $h = fopen($path, 'c');
  73. if (!flock($h, \LOCK_EX | \LOCK_NB)) {
  74. fclose($h);
  75. return $path;
  76. }
  77. $this->locks[$key] = $h;
  78. }
  79. return true;
  80. }
  81. /**
  82. * Releases the lock for the given Request.
  83. *
  84. * @return bool False if the lock file does not exist or cannot be unlocked, true otherwise
  85. */
  86. public function unlock(Request $request): bool
  87. {
  88. $key = $this->getCacheKey($request);
  89. if (isset($this->locks[$key])) {
  90. flock($this->locks[$key], \LOCK_UN);
  91. fclose($this->locks[$key]);
  92. unset($this->locks[$key]);
  93. return true;
  94. }
  95. return false;
  96. }
  97. public function isLocked(Request $request): bool
  98. {
  99. $key = $this->getCacheKey($request);
  100. if (isset($this->locks[$key])) {
  101. return true; // shortcut if lock held by this process
  102. }
  103. if (!is_file($path = $this->getPath($key))) {
  104. return false;
  105. }
  106. $h = fopen($path, 'r');
  107. flock($h, \LOCK_EX | \LOCK_NB, $wouldBlock);
  108. flock($h, \LOCK_UN); // release the lock we just acquired
  109. fclose($h);
  110. return (bool) $wouldBlock;
  111. }
  112. /**
  113. * Locates a cached Response for the Request provided.
  114. */
  115. public function lookup(Request $request): ?Response
  116. {
  117. $key = $this->getCacheKey($request);
  118. if (!$entries = $this->getMetadata($key)) {
  119. return null;
  120. }
  121. // find a cached entry that matches the request.
  122. $match = null;
  123. foreach ($entries as $entry) {
  124. if ($this->requestsMatch(isset($entry[1]['vary'][0]) ? implode(', ', $entry[1]['vary']) : '', $request->headers->all(), $entry[0])) {
  125. $match = $entry;
  126. break;
  127. }
  128. }
  129. if (null === $match) {
  130. return null;
  131. }
  132. $headers = $match[1];
  133. if (file_exists($path = $this->getPath($headers['x-content-digest'][0]))) {
  134. return $this->restoreResponse($headers, $path);
  135. }
  136. // TODO the metaStore referenced an entity that doesn't exist in
  137. // the entityStore. We definitely want to return nil but we should
  138. // also purge the entry from the meta-store when this is detected.
  139. return null;
  140. }
  141. /**
  142. * Writes a cache entry to the store for the given Request and Response.
  143. *
  144. * Existing entries are read and any that match the response are removed. This
  145. * method calls write with the new list of cache entries.
  146. *
  147. * @throws \RuntimeException
  148. */
  149. public function write(Request $request, Response $response): string
  150. {
  151. $key = $this->getCacheKey($request);
  152. $storedEnv = $this->persistRequest($request);
  153. if ($response->headers->has('X-Body-File')) {
  154. // Assume the response came from disk, but at least perform some safeguard checks
  155. if (!$response->headers->has('X-Content-Digest')) {
  156. throw new \RuntimeException('A restored response must have the X-Content-Digest header.');
  157. }
  158. $digest = $response->headers->get('X-Content-Digest');
  159. if ($this->getPath($digest) !== $response->headers->get('X-Body-File')) {
  160. throw new \RuntimeException('X-Body-File and X-Content-Digest do not match.');
  161. }
  162. // Everything seems ok, omit writing content to disk
  163. } else {
  164. $digest = $this->generateContentDigest($response);
  165. $response->headers->set('X-Content-Digest', $digest);
  166. if (!$this->save($digest, $response->getContent(), false)) {
  167. throw new \RuntimeException('Unable to store the entity.');
  168. }
  169. if (!$response->headers->has('Transfer-Encoding')) {
  170. $response->headers->set('Content-Length', \strlen($response->getContent()));
  171. }
  172. }
  173. // read existing cache entries, remove non-varying, and add this one to the list
  174. $entries = [];
  175. $vary = implode(', ', $response->headers->all('vary'));
  176. foreach ($this->getMetadata($key) as $entry) {
  177. if (!$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
  178. $entries[] = $entry;
  179. }
  180. }
  181. $headers = $this->persistResponse($response);
  182. unset($headers['age']);
  183. foreach ($this->options['private_headers'] as $h) {
  184. unset($headers[strtolower($h)]);
  185. }
  186. array_unshift($entries, [$storedEnv, $headers]);
  187. if (!$this->save($key, serialize($entries))) {
  188. throw new \RuntimeException('Unable to store the metadata.');
  189. }
  190. return $key;
  191. }
  192. /**
  193. * Returns content digest for $response.
  194. */
  195. protected function generateContentDigest(Response $response): string
  196. {
  197. return 'en'.hash('xxh128', $response->getContent());
  198. }
  199. /**
  200. * Invalidates all cache entries that match the request.
  201. *
  202. * @throws \RuntimeException
  203. */
  204. public function invalidate(Request $request): void
  205. {
  206. $modified = false;
  207. $key = $this->getCacheKey($request);
  208. $entries = [];
  209. foreach ($this->getMetadata($key) as $entry) {
  210. $response = $this->restoreResponse($entry[1]);
  211. if ($response->isFresh()) {
  212. $response->expire();
  213. $modified = true;
  214. $entries[] = [$entry[0], $this->persistResponse($response)];
  215. } else {
  216. $entries[] = $entry;
  217. }
  218. }
  219. if ($modified && !$this->save($key, serialize($entries))) {
  220. throw new \RuntimeException('Unable to store the metadata.');
  221. }
  222. }
  223. /**
  224. * Determines whether two Request HTTP header sets are non-varying based on
  225. * the vary response header value provided.
  226. *
  227. * @param string|null $vary A Response vary header
  228. * @param array $env1 A Request HTTP header array
  229. * @param array $env2 A Request HTTP header array
  230. */
  231. private function requestsMatch(?string $vary, array $env1, array $env2): bool
  232. {
  233. if ('' === ($vary ?? '')) {
  234. return true;
  235. }
  236. foreach (preg_split('/[\s,]+/', $vary) as $header) {
  237. $key = str_replace('_', '-', strtolower($header));
  238. $v1 = $env1[$key] ?? null;
  239. $v2 = $env2[$key] ?? null;
  240. if ($v1 !== $v2) {
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. /**
  247. * Gets all data associated with the given key.
  248. *
  249. * Use this method only if you know what you are doing.
  250. */
  251. private function getMetadata(string $key): array
  252. {
  253. if (!$entries = $this->load($key)) {
  254. return [];
  255. }
  256. return unserialize($entries) ?: [];
  257. }
  258. /**
  259. * Purges data for the given URL.
  260. *
  261. * This method purges both the HTTP and the HTTPS version of the cache entry.
  262. *
  263. * @return bool true if the URL exists with either HTTP or HTTPS scheme and has been purged, false otherwise
  264. */
  265. public function purge(string $url): bool
  266. {
  267. $http = preg_replace('#^https:#', 'http:', $url);
  268. $https = preg_replace('#^http:#', 'https:', $url);
  269. $purgedHttp = $this->doPurge($http);
  270. $purgedHttps = $this->doPurge($https);
  271. return $purgedHttp || $purgedHttps;
  272. }
  273. /**
  274. * Purges data for the given URL.
  275. */
  276. private function doPurge(string $url): bool
  277. {
  278. $key = $this->getCacheKey(Request::create($url));
  279. if (isset($this->locks[$key])) {
  280. flock($this->locks[$key], \LOCK_UN);
  281. fclose($this->locks[$key]);
  282. unset($this->locks[$key]);
  283. }
  284. if (is_file($path = $this->getPath($key))) {
  285. unlink($path);
  286. return true;
  287. }
  288. return false;
  289. }
  290. /**
  291. * Loads data for the given key.
  292. */
  293. private function load(string $key): ?string
  294. {
  295. $path = $this->getPath($key);
  296. return is_file($path) && false !== ($contents = @file_get_contents($path)) ? $contents : null;
  297. }
  298. /**
  299. * Save data for the given key.
  300. */
  301. private function save(string $key, string $data, bool $overwrite = true): bool
  302. {
  303. $path = $this->getPath($key);
  304. if (!$overwrite && file_exists($path)) {
  305. return true;
  306. }
  307. if (isset($this->locks[$key])) {
  308. $fp = $this->locks[$key];
  309. @ftruncate($fp, 0);
  310. @fseek($fp, 0);
  311. $len = @fwrite($fp, $data);
  312. if (\strlen($data) !== $len) {
  313. @ftruncate($fp, 0);
  314. return false;
  315. }
  316. } else {
  317. if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0o777, true) && !is_dir(\dirname($path))) {
  318. return false;
  319. }
  320. $tmpFile = tempnam(\dirname($path), basename($path));
  321. if (false === $fp = @fopen($tmpFile, 'w')) {
  322. @unlink($tmpFile);
  323. return false;
  324. }
  325. @fwrite($fp, $data);
  326. @fclose($fp);
  327. if ($data != file_get_contents($tmpFile)) {
  328. @unlink($tmpFile);
  329. return false;
  330. }
  331. if (false === @rename($tmpFile, $path)) {
  332. @unlink($tmpFile);
  333. return false;
  334. }
  335. }
  336. @chmod($path, 0o666 & ~umask());
  337. return true;
  338. }
  339. public function getPath(string $key): string
  340. {
  341. return $this->root.\DIRECTORY_SEPARATOR.substr($key, 0, 2).\DIRECTORY_SEPARATOR.substr($key, 2, 2).\DIRECTORY_SEPARATOR.substr($key, 4, 2).\DIRECTORY_SEPARATOR.substr($key, 6);
  342. }
  343. /**
  344. * Generates a cache key for the given Request.
  345. *
  346. * This method should return a key that must only depend on a
  347. * normalized version of the request URI.
  348. *
  349. * If the same URI can have more than one representation, based on some
  350. * headers, use a Vary header to indicate them, and each representation will
  351. * be stored independently under the same cache key.
  352. */
  353. protected function generateCacheKey(Request $request): string
  354. {
  355. $key = $request->getUri();
  356. if ('QUERY' === $request->getMethod()) {
  357. // add null byte to separate the URI from the body and avoid boundary collisions
  358. // which could lead to cache poisoning
  359. $key .= "\0".$request->getContent();
  360. }
  361. return 'md'.hash('sha256', $key);
  362. }
  363. /**
  364. * Returns a cache key for the given Request.
  365. */
  366. private function getCacheKey(Request $request): string
  367. {
  368. if (isset($this->keyCache[$request])) {
  369. return $this->keyCache[$request];
  370. }
  371. return $this->keyCache[$request] = $this->generateCacheKey($request);
  372. }
  373. /**
  374. * Persists the Request HTTP headers.
  375. */
  376. private function persistRequest(Request $request): array
  377. {
  378. return $request->headers->all();
  379. }
  380. /**
  381. * Persists the Response HTTP headers.
  382. */
  383. private function persistResponse(Response $response): array
  384. {
  385. $headers = $response->headers->all();
  386. $headers['X-Status'] = [$response->getStatusCode()];
  387. return $headers;
  388. }
  389. /**
  390. * Restores a Response from the HTTP headers and body.
  391. */
  392. private function restoreResponse(array $headers, ?string $path = null): ?Response
  393. {
  394. $status = $headers['X-Status'][0];
  395. unset($headers['X-Status']);
  396. $content = null;
  397. if (null !== $path) {
  398. $headers['X-Body-File'] = [$path];
  399. unset($headers['x-body-file']);
  400. if ($headers['X-Body-Eval'] ?? $headers['x-body-eval'] ?? false) {
  401. $content = file_get_contents($path);
  402. \assert(HttpCache::BODY_EVAL_BOUNDARY_LENGTH === 24);
  403. if (48 > \strlen($content) || substr($content, -24) !== substr($content, 0, 24)) {
  404. return null;
  405. }
  406. }
  407. }
  408. return new Response($content, $status, $headers);
  409. }
  410. }