Skip to content

Commit 8366b8a

Browse files
committed
[Routing] fixed validity check for hostname params in UrlGenerator
1 parent a8ce621 commit 8366b8a

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,24 +199,31 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
199199
}
200200

201201
if ($hostnameTokens) {
202-
$ghost = '';
202+
$routeHost = '';
203203
foreach ($hostnameTokens as $token) {
204204
if ('variable' === $token[0]) {
205-
if (in_array($mergedParams[$token[3]], array(null, '', false), true)) {
206-
// check requirement
207-
if ($mergedParams[$token[3]] && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
208-
throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]));
205+
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
206+
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]);
207+
208+
if ($this->strictRequirements) {
209+
throw new InvalidParameterException($message);
210+
}
211+
212+
if ($this->logger) {
213+
$this->logger->err($message);
209214
}
215+
216+
return null;
210217
}
211218

212-
$ghost = $token[1].$mergedParams[$token[3]].$ghost;
219+
$routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
213220
} elseif ('text' === $token[0]) {
214-
$ghost = $token[1].$ghost;
221+
$routeHost = $token[1].$routeHost;
215222
}
216223
}
217224

218-
if ($ghost != $host) {
219-
$host = $ghost;
225+
if ($routeHost != $host) {
226+
$host = $routeHost;
220227
$absolute = true;
221228
}
222229
}

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,41 @@ public function testWithHostnameSameAsContextAndAbsolute()
398398
$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));
399399
}
400400

401+
/**
402+
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
403+
*/
404+
public function testUrlWithInvalidParameterInHostname()
405+
{
406+
$routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
407+
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
408+
}
409+
410+
/**
411+
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
412+
*/
413+
public function testUrlWithInvalidParameterInHostnameWhenParamHasADefaultValue()
414+
{
415+
$routes = $this->getRoutes('test', new Route('/', array('foo' => 'bar'), array('foo' => 'bar'), array(), '{foo}.example.com'));
416+
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
417+
}
418+
419+
/**
420+
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
421+
*/
422+
public function testUrlWithInvalidParameterEqualsDefaultValueInHostname()
423+
{
424+
$routes = $this->getRoutes('test', new Route('/', array('foo' => 'baz'), array('foo' => 'bar'), array(), '{foo}.example.com'));
425+
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
426+
}
427+
428+
public function testUrlWithInvalidParameterInHostnameInNonStrictMode()
429+
{
430+
$routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
431+
$generator = $this->getGenerator($routes);
432+
$generator->setStrictRequirements(false);
433+
$this->assertNull($generator->generate('test', array('foo' => 'baz'), false));
434+
}
435+
401436
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
402437
{
403438
$context = new RequestContext('/app.php');

0 commit comments

Comments
 (0)