private function shouldValidate(RequestInterface $request, ResponseInterface $response)
 {
     if ($request->getMethod() != 'GET' || $request->getConfig()->get('cache.disable')) {
         return false;
     }
     $validate = Utils::getDirective($request, 'Pragma') === 'no-cache' || Utils::getDirective($response, 'Pragma') === 'no-cache' || Utils::getDirective($request, 'must-revalidate') || Utils::getDirective($response, 'must-revalidate') || Utils::getDirective($request, 'no-cache') || Utils::getDirective($response, 'no-cache') || Utils::getDirective($response, 'max-age') === '0' || Utils::getDirective($response, 's-maxage') === '0';
     // Use the strong ETag validator if available and the response contains
     // no Cache-Control directive
     if (!$validate && !$response->hasHeader('Cache-Control') && $response->hasHeader('ETag')) {
         $validate = true;
     }
     return $validate;
 }
Exemplo n.º 2
0
 private function getTtl(ResponseInterface $response)
 {
     $ttl = 0;
     if ($cacheControl = $response->getHeader('Cache-Control')) {
         $stale = Utils::getDirective($response, 'stale-if-error');
         $ttl += $stale == true ? $ttl : $stale;
         $ttl += Utils::getDirective($response, 'max-age');
     }
     return $ttl ?: $this->defaultTtl;
 }
 private function validateFailed(RequestInterface $request, ResponseInterface $response)
 {
     $req = Utils::getDirective($request, 'stale-if-error');
     $res = Utils::getDirective($response, 'stale-if-error');
     if (!$req && !$res) {
         return false;
     }
     $responseAge = Utils::getResponseAge($response);
     $maxAge = Utils::getMaxAge($response);
     if ($req && $responseAge - $maxAge > $req || $responseAge - $maxAge > $res) {
         return false;
     }
     return true;
 }
 /**
  * Return the TTL to use when caching a Response (extracted from guzzle/cache-subscriber).
  *
  * @param ResponseInterface $response Guzzle 4/5 response message.
  *
  * @link https://github.com/guzzle/cache-subscriber/blob/master/src/CacheStorage.php
  *
  * @return int
  */
 private function getTtl(ResponseInterface $response)
 {
     $ttl = 0;
     if ($response->getHeader('Cache-Control')) {
         return $ttl;
     }
     $maxAge = Utils::getDirective($response, 'max-age');
     if (is_numeric($maxAge)) {
         $ttl += $maxAge;
     }
     // According to RFC5861 stale headers are *in addition* to any
     // max-age values.
     $stale = Utils::getDirective($response, 'stale-if-error');
     if (is_numeric($stale)) {
         $ttl += $stale;
     }
     return $ttl;
 }
Exemplo n.º 5
0
 public static function isResponseValid(RequestInterface $request, ResponseInterface $response)
 {
     $responseAge = Utils::getResponseAge($response);
     $maxAge = Utils::getDirective($response, 'max-age');
     // Increment the age based on the X-Guzzle-Cache-Date
     if ($cacheDate = $response->getHeader('X-Guzzle-Cache-Date')) {
         $responseAge += time() - strtotime($cacheDate);
         $response->setHeader('Age', $responseAge);
     }
     // Check the request's max-age header against the age of the response
     if ($maxAge !== null && $responseAge > $maxAge) {
         return false;
     }
     // Check the response's max-age header against the freshness level
     $freshness = Utils::getFreshness($response);
     if ($freshness !== null) {
         $maxStale = Utils::getDirective($request, 'max-stale');
         if ($maxStale !== null) {
             if ($freshness < -1 * $maxStale) {
                 return false;
             }
         } elseif ($maxAge !== null && $responseAge > $maxAge) {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 6
0
 public static function isResponseValid(RequestInterface $request, ResponseInterface $response)
 {
     $responseAge = Utils::getResponseAge($response);
     $maxAge = Utils::getDirective($response, 'max-age');
     // Check the request's max-age header against the age of the response
     if ($maxAge !== null && $responseAge > $maxAge) {
         return false;
     }
     // Check the response's max-age header against the freshness level
     $freshness = Utils::getFreshness($response);
     if ($freshness !== null) {
         $maxStale = Utils::getDirective($request, 'max-stale');
         if ($maxStale !== null) {
             if ($freshness < -1 * $maxStale) {
                 return false;
             }
         } elseif ($maxAge !== null && $responseAge > $maxAge) {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 7
0
 /**
  * Return the TTL to use when caching a Response.
  *
  * @param ResponseInterface $response The response being cached.
  *
  * @return int The TTL in seconds.
  */
 private function getTtl(ResponseInterface $response)
 {
     $ttl = 0;
     if ($cacheControl = $response->getHeader('Cache-Control')) {
         $maxAge = Utils::getDirective($response, 'max-age');
         if (is_numeric($maxAge)) {
             $ttl += $maxAge;
         }
         // According to RFC5861 stale headers are *in addition* to any
         // max-age values.
         $stale = Utils::getDirective($response, 'stale-if-error');
         if (is_numeric($stale)) {
             $ttl += $stale;
         }
     } elseif ($expires = $response->getHeader('Expires')) {
         $ttl += strtotime($expires) - time();
     }
     return $ttl ?: $this->defaultTtl;
 }