private function addResponseHeaders(RequestInterface $request, ResponseInterface $response)
 {
     $params = $request->getConfig();
     $lookup = $params['cache_lookup'] . ' from GuzzleCache';
     $response->addHeader('X-Cache-Lookup', $lookup);
     if ($params['cache_hit'] === true) {
         $response->addHeader('X-Cache', 'HIT from GuzzleCache');
     } elseif ($params['cache_hit'] == 'error') {
         $response->addHeader('X-Cache', 'HIT_ERROR from GuzzleCache');
     } else {
         $response->addHeader('X-Cache', 'MISS from GuzzleCache');
     }
     $freshness = Utils::getFreshness($response);
     // Only add a Warning header if we are returning a stale response.
     if ($params['cache_hit'] && $freshness !== null && $freshness <= 0) {
         $response->addHeader('Warning', sprintf('%d GuzzleCache/' . ClientInterface::VERSION . ' "%s"', 110, 'Response is stale'));
     }
 }
Exemplo n.º 2
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.º 3
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.º 4
0
 /**
  * Test that responses that can't have freshness determined return null.
  */
 public function testGetFreshnessNull()
 {
     $messageFactory = new MessageFactory();
     $response = $messageFactory->createResponse(200);
     $this->assertSame(null, Utils::getFreshness($response));
 }