public function onComplete(CompleteEvent $e)
 {
     $lookup = $e->getRequest()->getConfig()->get('cache_lookup');
     if ($lookup == 'HIT' && $this->shouldvalidate($e->getRequest(), $e->getResponse())) {
         $this->validate($e->getRequest(), $e->getResponse(), $e);
     }
 }
Beispiel #2
0
 /**
  * Invoked for every HTTP error.
  *
  * @param CompleteEvent $event
  *
  * @throws BadResponseError
  */
 public function onComplete(CompleteEvent $event)
 {
     $response = $event->getResponse();
     if ($response->getStatusCode() >= 400) {
         throw $this->httpError($event->getRequest(), $response);
     }
 }
 public function onComplete(CompleteEvent $event)
 {
     if (in_array($event->getRequest()->getPath(), $this->paths)) {
         $response = $event->getResponse();
         $response->setHeaders(['Cache-Control' => sprintf('max-age=%d', $this->maxAge)]);
     }
 }
 /**
  * Throw a RequestException if the response is not marked as successful.
  *
  * @param \GuzzleHttp\Event\CompleteEvent $event
  *
  * @throws \GuzzleHttp\Exception\RequestException
  *
  * @return void
  */
 public function onComplete(CompleteEvent $event)
 {
     $json = $event->getResponse()->json();
     if (array_get($json, 'result') !== 'success' || array_key_exists('response', $json) === false) {
         throw RequestException::create($event->getRequest(), $event->getResponse());
     }
 }
Beispiel #5
0
 public function onComplete(CompleteEvent $event)
 {
     $request = $event->getRequest();
     $response = $event->getResponse();
     if (!$this->cache->contains($request->__toString())) {
         $this->cache->save($request->__toString(), $response, 1000);
     }
 }
 public function onComplete(CompleteEvent $event)
 {
     $request = $event->getRequest();
     $response = $event->getResponse();
     if ($request->getConfig()->get('cache_lookup') === 'MISS' && $this->canCacheRequest($request) && $this->canCacheResponse($response)) {
         $this->storage->save($request, $response);
     }
 }
