Skip to content

[Routing] ignore trailing slash for non-GET requests #29443

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
42 changes: 22 additions & 20 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
}
}\n" : '',
$this->supportsRedirections ? "
if (!\$requiredMethods || isset(\$requiredMethods['GET'])) {
if ((!\$requiredMethods || isset(\$requiredMethods['GET'])) && 'GET' === \$canonicalMethod) {
return \$allow = \$allowSchemes = array();
}" : ''
);
Expand Down Expand Up @@ -628,34 +628,44 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
$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();
}" : ''
);
}

Expand Down Expand Up @@ -687,8 +697,6 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
$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();
Expand Down Expand Up @@ -770,16 +778,10 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
$code = substr_replace($code, 'return', $retOffset, 6);
}
if ($conditions) {
$code .= " }\n";
} elseif ($schemes || $methods) {
$code .= ' ';
}

if ($schemes || $methods) {
$code .= " $gotoname:\n";
$code = $this->indent($code)." }\n";
}

return $conditions ? $this->indent($code) : $code;
return $code." $gotoname:\n";
}

private function getExpressionLanguage()
Expand Down
13 changes: 6 additions & 7 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
*/
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) {
Expand All @@ -140,7 +144,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
// 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();
Expand Down Expand Up @@ -170,7 +174,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}
}
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;
Expand All @@ -190,11 +194,6 @@ protected function matchCollection($pathinfo, RouteCollection $routes)

$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@ public function match($rawPathinfo)

// 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;
Expand All @@ -170,7 +171,7 @@ public function match($rawPathinfo)

// 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;
Expand All @@ -191,7 +192,7 @@ public function match($rawPathinfo)

// 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());
Expand All @@ -209,21 +210,23 @@ public function match($rawPathinfo)

// 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:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
}

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public function match($rawPathinfo)

// 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

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

// 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;
Expand All @@ -210,7 +214,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

// 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;
Expand All @@ -231,7 +235,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

// 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());
Expand All @@ -249,21 +253,29 @@ 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 $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:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// 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:
Expand Down Expand Up @@ -299,7 +311,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
}

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ public function match($rawPathinfo)
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function match($rawPathinfo)
case '/put_and_post':
// put_and_post
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
goto not_put_and_post;
}

$ret = array('_route' => 'put_and_post');
Expand All @@ -43,7 +43,7 @@ public function match($rawPathinfo)
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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

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

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

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

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
if ((!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
return $allow = $allowSchemes = array();
}
break;
Expand Down
Loading