From 8366b8ab2789edce0d21df035c02a258cebdce7d Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Mon, 5 Nov 2012 14:26:00 +0100 Subject: [PATCH] [Routing] fixed validity check for hostname params in UrlGenerator --- .../Routing/Generator/UrlGenerator.php | 25 ++++++++----- .../Tests/Generator/UrlGeneratorTest.php | 35 +++++++++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index 5cf7dbdcc0..535e363e88 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -199,24 +199,31 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt } if ($hostnameTokens) { - $ghost = ''; + $routeHost = ''; foreach ($hostnameTokens as $token) { if ('variable' === $token[0]) { - if (in_array($mergedParams[$token[3]], array(null, '', false), true)) { - // check requirement - if ($mergedParams[$token[3]] && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { - throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]])); + if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { + $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]); + + if ($this->strictRequirements) { + throw new InvalidParameterException($message); } + + if ($this->logger) { + $this->logger->err($message); + } + + return null; } - $ghost = $token[1].$mergedParams[$token[3]].$ghost; + $routeHost = $token[1].$mergedParams[$token[3]].$routeHost; } elseif ('text' === $token[0]) { - $ghost = $token[1].$ghost; + $routeHost = $token[1].$routeHost; } } - if ($ghost != $host) { - $host = $ghost; + if ($routeHost != $host) { + $host = $routeHost; $absolute = true; } } diff --git a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php index 080125ce11..8cadee9e65 100644 --- a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php +++ b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php @@ -398,6 +398,41 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase $this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'), true)); } + /** + * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException + */ + public function testUrlWithInvalidParameterInHostname() + { + $routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com')); + $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false); + } + + /** + * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException + */ + public function testUrlWithInvalidParameterInHostnameWhenParamHasADefaultValue() + { + $routes = $this->getRoutes('test', new Route('/', array('foo' => 'bar'), array('foo' => 'bar'), array(), '{foo}.example.com')); + $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false); + } + + /** + * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException + */ + public function testUrlWithInvalidParameterEqualsDefaultValueInHostname() + { + $routes = $this->getRoutes('test', new Route('/', array('foo' => 'baz'), array('foo' => 'bar'), array(), '{foo}.example.com')); + $this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false); + } + + public function testUrlWithInvalidParameterInHostnameInNonStrictMode() + { + $routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com')); + $generator = $this->getGenerator($routes); + $generator->setStrictRequirements(false); + $this->assertNull($generator->generate('test', array('foo' => 'baz'), false)); + } + protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null) { $context = new RequestContext('/app.php');