Beispiel #7
0
 /**
  * Throw a RequestException on an HTTP protocol error
  *
  * @param CompleteEvent $event Emitted event
  * @throws RequestException
  */
 public function onComplete(CompleteEvent $event)
 {
     $code = (string) $event->getResponse()->getStatusCode();
     // Throw an exception for an unsuccessful response
     if ($code[0] === '4' || $code[0] === '5') {
         throw RequestException::create($event->getRequest(), $event->getResponse());
     }
 }
 public function onComplete(CompleteEvent $event)
 {
     $request = $event->getRequest();
     if (!file_exists($this->getPath($request))) {
         mkdir($this->getPath($request), 0777, true);
     }
     $response = $event->getResponse();
     file_put_contents($this->getFullFilePath($request), (string) $response);
 }
 /**
  * When a request completes, this method is executed. Because this class
  * checks for HTTP errors and handles them, this method checks the HTTP
  * status code and invokes {@see RequestException} if necessary.
  *
  * @param CompleteEvent $event
  * @throws \OpenStack\Common\Transport\Exception\RequestException
  */
 public function onComplete(CompleteEvent $event)
 {
     $status = (int) $event->getResponse()->getStatusCode();
     // Has an error occurred (4xx or 5xx status)?
     if ($status >= 400 && $status <= 505) {
         $request = new RequestAdapter($event->getRequest());
         $response = new ResponseAdapter($event->getResponse());
         throw RequestException::create($request, $response);
     }
 }
 public function onComplete(CompleteEvent $event)
 {
     $response = $event->getResponse();
     if (!($expected = $this->getExpected($response))) {
         return;
     }
     $request = $event->getRequest();
     $response->setBody(new ReadIntegrityStream($response->getBody(), $this->hash, $expected, function ($result, $expected) use($request, $response) {
         throw new MessageIntegrityException(sprintf('Message integrity check failure. Expected ' . '"%s" but got "%s"', $expected, $result), $request, $response);
     }));
 }
 private function matchesHash(CompleteEvent $event, $hash, StreamInterface $body)
 {
     $body->seek(0);
     while (!$body->eof()) {
         $this->hash->update($body->read(16384));
     }
     $result = $this->hash->complete();
     if ($hash !== $result) {
         throw new MessageIntegrityException(sprintf('Message integrity check failure. Expected "%s" but' . ' got "%s"', $hash, $result), $event->getRequest(), $event->getResponse());
     }
 }
 function let(SiteConfigBuilder $siteConfigBuilder, SiteConfig $siteConfig, Factory $authenticatorFactory, ClientInterface $guzzle, Emitter $emitter, BeforeEvent $beforeEvent, CompleteEvent $completeEvent, RequestInterface $request, ResponseInterface $response, Factory $authenticatorFactory)
 {
     $siteConfig->getHost()->willReturn('example.com');
     $siteConfigBuilder->buildForHost('example.com')->willReturn($siteConfig);
     $guzzle->getEmitter()->willReturn($emitter);
     $request->getHost()->willReturn('example.com');
     $beforeEvent->getRequest()->willReturn($request);
     $beforeEvent->getClient()->willReturn($guzzle);
     $response->getBody()->willReturn('<html></html>');
     $completeEvent->getResponse()->willReturn($response);
     $completeEvent->getRequest()->willReturn($request);
     $completeEvent->getClient()->willReturn($guzzle);
     $this->beConstructedWith($siteConfigBuilder, $authenticatorFactory);
 }
 public function loginIfRequested(CompleteEvent $event)
 {
     if (($config = $this->buildSiteConfig($event->getRequest())) === false) {
         return;
     }
     if (!$config->requiresLogin()) {
         return;
     }
     $authenticator = $this->authenticatorFactory->buildFromSiteConfig($config);
     if ($authenticator->isLoginRequired($event->getResponse()->getBody())) {
         $client = $event->getClient();
         $emitter = $client->getEmitter();
         $emitter->detach($this);
         $authenticator->login($client);
         $emitter->attach($this);
         $event->retry();
     }
 }
 public function cacheQuery(CompleteEvent $event, $name)
 {
     if (!$this->cache) {
         return;
     }
     $request = $event->getRequest();
     $content = (string) $event->getResponse()->getBody();
     //if content is empty, do not proceed with caching
     if (count($content) < 1 or $content == '' or $content == NULL) {
         return;
     }
     if ($request->getMethod() == 'GET' and $event->getResponse()->getStatusCode() == 200) {
         if (!$this->store->has($this->_cache_name)) {
             \Log::info('storing response in cache');
             $this->store->put($this->_cache_name, $content, $this->cache_ttl);
         }
     }
 }
Beispiel #15
0
 public function cacheQuery(CompleteEvent $event, $name)
 {
     $request = $event->getRequest();
     $content = Base::parse($event->getResponse());
     //if content is empty, do not proceed with caching
     if (count($content) < 1 or $content == '' or $content == NULL) {
         return;
     }
     if ($request->getMethod() == 'GET' and $event->getResponse()->getStatusCode() == 200) {
         $url = Base::getBaseUrl($request->getUrl());
         if (!preg_match('/token|oauth/', $url)) {
             $ttl = $this->params['cache_ttl'];
             if (!$this->store->has($this->_cache_name)) {
                 \Log::info('storing response in cache');
                 $this->store->put($this->_cache_name, $content, $ttl);
             }
         }
     }
 }
Beispiel #16
0
 /**
  * Called when a request receives a redirect response
  *
  * @param CompleteEvent $event Event emitted
  * @throws TooManyRedirectsException
  */
 public function onComplete(CompleteEvent $event)
 {
     $response = $event->getResponse();
     if (substr($response->getStatusCode(), 0, 1) != '3' || !$response->hasHeader('Location')) {
         return;
     }
     $redirectCount = 0;
     $redirectRequest = $event->getRequest();
     $redirectResponse = $response;
     $max = $redirectRequest->getConfig()->getPath('redirect/max') ?: 5;
     do {
         if (++$redirectCount > $max) {
             throw new TooManyRedirectsException("Will not follow more than {$redirectCount} redirects", $redirectRequest);
         }
         $redirectRequest = $this->createRedirectRequest($redirectRequest, $redirectResponse);
         $redirectResponse = $event->getClient()->send($redirectRequest);
     } while (substr($redirectResponse->getStatusCode(), 0, 1) == '3' && $redirectResponse->hasHeader('Location'));
     if ($redirectResponse !== $response) {
         $event->intercept($redirectResponse);
     }
 }
