private function getContextFromResponse(Response $response)
 {
     $extraFields = array();
     $headersToLookFor = array('x-served-by', 'x-backend', 'x-location', 'x-varnish');
     foreach ($headersToLookFor as $headerName) {
         if ($response->hasHeader($headerName)) {
             $extraFields[$headerName] = (string) $response->getHeader($headerName);
         }
     }
     return $extraFields;
 }
 /**
  * @param string $errorName
  * @param \Guzzle\Http\Message\Response $response
  * @param int $statusCode
  * @return \Phobetor\Billomat\Exception\ExceptionInterface
  */
 public function createExceptionFromStatusCode($errorName, $response, $statusCode)
 {
     $exception = null;
     switch ($statusCode) {
         case self::STATUS_NOT_FOUND:
             return new NotFoundException($errorName, $statusCode);
         case self::STATUS_BAD_REQUEST:
             return new BadRequestException($errorName, $statusCode);
         case self::STATUS_UNAUTHORIZED:
             return new UnauthorizedException($errorName, $statusCode);
         case self::STATUS_TOO_MANY_REQUESTS:
             $exception = new TooManyRequestsException($errorName, $statusCode);
             if ($response->hasHeader('X-Rate-Limit-Remaining')) {
                 $exception->setRateLimitRemaining((int) (string) $response->getHeader('X-Rate-Limit-Remaining'));
             }
             if ($response->hasHeader('X-Rate-Limit-Reset')) {
                 $exception->setRateLimitReset((int) (string) $response->getHeader('X-Rate-Limit-Reset'));
             }
             return $exception;
         default:
             return new UnknownErrorException($errorName, $statusCode);
     }
 }
Example #3
0
 protected function handle304Response(RequestInterface $request, Response $validateResponse, Response $response)
 {
     static $replaceHeaders = array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified');
     if ($validateResponse->getEtag() != $response->getEtag()) {
         return false;
     }
     $modified = false;
     foreach ($replaceHeaders as $name) {
         if ($validateResponse->hasHeader($name)) {
             $modified = true;
             $response->setHeader($name, $validateResponse->getHeader($name));
         }
     }
     if ($modified && $this->canCache->canCacheResponse($response)) {
         $this->storage->cache($request, $response);
     }
     return true;
 }
Example #4
0
 /**
  * Check if a response in cache will satisfy the request before sending
  *
  * @param Event $event
  */
 public function onRequestBeforeSend(Event $event)
 {
     $request = $event['request'];
     if (!$this->canCache->canCacheRequest($request)) {
         return;
     }
     $hashKey = $this->keyProvider->getCacheKey($request);
     $this->cached[$request] = $hashKey;
     // If the cached data was found, then make the request into a
     // manually set request
     if ($cachedData = $this->storage->fetch($hashKey)) {
         unset($this->cached[$request]);
         $response = new Response($cachedData[0], $cachedData[1], $cachedData[2]);
         $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$response->hasHeader('X-Guzzle-Cache')) {
             $response->setHeader('X-Guzzle-Cache', "key={$hashKey}");
         }
         // Validate that the response satisfies the request
         if ($this->canResponseSatisfyRequest($request, $response)) {
             $request->setResponse($response);
         }
     }
 }
