Skip to content

Commit 626209b

Browse files
committed
Merge branch '2.8' into 3.3
* 2.8: Preserve percent-encoding in URLs when performing redirects in the UrlMatcher [Console] Fix a bug when passing a letter that could be an alias add missing validation options to XSD file
2 parents 4788b7e + a70818d commit 626209b

File tree

6 files changed

+97
-15
lines changed

6 files changed

+97
-15
lines changed

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ private function generateMatchMethod($supportsRedirections)
9696
$code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n");
9797

9898
return <<<EOF
99-
public function match(\$pathinfo)
99+
public function match(\$rawPathinfo)
100100
{
101101
\$allow = array();
102-
\$pathinfo = rawurldecode(\$pathinfo);
102+
\$pathinfo = rawurldecode(\$rawPathinfo);
103103
\$trimmedPathinfo = rtrim(\$pathinfo, '/');
104104
\$context = \$this->context;
105105
\$request = \$this->request;
@@ -331,7 +331,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
331331
if ($hasTrailingSlash) {
332332
$code .= <<<EOF
333333
if (substr(\$pathinfo, -1) !== '/') {
334-
return \$this->redirect(\$pathinfo.'/', '$name');
334+
return \$this->redirect(\$rawPathinfo.'/', '$name');
335335
}
336336
337337
@@ -346,7 +346,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
346346
$code .= <<<EOF
347347
\$requiredSchemes = $schemes;
348348
if (!isset(\$requiredSchemes[\$scheme])) {
349-
return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes));
349+
return \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes));
350350
}
351351
352352

Tests/Fixtures/dumper/url_matcher1.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public function __construct(RequestContext $context)
1515
$this->context = $context;
1616
}
1717

18-
public function match($pathinfo)
18+
public function match($rawPathinfo)
1919
{
2020
$allow = array();
21-
$pathinfo = rawurldecode($pathinfo);
21+
$pathinfo = rawurldecode($rawPathinfo);
2222
$trimmedPathinfo = rtrim($pathinfo, '/');
2323
$context = $this->context;
2424
$request = $this->request;

Tests/Fixtures/dumper/url_matcher2.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public function __construct(RequestContext $context)
1515
$this->context = $context;
1616
}
1717