Beispiel #17
0
 /**
  * Called when a request receives a redirect response
  *
  * @param CompleteEvent $event Event emitted
  * @throws TooManyRedirectsException
  */
 public function onComplete(CompleteEvent $event)
 {
     $response = $event->getResponse();
     if (substr($response->getStatusCode(), 0, 1) != '3' || !$response->hasHeader('Location')) {
         return;
     }
     $request = $event->getRequest();
     $config = $request->getConfig();
     // Increment the redirect and initialize the redirect state.
     if ($redirectCount = $config['redirect_count']) {
         $config['redirect_count'] = ++$redirectCount;
     } else {
         $config['redirect_scheme'] = $request->getScheme();
         $config['redirect_count'] = $redirectCount = 1;
     }
     $max = $config->getPath('redirect/max') ?: 5;
     if ($redirectCount > $max) {
         throw new TooManyRedirectsException("Will not follow more than {$redirectCount} redirects", $request);
     }
     $this->modifyRedirectRequest($request, $response);
     $event->retry();
 }
 /**
  * 
  * @param CompleteEvent $event
  */
 public function onComplete(CompleteEvent $event)
 {
     $this->logger->log(substr($event->getResponse()->getStatusCode(), 0, 1) == '2' ? LogLevel::INFO : LogLevel::WARNING, $this->formatter->format($event->getRequest(), $event->getResponse()), ['request' => $event->getRequest(), 'response' => $event->getResponse()]);
     $this->rewindResponse($event->getResponse());
 }
Beispiel #19
0
 public function onComplete(CompleteEvent $event)
 {
     $this->add($event->getRequest(), $event->getResponse());
 }
 /**
  * Checks if the request and response can be cached, and if so, store it.
  *
  * @param CompleteEvent $event
  */
 public function onComplete(CompleteEvent $event)
 {
     $request = $event->getRequest();
     $response = $event->getResponse();
     // Cache the response if it can be cached and isn't already
     if ($request->getConfig()->get('cache_lookup') === 'MISS' && call_user_func($this->canCache, $request) && Utils::canCacheResponse($response)) {
         // Store the date when the response was cached
         $response->setHeader('X-Guzzle-Cache-Date', gmdate('D, d M Y H:i:s T', time()));
         $this->storage->cache($request, $response);
     }
     $this->addResponseHeaders($request, $response);
 }
 /**
  * Checks if the request and response can be cached, and if so, store it
  */
 public function onComplete(CompleteEvent $event)
 {
     $request = $event->getRequest();
     $response = $event->getResponse();
     // Cache the response if it can be cached and isn't already
     if ($request->getConfig()->get('cache_lookup') === 'MISS' && call_user_func($this->canCache, $request) && Utils::canCacheResponse($response)) {
         $this->storage->cache($request, $response);
     }
     $this->addResponseHeaders($request, $response);
 }
 public function onComplete(CompleteEvent $event)
 {
     $request = $event->getRequest();
     $stopwatchEvent = $this->stopwatch->stop($request->getUrl());
     $this->add($request, $event->getResponse(), $stopwatchEvent);
 }
Beispiel #23
0
 public function onComplete(CompleteEvent $event)
 {
     $this->logger->log(LogLevel::INFO, $this->formatter->format($event->getRequest(), $event->getResponse()), ['request' => $event->getRequest(), 'response' => $event->getResponse()]);
 }
Beispiel #24
0
 public function onComplete(CompleteEvent $event)
 {
     $this->cookieJar->extractCookies($event->getRequest(), $event->getResponse());
 }
 /**
  * Method to handle complete events.
  *
  * @param CompleteEvent $event
  */
 public function onComplete(CompleteEvent $event)
 {
     // Stop measurement.
     $this->stopMeasure($event->getRequest(), $event->getResponse());
 }