Merge branch '3.4' into 4.1

* 3.4:
  [Routing] ignore trailing slash for non-GET requests
This commit is contained in:
Nicolas Grekas 2018-12-03 22:38:57 +01:00
commit db2c64d580
12 changed files with 84 additions and 51 deletions

View File

@ -564,7 +564,7 @@ EOF;
}
}\n" : '',
$this->supportsRedirections ? "
if (!\$requiredMethods || isset(\$requiredMethods['GET'])) {
if ((!\$requiredMethods || isset(\$requiredMethods['GET'])) && 'GET' === \$canonicalMethod) {
return \$allow = \$allowSchemes = array();
}" : ''
);
@ -628,34 +628,44 @@ EOF;
$matches = (bool) $compiledRoute->getPathVariables();
$hostMatches = (bool) $compiledRoute->getHostVariables();
$methods = array_flip($route->getMethods());
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
$code = " // $name";
if ('/' === $route->getPath()) {
$code .= "\n";
} elseif (!$matches) {
$code .= sprintf("
if ('/' !== \$pathinfo && '/' %s \$pathinfo[-1]) {
%s;
if ('/' !== \$pathinfo && '/' %s \$pathinfo[-1]) {%s
goto $gotoname;
}\n\n",
$hasTrailingSlash ? '!==' : '===',
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return $allow = $allowSchemes = array()' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? "
if ('GET' === \$canonicalMethod) {
return \$allow = \$allowSchemes = array();
}" : ''
);
} elseif ($hasTrailingSlash) {
$code .= sprintf("
if ('/' !== \$pathinfo[-1]) {
%s;
if ('/' !== \$pathinfo[-1]) {%s
goto $gotoname;
}
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 $allow = $allowSchemes = array()' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? "
if ('GET' === \$canonicalMethod) {
return \$allow = \$allowSchemes = array();
}" : ''
);
} else {
$code .= sprintf("
if ('/' !== \$pathinfo && '/' === \$pathinfo[-1] && preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {
%s;
if ('/' !== \$pathinfo && '/' === \$pathinfo[-1] && preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {%s
goto $gotoname;
}\n\n",
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return $allow = $allowSchemes = array()' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? "
if ('GET' === \$canonicalMethod) {
return \$allow = \$allowSchemes = array();
}" : ''
);
}
@ -687,8 +697,6 @@ EOF;
$code = $this->indent($code);
}
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
// the offset where the return value is appended below, with indendation
$retOffset = 12 + \strlen($code);
$defaults = $route->getDefaults();
@ -770,16 +778,10 @@ EOF;
$code = substr_replace($code, 'return', $retOffset, 6);
}
if ($conditions) {
$code .= " }\n";
} elseif ($schemes || $methods) {
$code .= ' ';
$code = $this->indent($code)." }\n";
}
if ($schemes || $methods) {
$code .= " $gotoname:\n";
}
return $conditions ? $this->indent($code) : $code;
return $code." $gotoname:\n";
}
private function getExpressionLanguage()

View File

@ -130,6 +130,10 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
*/
protected function matchCollection($pathinfo, RouteCollection $routes)
{
// HEAD and GET are equivalent as per RFC
if ('HEAD' === $method = $this->context->getMethod()) {
$method = 'GET';
}
$supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
foreach ($routes as $name => $route) {
@ -140,7 +144,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
// no-op
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods))) {
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods)) || 'GET' !== $method) {
continue;
} elseif ('/' === $staticPrefix[-1] && substr($staticPrefix, 0, -1) === $pathinfo) {
return $this->allow = $this->allowSchemes = array();
@ -170,7 +174,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
}
}
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || \in_array('GET', $requiredMethods)) {
if ((!$requiredMethods || \in_array('GET', $requiredMethods)) && 'GET' === $method) {
return $this->allow = $this->allowSchemes = array();
}
continue;
@ -190,11 +194,6 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
$hasRequiredScheme = !$route->getSchemes() || $route->hasScheme($this->context->getScheme());
if ($requiredMethods) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === $method = $this->context->getMethod()) {
$method = 'GET';
}
if (!\in_array($method, $requiredMethods)) {
if ($hasRequiredScheme) {
$this->allow = array_merge($this->allow, $requiredMethods);

View File

@ -143,17 +143,18 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// baz4
if ('/' !== $pathinfo[-1]) {
break;
goto not_baz4;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
}
return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
not_baz4:
// baz5
if ('/' !== $pathinfo[-1]) {
break;
goto not_baz5;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
@ -170,7 +171,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// baz.baz6
if ('/' !== $pathinfo[-1]) {
break;
goto not_bazbaz6;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
@ -191,7 +192,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
goto not_foo1;
}
$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
@ -209,10 +210,11 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
goto not_foo2;
}
return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
not_foo2:
break;
case 279:
@ -220,10 +222,11 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
goto not_foo3;
}
return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
not_foo3:
break;
default:

View File

@ -128,7 +128,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
}
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;

