1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 10:03:27 +01:00

Adding the ability to specify max_redirects

This commit is contained in:
Michael Dowling 2013-10-05 21:29:14 -07:00
parent b65e1a70c6
commit d72d340609
3 changed files with 11 additions and 2 deletions

View File

@ -166,6 +166,8 @@ class MessageFactory implements MessageFactoryInterface
if ($value !== false) {
if ($value === 'strict') {
$request->getConfig()[RedirectPlugin::STRICT_REDIRECTS] = true;
} elseif (is_int($value)) {
$request->getConfig()[RedirectPlugin::MAX_REDIRECTS] = $value;
}
$request->getEventDispatcher()->addSubscriber($this->redirectPlugin);
}

View File

@ -16,7 +16,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RedirectPlugin implements EventSubscriberInterface
{
const STRICT_REDIRECTS = 'strict_redirects';
private $defaultMaxRedirects = 5;
const MAX_REDIRECTS = 'max_redirects';
public static function getSubscribedEvents()
{
@ -34,9 +34,10 @@ class RedirectPlugin implements EventSubscriberInterface
$request = $event->getRequest();
$redirectCount = 0;
$redirectResponse = $response = $event->getResponse();
$max = $request->getConfig()->get(self::MAX_REDIRECTS) ?: 5;
while (substr($redirectResponse->getStatusCode(), 0, 1) == '3' && $redirectResponse->hasHeader('Location')) {
if (++$redirectCount > $this->defaultMaxRedirects) {
if (++$redirectCount > $max) {
throw new TooManyRedirectsException("Will not follow more than {$redirectCount} redirects", $request);
}
$redirectRequest = $this->createRedirectRequest($request, $redirectResponse);

View File

@ -126,6 +126,12 @@ class MessageFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($request->getConfig()->get(RedirectPlugin::STRICT_REDIRECTS));
}
public function testCanEnableStrictRedirectsWithInt()
{
$request = (new MessageFactory)->createRequest('GET', '/', [], null, ['allow_redirects' => 10]);
$this->assertEquals(10, $request->getConfig()->get(RedirectPlugin::MAX_REDIRECTS));
}
public function testCanAddCookies()
{
$request = (new MessageFactory)->createRequest('GET', '/', [], null, ['cookies' => ['Foo' => 'Bar']]);