public function revalidate(RequestInterface $request, Response $response)
 {
     try {
         $revalidate = $this->createRevalidationRequest($request, $response);
         $validateResponse = $revalidate->send();
         if ($validateResponse->getStatusCode() == 200) {
             return $this->handle200Response($request, $validateResponse);
         } elseif ($validateResponse->getStatusCode() == 304) {
             return $this->handle304Response($request, $validateResponse, $response);
         }
     } catch (CurlException $e) {
         return $this->plugin->canResponseSatisfyFailedRequest($request, $response);
     } catch (BadResponseException $e) {
         $this->handleBadResponse($e);
     }
     // Other exceptions encountered in the revalidation request are ignored
     // in hopes that sending a request to the origin server will fix it
     return false;
 }
Esempio n. 2
0
 /**
  * This is a very hacked way to listen for a cURL timeout.
  * and serve a cached response from stale cache.
  *
  * The Guzzle\Plugin\Cache\CachePlugin::onBeforeRequestSend event listener
  * method is called manually to allow the plugin to set the cached response.
  *
  * @param Event $event
  */
 public function onRequestException(Event $event)
 {
     $exception = $event['exception'];
     /** @var \Guzzle\Http\Message\Request $request */
     $request =& $event['request'];
     if (!$request) {
         return;
     }
     $cacheControl = $request->getHeader('Cache-Control');
     // Make sure the response can satisfy a request manually before proceeding.
     if ($exception instanceof CurlException && $exception->getErrorNo() === CURLE_OPERATION_TIMEOUTED && $this->guzzleCachePlugin->canResponseSatisfyFailedRequest($event['request'], new Response(408))) {
         if ($cacheControl && $cacheControl->hasDirective('max-age') && $cacheControl->hasDirective('stale-if-error')) {
             // Hack the max-age so the CachePlugin will accept the request as cacheable.
             $cacheControl->addDirective('max-age', $this->getConfig('stale-if-error'));
             $this->guzzleCachePlugin->onRequestBeforeSend(new Event(array('request' => $request)));
         }
     }
 }
 /**
  * @dataProvider satisfyFailedProvider
  */
 public function testChecksIfResponseCanSatisfyFailedRequest($request, $response, $can)
 {
     $plugin = new CachePlugin();
     $this->assertEquals($can, $plugin->canResponseSatisfyFailedRequest($request, $response));
 }