18-
public function match($pathinfo)
18+
public function match($rawPathinfo)
1919
{
2020
$allow = array();
21-
$pathinfo = rawurldecode($pathinfo);
21+
$pathinfo = rawurldecode($rawPathinfo);
2222
$trimmedPathinfo = rtrim($pathinfo, '/');
2323
$context = $this->context;
2424
$request = $this->request;
@@ -83,7 +83,7 @@ public function match($pathinfo)
8383
// baz3
8484
if ('/test/baz3' === $trimmedPathinfo) {
8585
if (substr($pathinfo, -1) !== '/') {
86-
return $this->redirect($pathinfo.'/', 'baz3');
86+
return $this->redirect($rawPathinfo.'/', 'baz3');
8787
}
8888

8989
return array('_route' => 'baz3');
@@ -94,7 +94,7 @@ public function match($pathinfo)
9494
// baz4
9595
if (preg_match('#^/test/(?P<foo>[^/]++)/?$#s', $pathinfo, $matches)) {
9696
if (substr($pathinfo, -1) !== '/') {
97-
return $this->redirect($pathinfo.'/', 'baz4');
97+
return $this->redirect($rawPathinfo.'/', 'baz4');
9898
}
9999

100100
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
@@ -177,7 +177,7 @@ public function match($pathinfo)
177177
// hey
178178
if ('/multi/hey' === $trimmedPathinfo) {
179179
if (substr($pathinfo, -1) !== '/') {
180-
return $this->redirect($pathinfo.'/', 'hey');
180+
return $this->redirect($rawPathinfo.'/', 'hey');
181181
}
182182

183183
return array('_route' => 'hey');
@@ -323,7 +323,7 @@ public function match($pathinfo)
323323
if ('/secure' === $pathinfo) {
324324
$requiredSchemes = array ( 'https' => 0,);
325325
if (!isset($requiredSchemes[$scheme])) {
326-
return $this->redirect($pathinfo, 'secure', key($requiredSchemes));
326+
return $this->redirect($rawPathinfo, 'secure', key($requiredSchemes));
327327
}
328328

329329
return array('_route' => 'secure');
@@ -333,7 +333,7 @@ public function match($pathinfo)
333333
if ('/nonsecure' === $pathinfo) {
334334
$requiredSchemes = array ( 'http' => 0,);
335335
if (!isset($requiredSchemes[$scheme])) {
336-
return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes));
336+
return $this->redirect($rawPathinfo, 'nonsecure', key($requiredSchemes));
337337
}
338338

339339
return array('_route' => 'nonsecure');

Tests/Fixtures/dumper/url_matcher3.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public function __construct(RequestContext $context)
1515
$this->context = $context;
1616
}
1717

18-
public function match($pathinfo)
18+
public function match($rawPathinfo)
1919
{
2020
$allow = array();
21-
$pathinfo = rawurldecode($pathinfo);
21+
$pathinfo = rawurldecode($rawPathinfo);
2222
$trimmedPathinfo = rtrim($pathinfo, '/');
2323
$context = $this->context;
2424
$request = $this->request;

Tests/Matcher/Dumper/PhpMatcherDumperTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,39 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
16+
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
17+
use Symfony\Component\Routing\Matcher\UrlMatcher;
18+
use Symfony\Component\Routing\RequestContext;
1619
use Symfony\Component\Routing\Route;
1720
use Symfony\Component\Routing\RouteCollection;
1821

1922
class PhpMatcherDumperTest extends TestCase
2023
{
24+
/**
25+
* @var string
26+
*/
27+
private $matcherClass;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $dumpPath;
33+
34+
protected function setUp()
35+
{
36+
parent::setUp();
37+
38+
$this->matcherClass = uniqid('ProjectUrlMatcher');
39+
$this->dumpPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_matcher.'.$this->matcherClass.'.php';
40+
}
41+
42+
protected function tearDown()
43+
{
44+
parent::tearDown();
45+
46+
@unlink($this->dumpPath);
47+
}
48+
2149
/**
2250
* @expectedException \LogicException
2351
*/
@@ -36,6 +64,23 @@ public function testDumpWhenSchemeIsUsedWithoutAProperDumper()
3664
$dumper->dump();
3765
}
3866

67+
public function testRedirectPreservesUrlEncoding()
68+
{
69+
$collection = new RouteCollection();
70+
$collection->add('foo', new Route('/foo:bar/'));
71+
72+
$class = $this->generateDumpedMatcher($collection, true);
73+
74+
$matcher = $this->getMockBuilder($class)
75+
->setMethods(array('redirect'))
76+
->setConstructorArgs(array(new RequestContext()))
77+
->getMock();
78+
79+
$matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/', 'foo');
80+
81+
$matcher->match('/foo%3Abar');
82+
}
83+
3984
/**
4085
* @dataProvider getRouteCollections
4186
*/
@@ -383,4 +428,31 @@ public function getRouteCollections()
383428
array($trailingSlashCollection, 'url_matcher7.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
384429
);
385430
}
431+
432+
/**
433+
* @param $dumper
434+
*/
435+
private function generateDumpedMatcher(RouteCollection $collection, $redirectableStub = false)
436+
{
437+
$options = array('class' => $this->matcherClass);
438+
439+
if ($redirectableStub) {
440+
$options['base_class'] = '\Symfony\Component\Routing\Tests\Matcher\Dumper\RedirectableUrlMatcherStub';
441+
}
442+
443+
$dumper = new PhpMatcherDumper($collection);
444+
$code = $dumper->dump($options);
445+
446+
file_put_contents($this->dumpPath, $code);
447+
include $this->dumpPath;
448+
449+
return $this->matcherClass;
450+
}
451+
}
452+
453+
abstract class RedirectableUrlMatcherStub extends UrlMatcher implements RedirectableUrlMatcherInterface
454+
{
455+
public function redirect($path, $route, $scheme = null)
456+
{
457+
}
386458
}

Tests/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,14 @@ public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches()
6969
;
7070
$matcher->match('/foo');
7171
}
72+
73+
public function testRedirectPreservesUrlEncoding()
74+
{
75+
$coll = new RouteCollection();
76+
$coll->add('foo', new Route('/foo:bar/'));
77+
78+
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
79+
$matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/');
80+
$matcher->match('/foo%3Abar');
81+
}
7282
}

0 commit comments

Comments
 (0)