View File

@ -46,17 +46,19 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// r1
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
goto not_r1;
}
return $this->mergeDefaults(array('_route' => 'r1') + $matches, array());
not_r1:
// r2
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
goto not_r2;
}
return $this->mergeDefaults(array('_route' => 'r2') + $matches, array());
not_r2:
break;
}

View File

@ -93,7 +93,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
if ('/' !== $pathinfo) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
@ -183,17 +183,21 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// baz4
if ('/' !== $pathinfo[-1]) {
return $allow = $allowSchemes = array();
if ('GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
goto not_baz4;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
}
return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
not_baz4:
// baz5
if ('/' !== $pathinfo[-1]) {
break;
goto not_baz5;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
@ -210,7 +214,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// baz.baz6
if ('/' !== $pathinfo[-1]) {
break;
goto not_bazbaz6;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
@ -231,7 +235,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
goto not_foo1;
}
$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
@ -249,10 +253,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
return $allow = $allowSchemes = array();
if ('GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
goto not_foo2;
}
return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
not_foo2:
break;
case 279:
@ -260,10 +268,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
return $allow = $allowSchemes = array();
if ('GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
goto not_foo3;
}
return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
not_foo3:
break;
default:
@ -299,7 +311,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
}
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;

View File

@ -30,12 +30,13 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
case '/with-condition':
// with-condition
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
goto not_withcondition;
}
if (($context->getMethod() == "GET")) {
return array('_route' => 'with-condition');
}
not_withcondition:
break;
default:
$routes = array(

View File

@ -30,7 +30,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
case '/put_and_post':
// put_and_post
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
goto not_put_and_post;
}
$ret = array('_route' => 'put_and_post');
@ -43,7 +43,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
not_put_and_post:
// put_and_get_and_head
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
goto not_put_and_get_and_head;
}
$ret = array('_route' => 'put_and_get_and_head');

View File

@ -85,7 +85,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
if ('/' !== $pathinfo) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
@ -136,7 +136,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
}
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;

View File

@ -81,7 +81,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
if ('/' !== $pathinfo) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
@ -148,7 +148,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
}
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;

View File

@ -33,14 +33,17 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
if (preg_match('#^(?P<d>[^\\.]++)\\.e\\.c\\.b\\.a$#sDi', $host, $hostMatches)) {
return $this->mergeDefaults(array('_route' => 'a') + $hostMatches, array());
}
not_a:
// c
if (preg_match('#^(?P<e>[^\\.]++)\\.e\\.c\\.b\\.a$#sDi', $host, $hostMatches)) {
return $this->mergeDefaults(array('_route' => 'c') + $hostMatches, array());
}
not_c:
// b
if ('d.c.b.a' === $host) {
return array('_route' => 'b');
}
not_b:
break;
}

View File

@ -720,6 +720,17 @@ class UrlMatcherTest extends TestCase
'customerId' => '123',
);
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
$coll = new RouteCollection();
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('get')));
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('post')));
$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
$expected = array(
'_route' => 'b',
'customerId' => '123',
);
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
}
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)