public function convert(AbstractTransferEvent $event)
 {
     $response = $event->getResponse();
     if ($response === null) {
         return;
     }
     $stream = $response->getBody();
     if ($stream === null) {
         // no body - nothing to convert
         return;
     }
     $headers = $response->getHeaders();
     // represent the headers as a string
     foreach ($headers as $name => $values) {
         $headers[$name] = implode("; ", $values);
     }
     $content = $stream->__toString();
     $result = $this->converter->convert($headers, $content);
     if ($result !== null) {
         $body = new Stream(fopen('php://temp', 'r+'));
         // see Guzzle 4.1.7 > GuzzleHttp\Adapter\Curl\RequestMediator::writeResponseBody
         $response->setBody($body);
         $body->write($result->getTargetContent());
         $response->setHeaders($result->getTargetHeaders());
     }
 }
Ejemplo n.º 2
0
 public function __invoke($retries, AbstractTransferEvent $event)
 {
     if (!($response = $event->getResponse())) {
         return RetrySubscriber::DEFER;
     }
     if (!$response->hasHeader('x-amz-crc32')) {
         return RetrySubscriber::DEFER;
     }
     $hash = hexdec(Utils::hash($response->getBody(), 'crc32b'));
     return (int) $response->getHeader('x-amz-crc32') !== $hash ? RetrySubscriber::RETRY : RetrySubscriber::DEFER;
 }
 /**
  * Save referer to identity before passing the event on to the evaluation function
  * @param AbstractTransferEvent $event
  */
 public function evaluate(AbstractTransferEvent $event)
 {
     $resp = $event->getResponse();
     if ($resp != null) {
         $url = $resp->getEffectiveUrl();
         if ($url !== "") {
             $identity = $this->getCurrentIdentity();
             $identity->setReferer($url);
         }
     }
     return parent::evaluate($event);
 }
Ejemplo n.º 4
0
 public function __invoke($retries, AbstractTransferEvent $event)
 {
     // Don't mess with networking errors.
     if (!($response = $event->getResponse())) {
         return RetrySubscriber::DEFER;
     }
     // Only retry 400 errors that contain the targeted exception message.
     if ($response->getStatusCode() != 400 || strpos($response->getBody(), self::ERR) === false) {
         return RetrySubscriber::DEFER;
     }
     return RetrySubscriber::RETRY;
 }
Ejemplo n.º 5
0
 public function __invoke($retries, AbstractTransferEvent $event)
 {
     // Doesn't mess with networking errors.
     if (!($response = $event->getResponse())) {
         return RetrySubscriber::DEFER;
     }
     // Only works on 4xx respsonses
     if (substr($response->getStatusCode(), 0, 1) != '4') {
         return RetrySubscriber::DEFER;
     }
     $parser = $this->exceptionParser;
     $parts = $parser($response);
     return isset(self::$throttlingExceptions[$parts['code']]) ? RetrySubscriber::RETRY : RetrySubscriber::DEFER;
 }
Ejemplo n.º 6
0
 public function onComplete(AbstractTransferEvent $event)
 {
     $request = $event->getRequest();
     $retries = (int) $request->getConfig()->get('retries');
     if ($retries >= $this->maxRetries) {
         return;
     }
     $filterFn = $this->filter;
     if (call_user_func_array($filterFn, array($retries, $event))) {
         $delayFn = $this->delayFn;
         $sleepFn = $this->sleepFn;
         call_user_func_array($sleepFn, array(call_user_func_array($delayFn, array($retries, $event)), $event));
         $request->getConfig()->set('retries', ++$retries);
         $event->intercept($event->getClient()->send($request));
     }
 }
Ejemplo n.º 7
0
 /**
  * @param TransactionInterface $transaction   Transaction that contains the request
  * @param RequestException     $e             Exception encountered
  * @param array                $transferStats Array of transfer statistics
  */
 public function __construct(TransactionInterface $transaction, RequestException $e, $transferStats = [])
 {
     parent::__construct($transaction, $transferStats);
     $this->exception = $e;
 }
 /**
  * @param AbstractTransferEvent $event
  * @throws RotatingProxySubscriberException
  * @return void
  */
 public function evaluateResult(AbstractTransferEvent $event)
 {
     $request = $event->getRequest();
     if ($event instanceof ErrorEvent) {
         $exception = $event->getException();
         if ($exception !== null) {
             do {
                 if ($exception instanceof NoProxiesLeftException) {
                     throw $exception;
                 }
                 $exception = $exception->getPrevious();
             } while ($exception !== null);
         }
     }
     $requestId = $request->getConfig()->get(self::$REQUEST_CONFIG_KEY);
     if ($requestId === null) {
         return;
         // Question: What about caches? A cached response might be served so that no proxy was used.
         // SOLUTION: simply return without exception.
         //            throw new RotatingProxySubscriberException("Config key '".self::$REQUEST_CONFIG_KEY."' not found in request config - this shouldn't happen...");
     }
     if (!array_key_exists($requestId, $this->requestId2ProxyMap)) {
         // This method really should only be called once, because it determines the result of the proxy request
         // if it's called multiple times, something is probably wrong. A possible scenario:
         // Client has a RotatingProxySubscriber and an additional function attached to the complete event that checks wether the
         // response was logically correct (e.g. contained the right json format) and throws an exception if not.
         // In that case, this method (evaluateResult) would be called twice: one time in the complete event and the next time
         // after the exception was thrown (which results in an error event being emitted).
         // SOLUTION: This can be solved by giving this RotatingProxySubscriber a lower priority so that is called last
         // Question: Does this introduce problems with ->retry() calls, e.g. is only the last call counted?
         // Answer: No - see RotatingProxySubscriberTest::test_integration_RetryingRequestsShouldIncreaseFailesAccordingly()
         $msg = "Request with id '{$requestId}' not found - it was probably already processed. Make sure not to pass on multiple events for the same request. This might be influenced by the event priority.";
         throw new RotatingProxySubscriberException($msg, $event->getRequest());
     }
     $proxy = $this->requestId2ProxyMap[$requestId];
     unset($this->requestId2ProxyMap[$requestId]);
     $proxy->requested();
     // increase request count
     if ($proxy->evaluate($event)) {
         $proxy->succeeded();
     } else {
         $proxy->failed();
     }
 }
Ejemplo n.º 9
0
 protected static function getRetryDelay($retries, AbstractTransferEvent $event, $headerName)
 {
     if (is_null($event->getResponse()) || !$event->getResponse()->hasHeader($headerName)) {
         return RetrySubscriber::exponentialDelay($retries, $event);
     }
     $retryAfter = $event->getResponse()->getHeader($headerName);
     if (is_numeric($retryAfter)) {
         if ($retryAfter < time() - strtotime('1 day', 0)) {
             return $retryAfter;
         } else {
             return $retryAfter - time();
         }
     }
     if (Utils::isValidDateTimeString($retryAfter, DATE_RFC1123)) {
         $date = \DateTime::createFromFormat(DATE_RFC1123, $retryAfter);
         return $date->getTimestamp() - time();
     }
     return RetrySubscriber::exponentialDelay($retries, $event);
 }