Store.php 14 KB

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