Store.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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, 0777, 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), 0777, 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 = $response->headers->get('vary');
  176. foreach ($this->getMetadata($key) as $entry) {
  177. if (!isset($entry[1]['vary'][0])) {
  178. $entry[1]['vary'] = [''];
  179. }
  180. if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
  181. $entries[] = $entry;
  182. }
  183. }
  184. $headers = $this->persistResponse($response);
  185. unset($headers['age']);
  186. foreach ($this->options['private_headers'] as $h) {
  187. unset($headers[strtolower($h)]);
  188. }
  189. array_unshift($entries, [$storedEnv, $headers]);
  190. if (!$this->save($key, serialize($entries))) {
  191. throw new \RuntimeException('Unable to store the metadata.');
  192. }
  193. return $key;
  194. }
  195. /**
  196. * Returns content digest for $response.
  197. */
  198. protected function generateContentDigest(Response $response): string
  199. {
  200. return 'en'.hash('xxh128', $response->getContent());
  201. }
  202. /**
  203. * Invalidates all cache entries that match the request.
  204. *
  205. * @throws \RuntimeException
  206. */
  207. public function invalidate(Request $request): void
  208. {
  209. $modified = false;
  210. $key = $this->getCacheKey($request);
  211. $entries = [];
  212. foreach ($this->getMetadata($key) as $entry) {
  213. $response = $this->restoreResponse($entry[1]);
  214. if ($response->isFresh()) {
  215. $response->expire();
  216. $modified = true;
  217. $entries[] = [$entry[0], $this->persistResponse($response)];
  218. } else {
  219. $entries[] = $entry;
  220. }
  221. }
  222. if ($modified && !$this->save($key, serialize($entries))) {
  223. throw new \RuntimeException('Unable to store the metadata.');
  224. }
  225. }
  226. /**
  227. * Determines whether two Request HTTP header sets are non-varying based on
  228. * the vary response header value provided.
  229. *
  230. * @param string|null $vary A Response vary header
  231. * @param array $env1 A Request HTTP header array
  232. * @param array $env2 A Request HTTP header array
  233. */
  234. private function requestsMatch(?string $vary, array $env1, array $env2): bool
  235. {
  236. if (!$vary) {
  237. return true;
  238. }
  239. foreach (preg_split('/[\s,]+/', $vary) as $header) {
  240. $key = str_replace('_', '-', strtolower($header));
  241. $v1 = $env1[$key] ?? null;
  242. $v2 = $env2[$key] ?? null;
  243. if ($v1 !== $v2) {
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. /**
  250. * Gets all data associated with the given key.
  251. *
  252. * Use this method only if you know what you are doing.
  253. */
  254. private function getMetadata(string $key): array
  255. {
  256. if (!$entries = $this->load($key)) {
  257. return [];
  258. }
  259. return unserialize($entries) ?: [];
  260. }
  261. /**
  262. * Purges data for the given URL.
  263. *
  264. * This method purges both the HTTP and the HTTPS version of the cache entry.
  265. *
  266. * @return bool true if the URL exists with either HTTP or HTTPS scheme and has been purged, false otherwise
  267. */
  268. public function purge(string $url): bool
  269. {
  270. $http = preg_replace('#^https:#', 'http:', $url);
  271. $https = preg_replace('#^http:#', 'https:', $url);
  272. $purgedHttp = $this->doPurge($http);
  273. $purgedHttps = $this->doPurge($https);
  274. return $purgedHttp || $purgedHttps;
  275. }
  276. /**
  277. * Purges data for the given URL.
  278. */
  279. private function doPurge(string $url): bool
  280. {
  281. $key = $this->getCacheKey(Request::create($url));
  282. if (isset($this->locks[$key])) {
  283. flock($this->locks[$key], \LOCK_UN);
  284. fclose($this->locks[$key]);
  285. unset($this->locks[$key]);
  286. }
  287. if (is_file($path = $this->getPath($key))) {
  288. unlink($path);
  289. return true;
  290. }
  291. return false;
  292. }
  293. /**
  294. * Loads data for the given key.
  295. */
  296. private function load(string $key): ?string
  297. {
  298. $path = $this->getPath($key);
  299. return is_file($path) && false !== ($contents = @file_get_contents($path)) ? $contents : null;
  300. }
  301. /**
  302. * Save data for the given key.
  303. */
  304. private function save(string $key, string $data, bool $overwrite = true): bool
  305. {
  306. $path = $this->getPath($key);
  307. if (!$overwrite && file_exists($path)) {
  308. return true;
  309. }
  310. if (isset($this->locks[$key])) {
  311. $fp = $this->locks[$key];
  312. @ftruncate($fp, 0);
  313. @fseek($fp, 0);
  314. $len = @fwrite($fp, $data);
  315. if (\strlen($data) !== $len) {
  316. @ftruncate($fp, 0);
  317. return false;
  318. }
  319. } else {
  320. if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
  321. return false;
  322. }
  323. $tmpFile = tempnam(\dirname($path), basename($path));
  324. if (false === $fp = @fopen($tmpFile, 'w')) {
  325. @unlink($tmpFile);
  326. return false;
  327. }
  328. @fwrite($fp, $data);
  329. @fclose($fp);
  330. if ($data != file_get_contents($tmpFile)) {
  331. @unlink($tmpFile);
  332. return false;
  333. }
  334. if (false === @rename($tmpFile, $path)) {
  335. @unlink($tmpFile);
  336. return false;
  337. }
  338. }
  339. @chmod($path, 0666 & ~umask());
  340. return true;
  341. }
  342. public function getPath(string $key): string
  343. {
  344. 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);
  345. }
  346. /**
  347. * Generates a cache key for the given Request.
  348. *
  349. * This method should return a key that must only depend on a
  350. * normalized version of the request URI.
  351. *
  352. * If the same URI can have more than one representation, based on some
  353. * headers, use a Vary header to indicate them, and each representation will
  354. * be stored independently under the same cache key.
  355. */
  356. protected function generateCacheKey(Request $request): string
  357. {
  358. return 'md'.hash('sha256', $request->getUri());
  359. }
  360. /**
  361. * Returns a cache key for the given Request.
  362. */
  363. private function getCacheKey(Request $request): string
  364. {
  365. if (isset($this->keyCache[$request])) {
  366. return $this->keyCache[$request];
  367. }
  368. return $this->keyCache[$request] = $this->generateCacheKey($request);
  369. }
  370. /**
  371. * Persists the Request HTTP headers.
  372. */
  373. private function persistRequest(Request $request): array
  374. {
  375. return $request->headers->all();
  376. }
  377. /**
  378. * Persists the Response HTTP headers.
  379. */
  380. private function persistResponse(Response $response): array
  381. {
  382. $headers = $response->headers->all();
  383. $headers['X-Status'] = [$response->getStatusCode()];
  384. return $headers;
  385. }
  386. /**
  387. * Restores a Response from the HTTP headers and body.
  388. */
  389. private function restoreResponse(array $headers, ?string $path = null): ?Response
  390. {
  391. $status = $headers['X-Status'][0];
  392. unset($headers['X-Status']);
  393. $content = null;
  394. if (null !== $path) {
  395. $headers['X-Body-File'] = [$path];
  396. unset($headers['x-body-file']);
  397. if ($headers['X-Body-Eval'] ?? $headers['x-body-eval'] ?? false) {
  398. $content = file_get_contents($path);
  399. \assert(HttpCache::BODY_EVAL_BOUNDARY_LENGTH === 24);
  400. if (48 > \strlen($content) || substr($content, -24) !== substr($content, 0, 24)) {
  401. return null;
  402. }
  403. }
  404. }
  405. return new Response($content, $status, $headers);
  406. }
  407. }