vendor/symfony/http-kernel/HttpCache/HttpCache.php line 332

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