示例#1
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;
 }
 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;
 }
示例#3
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;
 }