Example #1
0
 /**
  * Throw a puzzle_exception_RequestException on an HTTP protocol error
  *
  * @param puzzle_event_CompleteEvent $event Emitted event
  * @throws puzzle_exception_RequestException
  */
 public function onComplete(puzzle_event_CompleteEvent $event)
 {
     $code = (string) $event->getResponse()->getStatusCode();
     // Throw an exception for an unsuccessful response
     if ($code[0] === '4' || $code[0] === '5') {
         throw puzzle_exception_RequestException::create($event->getRequest(), $event->getResponse());
     }
 }
 public function testHasValues()
 {
     $c = new puzzle_Client();
     $r = new puzzle_message_Request('GET', '/');
     $res = new puzzle_message_Response(200);
     $t = new puzzle_adapter_Transaction($c, $r);
     $e = new puzzle_event_CompleteEvent($t);
     $e->intercept($res);
     $this->assertTrue($e->isPropagationStopped());
     $this->assertSame($res, $e->getResponse());
 }
Example #3
0
 public function onComplete(puzzle_event_CompleteEvent $event)
 {
     $transferInfo = $event->getTransferInfo();
     if (array_key_exists('http_code', $transferInfo)) {
         //curl
         return;
     }
     $response = $event->getResponse();
     if (!$response->hasHeader('Transfer-Encoding')) {
         return;
     }
     $encoding = $response->getHeader('Transfer-Encoding');
     if (strcasecmp($encoding, 'chunked') !== 0) {
         return;
     }
     $body = $response->getBody()->__toString();
     $decodedBody = $this->_decode($body);
     $response->setBody(puzzle_stream_Stream::factory($decodedBody));
 }
Example #4
0
 /**
  * Called when a request receives a redirect response
  *
  * @param puzzle_event_CompleteEvent $event Event emitted
  * @throws puzzle_exception_TooManyRedirectsException
  */
 public function onComplete(puzzle_event_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') ? $redirectRequest->getConfig()->getPath('redirect/max') : 5;
     do {
         if (++$redirectCount > $max) {
             throw new puzzle_exception_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);
     }
 }
Example #5
0
 public function __callback_testThrowsUnhandledErrors(puzzle_event_CompleteEvent $e)
 {
     throw new puzzle_exception_RequestException('foo', $e->getRequest());
 }
Example #6
0
 public function __callback_testDispatchesErrorEventAndRecovers_1(puzzle_event_CompleteEvent $e)
 {
     throw new puzzle_exception_RequestException('Foo', $e->getRequest());
 }
Example #7
0
 public function onComplete(puzzle_event_CompleteEvent $event)
 {
     $this->add($event->getRequest(), $event->getResponse());
 }
Example #8
0
 public function __callback_testThrowsAndReleasesWhenErrorDuringCompleteEvent(puzzle_event_CompleteEvent $e)
 {
     throw new puzzle_exception_RequestException('foo', $e->getRequest());
 }
Example #9
0
 public function onComplete(puzzle_event_CompleteEvent $event)
 {
     $this->cookieJar->extractCookies($event->getRequest(), $event->getResponse());
 }