Add max redirections limit
This commit is contained in:
parent
6bbcb21fc9
commit
c8bc9533e5
@ -41,6 +41,8 @@ abstract class Client
|
|||||||
protected $insulated;
|
protected $insulated;
|
||||||
protected $redirect;
|
protected $redirect;
|
||||||
protected $followRedirects;
|
protected $followRedirects;
|
||||||
|
protected $maxRedirects = -1;
|
||||||
|
protected $redirectCount = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -72,6 +74,16 @@ abstract class Client
|
|||||||
$this->followRedirects = (Boolean) $followRedirect;
|
$this->followRedirects = (Boolean) $followRedirect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the maximum number of requests that crawler can follow.
|
||||||
|
*
|
||||||
|
* @param integer $maxRedirects
|
||||||
|
*/
|
||||||
|
public function setMaxRedirects($maxRedirects)
|
||||||
|
{
|
||||||
|
$this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the insulated flag.
|
* Sets the insulated flag.
|
||||||
*
|
*
|
||||||
@ -456,6 +468,14 @@ abstract class Client
|
|||||||
throw new \LogicException('The request was not redirected.');
|
throw new \LogicException('The request was not redirected.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (-1 !== $this->maxRedirects) {
|
||||||
|
if ($this->redirectCount === $this->maxRedirects) {
|
||||||
|
throw new \LogicException(sprintf('The maximum number (%d) of redirections was reached.', $this->maxRedirects));
|
||||||
|
}
|
||||||
|
|
||||||
|
++$this->redirectCount;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->request('get', $this->redirect);
|
return $this->request('get', $this->redirect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,6 +350,25 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
|
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFollowRedirectWithMaxRedirects()
|
||||||
|
{
|
||||||
|
$client = new TestClient();
|
||||||
|
$client->setMaxRedirects(1);
|
||||||
|
$client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
|
||||||
|
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||||
|
|
||||||
|
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
|
||||||
|
|
||||||
|
$client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected2')));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$client->followRedirect();
|
||||||
|
$this->fail('->followRedirect() throws a \LogicException if the request was redirected and limit of redirections were reached');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->assertInstanceof('LogicException', $e, '->followRedirect() throws a \LogicException if the request was redirected and limit of redirections were reached');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testFollowRedirectWithCookies()
|
public function testFollowRedirectWithCookies()
|
||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
|
Reference in New Issue
Block a user