Example #5
0
 /**
  * Check if a response in cache will satisfy the request before sending
  *
  * @param Event $event
  */
 public function onRequestBeforeSend(Event $event)
 {
     $request = $event['request'];
     // This request is being prepared
     $key = spl_object_hash($request);
     $hashKey = $this->getCacheKey($request);
     $this->cached[$key] = $hashKey;
     $cachedData = $this->getCacheAdapter()->fetch($hashKey);
     // If the cached data was found, then make the request into a
     // manually set request
     if ($cachedData) {
         if ($this->serialize) {
             $cachedData = unserialize($cachedData);
         }
         unset($this->cached[$key]);
         $response = new Response($cachedData['c'], $cachedData['h'], $cachedData['b']);
         $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$response->hasHeader('X-Guzzle-Cache')) {
             $response->setHeader('X-Guzzle-Cache', "key={$key}");
         }
         // Validate that the response satisfies the request
         if ($this->canResponseSatisfyRequest($request, $response)) {
             $request->setResponse($response);
         }
     }
 }
 /**
  * Handle a 304 response and ensure that it is still valid
  *
  * @param RequestInterface $request          Request that was sent
  * @param Response         $validateResponse Response received
  * @param Response         $response         Original cached response
  *
  * @return bool Returns true if valid, false if invalid
  */
 protected function handle304Response(RequestInterface $request, Response $validateResponse, Response $response)
 {
     static $replaceHeaders = array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified');
     // Make sure that this response has the same ETag
     if ($validateResponse->getEtag() != $response->getEtag()) {
         return false;
     }
     // Replace cached headers with any of these headers from the
     // origin server that might be more up to date
     $modified = false;
     foreach ($replaceHeaders as $name) {
         if ($validateResponse->hasHeader($name)) {
             $modified = true;
             $response->setHeader($name, $validateResponse->getHeader($name));
         }
     }
     // Store the updated response in cache
     if ($modified && $this->canCache->canCacheResponse($response)) {
         $this->storage->cache($request, $response);
     }
     return true;
 }
 /**
  * Add the plugin's headers to a response
  *
  * @param string           $cacheKey Cache key
  * @param RequestInterface $request  Request
  * @param Response         $response Response to add headers to
  */
 protected function addResponseHeaders($cacheKey, RequestInterface $request, Response $response)
 {
     if (!$response->hasHeader('X-Guzzle-Cache')) {
         $response->setHeader('X-Guzzle-Cache', "key={$cacheKey}");
     }
     $response->addHeader('Via', sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION));
     if ($this->debugHeaders) {
         if ($request->getParams()->get('cache.lookup') === true) {
             $response->addHeader('X-Cache-Lookup', 'HIT from GuzzleCache');
         } else {
             $response->addHeader('X-Cache-Lookup', 'MISS from GuzzleCache');
         }
         if ($request->getParams()->get('cache.hit') === true) {
             $response->addHeader('X-Cache', 'HIT from GuzzleCache');
         } elseif ($request->getParams()->get('cache.hit') === 'error') {
             $response->addHeader('X-Cache', 'HIT_ERROR from GuzzleCache');
         } else {
             $response->addHeader('X-Cache', 'MISS from GuzzleCache');
         }
     }
     if ($response->isFresh() === false) {
         $response->addHeader('Warning', sprintf('110 GuzzleCache/%s "Response is stale"', Version::VERSION));
         if ($request->getParams()->get('cache.hit') === 'error') {
             $response->addHeader('Warning', sprintf('111 GuzzleCache/%s "Revalidation failed"', Version::VERSION));
         }
     }
 }
 private function parseResponse(Response $response, $path)
 {
     $statusCode = $response->getStatusCode();
     if ($statusCode !== 200) {
         throw new UnexpectedValueException(sprintf('Expected status code 200 from "%s", got %d', $path, $statusCode));
     }
     $contentType = $response->hasHeader('content-type') ? $response->getContentType() : '';
     if (substr($contentType, 0, 10) !== 'text/plain') {
         throw new UnexpectedValueException(sprintf('Expected content type "text/plain" from "%s", got "%s"', $path, $contentType));
     }
     return $this->parseRequestFromResponse($response, $path);
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function hasHeader($header)
 {
     return $this->response->hasHeader($header);
 }
 /**
  * Check if a response in cache will satisfy the request before sending
  *
  * @param Event $event
  */
 public function onRequestBeforeSend(Event $event)
 {
     $request = $event['request'];
     // Only cache cacheable requests
     if ($cacheFilter = $request->getParams()->get('cache.filter_strategy')) {
         if (!call_user_func($cacheFilter, $request)) {
             return;
         }
     } elseif (!$request->canCache()) {
         return;
     }
     $hashKey = $this->getCacheKey($request);
     $this->cached[$request] = $hashKey;
     $cachedData = $this->adapter->fetch($hashKey);
     // If the cached data was found, then make the request into a
     // manually set request
     if ($cachedData) {
         unset($this->cached[$request]);
         $response = new Response($cachedData[0], $cachedData[1], $cachedData[2]);
         $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$response->hasHeader('X-Guzzle-Cache')) {
             $response->setHeader('X-Guzzle-Cache', "key={$hashKey}");
         }
         // Validate that the response satisfies the request
         if ($this->canResponseSatisfyRequest($request, $response)) {
             $request->setResponse($response);
         }
     }
 }