Exemplo n.º 1
0
 public function testHasValues()
 {
     $c = new Client();
     $r = new Request('GET', '/');
     $res = new Response(200);
     $t = new Transaction($c, $r);
     $e = new CompleteEvent($t);
     $e->intercept($res);
     $this->assertTrue($e->isPropagationStopped());
     $this->assertSame($res, $e->getResponse());
 }
 public function stripXmlNamespaces(CompleteEvent $event)
 {
     /**
      * Parsing XML with namespaces doesn't seem to work for Guzzle,
      * so using regex to remove them.
      */
     $xml = $event->getResponse()->getBody()->getContents();
     $xml = Utils::stripNamespacesFromXml($xml);
     /**
      * Intercept response and replace body with cleaned up XML
      */
     $stream = Stream::factory($xml);
     $response = $event->getResponse();
     $response->setBody($stream);
     $event->intercept($response);
 }
Exemplo n.º 3
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);
     }
 }
 private function handle304Response(RequestInterface $request, ResponseInterface $response, ResponseInterface $validated, CompleteEvent $event)
 {
     // Make sure that this response has the same ETag
     if ($validated->getHeader('ETag') !== $response->getHeader('ETag')) {
         // Revalidation failed, so remove from cache and retry.
         $this->storage->delete($request);
         $event->intercept($event->getClient()->send($request));
         return;
     }
     // Replace cached headers with any of these headers from the
     // origin server that might be more up to date
     $modified = false;
     foreach (self::$replaceHeaders as $name) {
         if ($validated->hasHeader($name) && $validated->getHeader($name) != $response->getHeader($name)) {
             $modified = true;
             $response->setHeader($name, $validated->getHeader($name));
         }
     }
     // Store the updated response in cache
     if ($modified) {
         $this->storage->cache($request, $response);
     }
 }