Example #1
0
 /**
  * Check if a cache response satisfies a request's caching constraints
  *
  * @param RequestInterface $request  Request to validate
  * @param Response         $response Response to validate
  *
  * @return bool
  */
 public function canResponseSatisfyRequest(RequestInterface $request, Response $response)
 {
     $responseAge = $response->calculateAge();
     $reqc = $request->getHeader('Cache-Control');
     $resc = $response->getHeader('Cache-Control');
     // Check the request's max-age header against the age of the response
     if ($reqc && $reqc->hasDirective('max-age') && $responseAge > $reqc->getDirective('max-age')) {
         return false;
     }
     // Check the response's max-age header
     if ($response->isFresh() === false) {
         $maxStale = $reqc ? $reqc->getDirective('max-stale') : null;
         if (null !== $maxStale) {
             if ($maxStale !== true && $response->getFreshness() < -1 * $maxStale) {
                 return false;
             }
         } elseif ($resc && $resc->hasDirective('max-age') && $responseAge > $resc->getDirective('max-age')) {
             return false;
         }
     }
     if ($this->revalidation->shouldRevalidate($request, $response)) {
         try {
             return $this->revalidation->revalidate($request, $response);
         } catch (CurlException $e) {
             $request->getParams()->set('cache.hit', 'error');
             return $this->canResponseSatisfyFailedRequest($request, $response);
         }
     }
     return true;
 }
 /**
  * Check if a cache response satisfies a request's caching constraints
  *
  * @param RequestInterface $request  Request to validate
  * @param Response         $response Response to validate
  *
  * @return bool
  */
 public function canResponseSatisfyRequest(RequestInterface $request, Response $response)
 {
     $responseAge = $response->calculateAge();
     $reqc = $request->getHeader('Cache-Control');
     $resc = $response->getHeader('Cache-Control');
     // Check the request's max-age header against the age of the response
     if ($reqc && $reqc->hasDirective('max-age') && $responseAge > $reqc->getDirective('max-age')) {
         return false;
     }
     // Check the response's max-age header
     if ($response->isFresh() === false) {
         $maxStale = $reqc ? $reqc->getDirective('max-stale') : null;
         if (null !== $maxStale) {
             if ($maxStale !== true && $response->getFreshness() < -1 * $maxStale) {
                 return false;
             }
         } elseif ($resc && $resc->hasDirective('max-age') && $responseAge > $resc->getDirective('max-age')) {
             return false;
         }
     }
     // Only revalidate GET requests
     if ($request->getMethod() == RequestInterface::GET) {
         // Check if the response must be validated against the origin server
         if ($request->getHeader('Pragma') == 'no-cache' || $reqc && ($reqc->hasDirective('no-cache') || $reqc->hasDirective('must-revalidate')) || $resc && ($resc->hasDirective('no-cache') || $resc->hasDirective('must-revalidate'))) {
             // no-cache: When no parameters are present, always revalidate
             // When parameters are present in no-cache and the request includes those same parameters, then the
             // response must re-validate. I'll need an example of what fields look like in order to implement a
             // smarter version of no-cache
             // Requests can decline to revalidate against the origin server by setting the cache.revalidate param:
             // - never - To never revalidate and always contact the origin server
             // - skip  - To skip revalidation and just use what is in cache
             switch ($request->getParams()->get('cache.revalidate')) {
                 case 'never':
                     return false;
                 case 'skip':
                     return true;
                 default:
                     return $this->revalidation->revalidate($request, $response);
             }
         }
     }
     return true;
 }