Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] fix taking verb into account when redirecting #29425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public function match($pathinfo)
throw new ResourceNotFoundException();
}

private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): ?array
private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): array

EOF
.$code."\n return null;\n }";
.$code."\n return array();\n }";
}

return " public function match(\$rawPathinfo)\n".$code."\n throw \$allow ? new MethodNotAllowedException(array_keys(\$allow)) : new ResourceNotFoundException();\n }";
Expand Down Expand Up @@ -565,7 +565,7 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
}\n" : '',
$this->supportsRedirections ? "
if (!\$requiredMethods || isset(\$requiredMethods['GET'])) {
return null;
return \$allow = \$allowSchemes = array();
}" : ''
);

Expand Down Expand Up @@ -638,7 +638,7 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
%s;
}\n\n",
$hasTrailingSlash ? '!==' : '===',
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return null' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return $allow = $allowSchemes = array()' : 'break'
);
} elseif ($hasTrailingSlash) {
$code .= sprintf("
Expand All @@ -648,14 +648,14 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
if ('/' !== \$pathinfo && preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {
\$matches = \$n;
}\n\n",
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return null' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return $allow = $allowSchemes = array()' : 'break'
);
} else {
$code .= sprintf("
if ('/' !== \$pathinfo && '/' === \$pathinfo[-1] && preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {
%s;
}\n\n",
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return null' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return $allow = $allowSchemes = array()' : 'break'
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods))) {
continue;
} elseif ('/' === $staticPrefix[-1] && substr($staticPrefix, 0, -1) === $pathinfo) {
return;
return $this->allow = $this->allowSchemes = array();
} elseif ('/' === $pathinfo[-1] && substr($pathinfo, 0, -1) === $staticPrefix) {
return;
return $this->allow = $this->allowSchemes = array();
} else {
continue;
}
Expand All @@ -171,7 +171,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || \in_array('GET', $requiredMethods)) {
return;
return $this->allow = $this->allowSchemes = array();
}
continue;
}
Expand Down Expand Up @@ -212,6 +212,8 @@ protected function matchCollection($pathinfo, RouteCollection $routes)

return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array()));
}

return array();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function match($pathinfo)
throw new ResourceNotFoundException();
}

private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): ?array
private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): array
{
$allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo) ?: '/';
Expand Down Expand Up @@ -129,7 +129,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -167,6 +167,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
throw new Symfony\Component\Routing\Exception\NoConfigurationException();
}

return null;
return array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function match($pathinfo)
throw new ResourceNotFoundException();
}

private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): ?array
private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): array
{
$allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo) ?: '/';
Expand Down Expand Up @@ -94,7 +94,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
if ('/' !== $pathinfo) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -183,7 +183,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

// baz4
if ('/' !== $pathinfo[-1]) {
return null;
return $allow = $allowSchemes = array();
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
Expand Down Expand Up @@ -249,7 +249,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

// foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
return null;
return $allow = $allowSchemes = array();
}

return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
Expand All @@ -260,7 +260,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

// foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
return null;
return $allow = $allowSchemes = array();
}

return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
Expand Down Expand Up @@ -300,7 +300,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -338,6 +338,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
throw new Symfony\Component\Routing\Exception\NoConfigurationException();
}

return null;
return array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function match($pathinfo)
throw new ResourceNotFoundException();
}

private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): ?array
private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): array
{
$allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo) ?: '/';
Expand Down Expand Up @@ -86,7 +86,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
if ('/' !== $pathinfo) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -175,6 +175,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
throw new Symfony\Component\Routing\Exception\NoConfigurationException();
}

return null;
return array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function match($pathinfo)
throw new ResourceNotFoundException();
}

private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): ?array
private function doMatch(string $rawPathinfo, array &$allow = array(), array &$allowSchemes = array()): array
{
$allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo) ?: '/';
Expand Down Expand Up @@ -82,7 +82,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
if ('/' !== $pathinfo) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
return $allow = $allowSchemes = array();
}
break;
}
Expand Down Expand Up @@ -187,6 +187,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
throw new Symfony\Component\Routing\Exception\NoConfigurationException();
}

return null;
return array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ public function testMissingTrailingSlashAndScheme()
$matcher->match('/foo');
}

public function testSlashAndVerbPrecedenceWithRedirection()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('post')));
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('get')));

$matcher = $this->getUrlMatcher($coll);
$expected = array(
'_route' => 'b',
'customerId' => '123',
);
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons/'));

$matcher->expects($this->once())->method('redirect')->with('/api/customers/123/contactpersons/')->willReturn(array());
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($routes, $context ?: new RequestContext()));
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,20 @@ public function testSlashWithVerb()
$this->assertSame(array('_route' => 'b'), $matcher->match('/bar/'));
}

public function testSlashAndVerbPrecedence()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('post')));
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('get')));

$matcher = $this->getUrlMatcher($coll);
$expected = array(
'_route' => 'b',
'customerId' => '123',
);
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?: new RequestContext());
Expand Down