bug #29298 [Routing] fix trailing slash redirection when using RedirectableUrlMatcher (nicolas-grekas)

This PR was merged into the 4.1 branch.

Discussion
----------

[Routing] fix trailing slash redirection when using RedirectableUrlMatcher

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29297
| License       | MIT
| Doc PR        | -

This is #29297 for 4.1

Commits
-------

6968000a3c [Routing] fix trailing slash redirection when using RedirectableUrlMatcher
This commit is contained in:
Nicolas Grekas 2018-11-26 11:24:14 +01:00
commit 95ebc9f38f
17 changed files with 1520 additions and 1323 deletions

View File

@ -29,6 +29,7 @@ class PhpMatcherDumper extends MatcherDumper
{ {
private $expressionLanguage; private $expressionLanguage;
private $signalingException; private $signalingException;
private $supportsRedirections;
/** /**
* @var ExpressionFunctionProviderInterface[] * @var ExpressionFunctionProviderInterface[]
@ -56,7 +57,7 @@ class PhpMatcherDumper extends MatcherDumper
// trailing slash support is only enabled if we know how to redirect the user // trailing slash support is only enabled if we know how to redirect the user
$interfaces = class_implements($options['base_class']); $interfaces = class_implements($options['base_class']);
$supportsRedirections = isset($interfaces[RedirectableUrlMatcherInterface::class]); $this->supportsRedirections = isset($interfaces[RedirectableUrlMatcherInterface::class]);
return <<<EOF return <<<EOF
<?php <?php
@ -76,7 +77,7 @@ class {$options['class']} extends {$options['base_class']}
\$this->context = \$context; \$this->context = \$context;
} }
{$this->generateMatchMethod($supportsRedirections)} {$this->generateMatchMethod()}
} }
EOF; EOF;
@ -90,7 +91,7 @@ EOF;
/** /**
* Generates the code for the match method implementing UrlMatcherInterface. * Generates the code for the match method implementing UrlMatcherInterface.
*/ */
private function generateMatchMethod(bool $supportsRedirections): string private function generateMatchMethod(): string
{ {
// Group hosts by same-suffix, re-order when possible // Group hosts by same-suffix, re-order when possible
$matchHost = false; $matchHost = false;
@ -111,7 +112,7 @@ EOF;
$code = <<<EOF $code = <<<EOF
{ {
\$allow = \$allowSchemes = array(); \$allow = \$allowSchemes = array();
\$pathinfo = rawurldecode(\$rawPathinfo); \$pathinfo = rawurldecode(\$rawPathinfo) ?: '/';
\$context = \$this->context; \$context = \$this->context;
\$requestMethod = \$canonicalMethod = \$context->getMethod(); \$requestMethod = \$canonicalMethod = \$context->getMethod();
{$fetchHost} {$fetchHost}
@ -123,7 +124,7 @@ $code
EOF; EOF;
if ($supportsRedirections) { if ($this->supportsRedirections) {
return <<<'EOF' return <<<'EOF'
public function match($pathinfo) public function match($pathinfo)
{ {
@ -213,9 +214,18 @@ EOF
$compiledRoute = $route->compile(); $compiledRoute = $route->compile();
$hostRegex = $compiledRoute->getHostRegex(); $hostRegex = $compiledRoute->getHostRegex();
$regex = $compiledRoute->getRegex(); $regex = $compiledRoute->getRegex();
if ($hasTrailingSlash = '/' !== $route->getPath()) {
$pos = strrpos($regex, '$');
$hasTrailingSlash = '/' === $regex[$pos - 1];
$regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
}
if (!$compiledRoute->getPathVariables()) { if (!$compiledRoute->getPathVariables()) {
$host = !$compiledRoute->getHostVariables() ? $route->getHost() : ''; $host = !$compiledRoute->getHostVariables() ? $route->getHost() : '';
$url = $route->getPath(); $url = $route->getPath();
if ($hasTrailingSlash) {
$url = substr($url, 0, -1);
}
foreach ($dynamicRegex as list($hostRx, $rx)) { foreach ($dynamicRegex as list($hostRx, $rx)) {
if (preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) { if (preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
$dynamicRegex[] = array($hostRegex, $regex); $dynamicRegex[] = array($hostRegex, $regex);
@ -224,7 +234,7 @@ EOF
} }
} }
$staticRoutes[$url][$name] = $route; $staticRoutes[$url][$name] = array($route, $hasTrailingSlash);
} else { } else {
$dynamicRegex[] = array($hostRegex, $regex); $dynamicRegex[] = array($hostRegex, $regex);
$dynamicRoutes->add($name, $route); $dynamicRoutes->add($name, $route);
@ -251,7 +261,7 @@ EOF
foreach ($staticRoutes as $url => $routes) { foreach ($staticRoutes as $url => $routes) {
if (1 === \count($routes)) { if (1 === \count($routes)) {
foreach ($routes as $name => $route) { foreach ($routes as $name => list($route, $hasTrailingSlash)) {
} }
if (!$route->getCondition()) { if (!$route->getCondition()) {
@ -261,20 +271,21 @@ EOF
unset($defaults['_canonical_route']); unset($defaults['_canonical_route']);
} }
$default .= sprintf( $default .= sprintf(
"%s => array(%s, %s, %s, %s),\n", "%s => array(%s, %s, %s, %s, %s),\n",
self::export($url), self::export($url),
self::export(array('_route' => $name) + $defaults), self::export(array('_route' => $name) + $defaults),
self::export(!$route->compile()->getHostVariables() ? $route->getHost() : $route->compile()->getHostRegex() ?: null), self::export(!$route->compile()->getHostVariables() ? $route->getHost() : $route->compile()->getHostRegex() ?: null),
self::export(array_flip($route->getMethods()) ?: null), self::export(array_flip($route->getMethods()) ?: null),
self::export(array_flip($route->getSchemes()) ?: null) self::export(array_flip($route->getSchemes()) ?: null),
self::export($hasTrailingSlash)
); );
continue; continue;
} }
} }
$code .= sprintf(" case %s:\n", self::export($url)); $code .= sprintf(" case %s:\n", self::export($url));
foreach ($routes as $name => $route) { foreach ($routes as $name => list($route, $hasTrailingSlash)) {
$code .= $this->compileRoute($route, $name, true); $code .= $this->compileRoute($route, $name, true, $hasTrailingSlash);
} }
$code .= " break;\n"; $code .= " break;\n";
} }
@ -285,15 +296,15 @@ EOF
\$routes = array( \$routes = array(
{$this->indent($default, 4)} ); {$this->indent($default, 4)} );
if (!isset(\$routes[\$pathinfo])) { if (!isset(\$routes[\$trimmedPathinfo])) {
break; break;
} }
list(\$ret, \$requiredHost, \$requiredMethods, \$requiredSchemes) = \$routes[\$pathinfo]; list(\$ret, \$requiredHost, \$requiredMethods, \$requiredSchemes, \$hasTrailingSlash) = \$routes[\$trimmedPathinfo];
{$this->compileSwitchDefault(false, $matchHost)} {$this->compileSwitchDefault(false, $matchHost)}
EOF; EOF;
} }
return sprintf(" switch (\$pathinfo) {\n%s }\n\n", $this->indent($code)); return sprintf(" switch (\$trimmedPathinfo = '/' !== \$pathinfo && '/' === \$pathinfo[-1] ? substr(\$pathinfo, 0, -1) : \$pathinfo) {\n%s }\n\n", $this->indent($code));
} }
/** /**
@ -394,7 +405,11 @@ EOF;
$state->vars = array(); $state->vars = array();
$regex = preg_replace_callback('#\?P<([^>]++)>#', $state->getVars, $rx[1]); $regex = preg_replace_callback('#\?P<([^>]++)>#', $state->getVars, $rx[1]);
$tree->addRoute($regex, array($name, $regex, $state->vars, $route)); if ($hasTrailingSlash = '/' !== $regex && '/' === $regex[-1]) {
$regex = substr($regex, 0, -1);
}
$tree->addRoute($regex, array($name, $regex, $state->vars, $route, $hasTrailingSlash));
} }
$code .= $this->compileStaticPrefixCollection($tree, $state); $code .= $this->compileStaticPrefixCollection($tree, $state);
@ -403,7 +418,7 @@ EOF;
$code .= "\n .')'"; $code .= "\n .')'";
$state->regex .= ')'; $state->regex .= ')';
} }
$rx = ")$}{$modifiers}"; $rx = ")(?:/?)$}{$modifiers}";
$code .= "\n .'{$rx}',"; $code .= "\n .'{$rx}',";
$state->regex .= $rx; $state->regex .= $rx;
$state->markTail = 0; $state->markTail = 0;
@ -423,7 +438,7 @@ EOF;
\$routes = array( \$routes = array(
{$this->indent($state->default, 4)} ); {$this->indent($state->default, 4)} );
list(\$ret, \$vars, \$requiredMethods, \$requiredSchemes) = \$routes[\$m]; list(\$ret, \$vars, \$requiredMethods, \$requiredSchemes, \$hasTrailingSlash) = \$routes[\$m];
{$this->compileSwitchDefault(true, $matchHost)} {$this->compileSwitchDefault(true, $matchHost)}
EOF; EOF;
} }
@ -478,11 +493,11 @@ EOF;
continue; continue;
} }
list($name, $regex, $vars, $route) = $route; list($name, $regex, $vars, $route, $hasTrailingSlash) = $route;
$compiledRoute = $route->compile(); $compiledRoute = $route->compile();
if ($compiledRoute->getRegex() === $prevRegex) { if ($compiledRoute->getRegex() === $prevRegex) {
$state->switch = substr_replace($state->switch, $this->compileRoute($route, $name, false)."\n", -19, 0); $state->switch = substr_replace($state->switch, $this->compileRoute($route, $name, false, $hasTrailingSlash)."\n", -19, 0);
continue; continue;
} }
@ -501,12 +516,13 @@ EOF;
unset($defaults['_canonical_route']); unset($defaults['_canonical_route']);
} }
$state->default .= sprintf( $state->default .= sprintf(
"%s => array(%s, %s, %s, %s),\n", "%s => array(%s, %s, %s, %s, %s),\n",
$state->mark, $state->mark,
self::export(array('_route' => $name) + $defaults), self::export(array('_route' => $name) + $defaults),
self::export($vars), self::export($vars),
self::export(array_flip($route->getMethods()) ?: null), self::export(array_flip($route->getMethods()) ?: null),
self::export(array_flip($route->getSchemes()) ?: null) self::export(array_flip($route->getSchemes()) ?: null),
self::export($hasTrailingSlash)
); );
} else { } else {
$prevRegex = $compiledRoute->getRegex(); $prevRegex = $compiledRoute->getRegex();
@ -518,7 +534,7 @@ EOF;
$state->switch .= <<<EOF $state->switch .= <<<EOF
case {$state->mark}: case {$state->mark}:
{$combine}{$this->compileRoute($route, $name, false)} {$combine}{$this->compileRoute($route, $name, false, $hasTrailingSlash)}
break; break;
EOF; EOF;
@ -533,8 +549,15 @@ EOF;
*/ */
private function compileSwitchDefault(bool $hasVars, bool $matchHost): string private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
{ {
$code = sprintf("
if ('/' !== \$pathinfo && \$hasTrailingSlash !== ('/' === \$pathinfo[-1])) {
%s;
}\n",
$this->supportsRedirections ? 'return null' : 'break'
);
if ($hasVars) { if ($hasVars) {
$code = <<<EOF $code .= <<<EOF
foreach (\$vars as \$i => \$v) { foreach (\$vars as \$i => \$v) {
if (isset(\$matches[1 + \$i])) { if (isset(\$matches[1 + \$i])) {
@ -544,7 +567,7 @@ EOF;
EOF; EOF;
} elseif ($matchHost) { } elseif ($matchHost) {
$code = <<<EOF $code .= <<<EOF
if (\$requiredHost) { if (\$requiredHost) {
if ('#' !== \$requiredHost[0] ? \$requiredHost !== \$host : !preg_match(\$requiredHost, \$host, \$hostMatches)) { if ('#' !== \$requiredHost[0] ? \$requiredHost !== \$host : !preg_match(\$requiredHost, \$host, \$hostMatches)) {
@ -557,8 +580,6 @@ EOF;
} }
EOF; EOF;
} else {
$code = '';
} }
$code .= <<<EOF $code .= <<<EOF
@ -587,9 +608,22 @@ EOF;
* *
* @throws \LogicException * @throws \LogicException
*/ */
private function compileRoute(Route $route, string $name, bool $checkHost): string private function compileRoute(Route $route, string $name, bool $checkHost, bool $hasTrailingSlash): string
{ {
$code = ''; $code = " // $name";
if ('/' !== $route->getPath()) {
$code .= sprintf("
if ('/' !== \$pathinfo && '/' %s \$pathinfo[-1]) {
%s;
}\n",
$hasTrailingSlash ? '!==' : '===',
$this->supportsRedirections ? 'return null' : 'break'
);
} else {
$code .= "\n";
}
$compiledRoute = $route->compile(); $compiledRoute = $route->compile();
$conditions = array(); $conditions = array();
$matches = (bool) $compiledRoute->getPathVariables(); $matches = (bool) $compiledRoute->getPathVariables();
@ -617,12 +651,11 @@ EOF;
if ($conditions) { if ($conditions) {
$code .= <<<EOF $code .= <<<EOF
// $name
if ($conditions) { if ($conditions) {
EOF; EOF;
} else { } else {
$code .= " // {$name}\n"; $code = $this->indent($code);
} }
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name); $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);

View File

@ -130,16 +130,38 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
*/ */
protected function matchCollection($pathinfo, RouteCollection $routes) protected function matchCollection($pathinfo, RouteCollection $routes)
{ {
$supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
foreach ($routes as $name => $route) { foreach ($routes as $name => $route) {
$compiledRoute = $route->compile(); $compiledRoute = $route->compile();
$staticPrefix = $compiledRoute->getStaticPrefix();
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) { if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
// no-op
} elseif (!$supportsTrailingSlash) {
continue;
} elseif ('/' === $staticPrefix[-1] && substr($staticPrefix, 0, -1) === $pathinfo) {
return;
} elseif ('/' === $pathinfo[-1] && substr($pathinfo, 0, -1) === $staticPrefix) {
return;
} else {
continue;
}
$regex = $compiledRoute->getRegex();
if ($supportsTrailingSlash) {
$pos = strrpos($regex, '$');
$hasTrailingSlash = '/' === $regex[$pos - 1];
$regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
}
if (!preg_match($regex, $pathinfo, $matches)) {
continue; continue;
} }
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) { if ($supportsTrailingSlash && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
continue; return;
} }
$hostMatches = array(); $hostMatches = array();

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$host = strtolower($context->getHost()); $host = strtolower($context->getHost());
@ -27,32 +27,36 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
default: default:
$routes = array( $routes = array(
'/test/baz' => array(array('_route' => 'baz'), null, null, null), '/test/baz' => array(array('_route' => 'baz'), null, null, null, false),
'/test/baz.html' => array(array('_route' => 'baz2'), null, null, null), '/test/baz.html' => array(array('_route' => 'baz2'), null, null, null, false),
'/test/baz3/' => array(array('_route' => 'baz3'), null, null, null), '/test/baz3' => array(array('_route' => 'baz3'), null, null, null, true),
'/foofoo' => array(array('_route' => 'foofoo', 'def' => 'test'), null, null, null), '/foofoo' => array(array('_route' => 'foofoo', 'def' => 'test'), null, null, null, false),
'/spa ce' => array(array('_route' => 'space'), null, null, null), '/spa ce' => array(array('_route' => 'space'), null, null, null, false),
'/multi/new' => array(array('_route' => 'overridden2'), null, null, null), '/multi/new' => array(array('_route' => 'overridden2'), null, null, null, false),
'/multi/hey/' => array(array('_route' => 'hey'), null, null, null), '/multi/hey' => array(array('_route' => 'hey'), null, null, null, true),
'/ababa' => array(array('_route' => 'ababa'), null, null, null), '/ababa' => array(array('_route' => 'ababa'), null, null, null, false),
'/route1' => array(array('_route' => 'route1'), 'a.example.com', null, null), '/route1' => array(array('_route' => 'route1'), 'a.example.com', null, null, false),
'/c2/route2' => array(array('_route' => 'route2'), 'a.example.com', null, null), '/c2/route2' => array(array('_route' => 'route2'), 'a.example.com', null, null, false),
'/route4' => array(array('_route' => 'route4'), 'a.example.com', null, null), '/route4' => array(array('_route' => 'route4'), 'a.example.com', null, null, false),
'/c2/route3' => array(array('_route' => 'route3'), 'b.example.com', null, null), '/c2/route3' => array(array('_route' => 'route3'), 'b.example.com', null, null, false),
'/route5' => array(array('_route' => 'route5'), 'c.example.com', null, null), '/route5' => array(array('_route' => 'route5'), 'c.example.com', null, null, false),
'/route6' => array(array('_route' => 'route6'), null, null, null), '/route6' => array(array('_route' => 'route6'), null, null, null, false),
'/route11' => array(array('_route' => 'route11'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null), '/route11' => array(array('_route' => 'route11'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null, false),
'/route12' => array(array('_route' => 'route12', 'var1' => 'val'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null), '/route12' => array(array('_route' => 'route12', 'var1' => 'val'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null, false),
'/route17' => array(array('_route' => 'route17'), null, null, null), '/route17' => array(array('_route' => 'route17'), null, null, null, false),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break;
}
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo];
if ($requiredHost) { if ($requiredHost) {
if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) { if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
@ -88,57 +92,63 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
.'|/([^/]++)(*:70)' .'|/([^/]++)(*:70)'
.'|head/([^/]++)(*:90)' .'|head/([^/]++)(*:90)'
.')' .')'
.'|/test/([^/]++)/(?' .'|/test/([^/]++)(?'
.'|(*:116)' .'|(*:115)'
.')' .')'
.'|/([\']+)(*:132)' .'|/([\']+)(*:131)'
.'|/a/(?' .'|/a/(?'
.'|b\'b/([^/]++)(?' .'|b\'b/([^/]++)(?'
.'|(*:161)' .'|(*:160)'
.'|(*:169)' .'|(*:168)'
.')' .')'
.'|(.*)(*:182)' .'|(.*)(*:181)'
.'|b\'b/([^/]++)(?' .'|b\'b/([^/]++)(?'
.'|(*:205)' .'|(*:204)'
.'|(*:213)' .'|(*:212)'
.')' .')'
.')' .')'
.'|/multi/hello(?:/([^/]++))?(*:249)' .'|/multi/hello(?:/([^/]++))?(*:248)'
.'|/([^/]++)/b/([^/]++)(?' .'|/([^/]++)/b/([^/]++)(?'
.'|(*:280)' .'|(*:279)'
.'|(*:288)' .'|(*:287)'
.')' .')'
.'|/aba/([^/]++)(*:310)' .'|/aba/([^/]++)(*:309)'
.')|(?i:([^\\.]++)\\.example\\.com)\\.(?' .')|(?i:([^\\.]++)\\.example\\.com)\\.(?'
.'|/route1(?' .'|/route1(?'
.'|3/([^/]++)(*:372)' .'|3/([^/]++)(*:371)'
.'|4/([^/]++)(*:390)' .'|4/([^/]++)(*:389)'
.')' .')'
.')|(?i:c\\.example\\.com)\\.(?' .')|(?i:c\\.example\\.com)\\.(?'
.'|/route15/([^/]++)(*:442)' .'|/route15/([^/]++)(*:441)'
.')|(?:(?:[^./]*+\\.)++)(?' .')|(?:(?:[^./]*+\\.)++)(?'
.'|/route16/([^/]++)(*:490)' .'|/route16/([^/]++)(*:489)'
.'|/a/(?' .'|/a/(?'
.'|a\\.\\.\\.(*:511)' .'|a\\.\\.\\.(*:510)'
.'|b/(?' .'|b/(?'
.'|([^/]++)(*:532)' .'|([^/]++)(*:531)'
.'|c/([^/]++)(*:550)' .'|c/([^/]++)(*:549)'
.')' .')'
.')' .')'
.')' .')'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
while (preg_match($regex, $matchedPathinfo, $matches)) { while (preg_match($regex, $matchedPathinfo, $matches)) {
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
case 116: case 115:
$matches = array('foo' => $matches[1] ?? null); $matches = array('foo' => $matches[1] ?? null);
// baz4 // baz4
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
break;
}
return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array()); return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
// baz5 // baz5
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
break;
}
$ret = $this->mergeDefaults(array('_route' => 'baz5') + $matches, array()); $ret = $this->mergeDefaults(array('_route' => 'baz5') + $matches, array());
if (!isset(($a = array('POST' => 0))[$requestMethod])) { if (!isset(($a = array('POST' => 0))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -149,6 +159,9 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
not_baz5: not_baz5:
// baz.baz6 // baz.baz6
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
break;
}
$ret = $this->mergeDefaults(array('_route' => 'baz.baz6') + $matches, array()); $ret = $this->mergeDefaults(array('_route' => 'baz.baz6') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) { if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -159,10 +172,13 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
not_bazbaz6: not_bazbaz6:
break; break;
case 161: case 160:
$matches = array('foo' => $matches[1] ?? null); $matches = array('foo' => $matches[1] ?? null);
// foo1 // foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array()); $ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) { if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -173,42 +189,52 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
not_foo1: not_foo1:
break; break;
case 205: case 204:
$matches = array('foo1' => $matches[1] ?? null); $matches = array('foo1' => $matches[1] ?? null);
// foo2 // foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array()); return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
break; break;
case 280: case 279:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null); $matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
// foo3 // foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array()); return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
break; break;
default: default:
$routes = array( $routes = array(
47 => array(array('_route' => 'foo', 'def' => 'test'), array('bar'), null, null), 47 => array(array('_route' => 'foo', 'def' => 'test'), array('bar'), null, null, false),
70 => array(array('_route' => 'bar'), array('foo'), array('GET' => 0, 'HEAD' => 1), null), 70 => array(array('_route' => 'bar'), array('foo'), array('GET' => 0, 'HEAD' => 1), null, false),
90 => array(array('_route' => 'barhead'), array('foo'), array('GET' => 0), null), 90 => array(array('_route' => 'barhead'), array('foo'), array('GET' => 0), null, false),
132 => array(array('_route' => 'quoter'), array('quoter'), null, null), 131 => array(array('_route' => 'quoter'), array('quoter'), null, null, false),
169 => array(array('_route' => 'bar1'), array('bar'), null, null), 168 => array(array('_route' => 'bar1'), array('bar'), null, null, false),
182 => array(array('_route' => 'overridden'), array('var'), null, null), 181 => array(array('_route' => 'overridden'), array('var'), null, null, false),
213 => array(array('_route' => 'bar2'), array('bar1'), null, null), 212 => array(array('_route' => 'bar2'), array('bar1'), null, null, false),
249 => array(array('_route' => 'helloWorld', 'who' => 'World!'), array('who'), null, null), 248 => array(array('_route' => 'helloWorld', 'who' => 'World!'), array('who'), null, null, false),
288 => array(array('_route' => 'bar3'), array('_locale', 'bar'), null, null), 287 => array(array('_route' => 'bar3'), array('_locale', 'bar'), null, null, false),
310 => array(array('_route' => 'foo4'), array('foo'), null, null), 309 => array(array('_route' => 'foo4'), array('foo'), null, null, false),
372 => array(array('_route' => 'route13'), array('var1', 'name'), null, null), 371 => array(array('_route' => 'route13'), array('var1', 'name'), null, null, false),
390 => array(array('_route' => 'route14', 'var1' => 'val'), array('var1', 'name'), null, null), 389 => array(array('_route' => 'route14', 'var1' => 'val'), array('var1', 'name'), null, null, false),
442 => array(array('_route' => 'route15'), array('name'), null, null), 441 => array(array('_route' => 'route15'), array('name'), null, null, false),
490 => array(array('_route' => 'route16', 'var1' => 'val'), array('name'), null, null), 489 => array(array('_route' => 'route16', 'var1' => 'val'), array('name'), null, null, false),
511 => array(array('_route' => 'a'), array(), null, null), 510 => array(array('_route' => 'a'), array(), null, null, false),
532 => array(array('_route' => 'b'), array('var'), null, null), 531 => array(array('_route' => 'b'), array('var'), null, null, false),
550 => array(array('_route' => 'c'), array('var'), null, null), 549 => array(array('_route' => 'c'), array('var'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {
@ -231,7 +257,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
return $ret; return $ret;
} }
if (550 === $m) { if (549 === $m) {
break; break;
} }
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m)); $regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));

View File

@ -53,7 +53,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
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(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -65,30 +65,34 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$regexList = array( $regexList = array(
0 => '{^(?' 0 => '{^(?'
.'|/(en|fr)/(?' .'|/(en|fr)/(?'
.'|admin/post/(?' .'|admin/post(?'
.'|(*:33)' .'|(*:32)'
.'|new(*:43)' .'|/(?'
.'|(\\d+)(*:55)' .'|new(*:46)'
.'|(\\d+)/edit(*:72)' .'|(\\d+)(*:58)'
.'|(\\d+)/delete(*:91)' .'|(\\d+)/edit(*:75)'
.'|(\\d+)/delete(*:94)'
.')' .')'
.'|blog/(?' .')'
.'|(*:107)' .'|blog(?'
.'|rss\\.xml(*:123)' .'|(*:110)'
.'|/(?'
.'|rss\\.xml(*:130)'
.'|p(?' .'|p(?'
.'|age/([^/]++)(*:147)' .'|age/([^/]++)(*:154)'
.'|osts/([^/]++)(*:168)' .'|osts/([^/]++)(*:175)'
.')'
.'|comments/(\\d+)/new(*:202)'
.'|search(*:216)'
.')' .')'
.'|comments/(\\d+)/new(*:195)'
.'|search(*:209)'
.')' .')'
.'|log(?' .'|log(?'
.'|in(*:226)' .'|in(*:234)'
.'|out(*:237)' .'|out(*:245)'
.')' .')'
.')' .')'
.'|/(en|fr)?(*:256)' .'|/(en|fr)?(*:264)'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -96,23 +100,27 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
33 => array(array('_route' => 'a', '_locale' => 'en'), array('_locale'), null, null), 32 => array(array('_route' => 'a', '_locale' => 'en'), array('_locale'), null, null, true),
43 => array(array('_route' => 'b', '_locale' => 'en'), array('_locale'), null, null), 46 => array(array('_route' => 'b', '_locale' => 'en'), array('_locale'), null, null, false),
55 => array(array('_route' => 'c', '_locale' => 'en'), array('_locale', 'id'), null, null), 58 => array(array('_route' => 'c', '_locale' => 'en'), array('_locale', 'id'), null, null, false),
72 => array(array('_route' => 'd', '_locale' => 'en'), array('_locale', 'id'), null, null), 75 => array(array('_route' => 'd', '_locale' => 'en'), array('_locale', 'id'), null, null, false),
91 => array(array('_route' => 'e', '_locale' => 'en'), array('_locale', 'id'), null, null), 94 => array(array('_route' => 'e', '_locale' => 'en'), array('_locale', 'id'), null, null, false),
107 => array(array('_route' => 'f', '_locale' => 'en'), array('_locale'), null, null), 110 => array(array('_route' => 'f', '_locale' => 'en'), array('_locale'), null, null, true),
123 => array(array('_route' => 'g', '_locale' => 'en'), array('_locale'), null, null), 130 => array(array('_route' => 'g', '_locale' => 'en'), array('_locale'), null, null, false),
147 => array(array('_route' => 'h', '_locale' => 'en'), array('_locale', 'page'), null, null), 154 => array(array('_route' => 'h', '_locale' => 'en'), array('_locale', 'page'), null, null, false),
168 => array(array('_route' => 'i', '_locale' => 'en'), array('_locale', 'page'), null, null), 175 => array(array('_route' => 'i', '_locale' => 'en'), array('_locale', 'page'), null, null, false),
195 => array(array('_route' => 'j', '_locale' => 'en'), array('_locale', 'id'), null, null), 202 => array(array('_route' => 'j', '_locale' => 'en'), array('_locale', 'id'), null, null, false),
209 => array(array('_route' => 'k', '_locale' => 'en'), array('_locale'), null, null), 216 => array(array('_route' => 'k', '_locale' => 'en'), array('_locale'), null, null, false),
226 => array(array('_route' => 'l', '_locale' => 'en'), array('_locale'), null, null), 234 => array(array('_route' => 'l', '_locale' => 'en'), array('_locale'), null, null, false),
237 => array(array('_route' => 'm', '_locale' => 'en'), array('_locale'), null, null), 245 => array(array('_route' => 'm', '_locale' => 'en'), array('_locale'), null, null, false),
256 => array(array('_route' => 'n', '_locale' => 'en'), array('_locale'), null, null), 264 => array(array('_route' => 'n', '_locale' => 'en'), array('_locale'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {
@ -135,7 +143,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
return $ret; return $ret;
} }
if (256 === $m) { if (264 === $m) {
break; break;
} }
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m)); $regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -45,7 +45,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
.')' .')'
.')' .')'
.')' .')'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -53,15 +53,19 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
27 => array(array('_route' => 'r1'), array('foo'), null, null), 27 => array(array('_route' => 'r1'), array('foo'), null, null, false),
38 => array(array('_route' => 'r10'), array('foo'), null, null), 38 => array(array('_route' => 'r10'), array('foo'), null, null, false),
46 => array(array('_route' => 'r100'), array('foo'), null, null), 46 => array(array('_route' => 'r100'), array('foo'), null, null, false),
59 => array(array('_route' => 'r2'), array('foo'), null, null), 59 => array(array('_route' => 'r2'), array('foo'), null, null, false),
70 => array(array('_route' => 'r20'), array('foo'), null, null), 70 => array(array('_route' => 'r20'), array('foo'), null, null, false),
78 => array(array('_route' => 'r200'), array('foo'), null, null), 78 => array(array('_route' => 'r200'), array('foo'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$host = strtolower($context->getHost()); $host = strtolower($context->getHost());
@ -35,7 +35,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
.'|(*:56)' .'|(*:56)'
.')' .')'
.')' .')'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -45,9 +45,15 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null); $matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
// r1 // r1
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
return $this->mergeDefaults(array('_route' => 'r1') + $matches, array()); return $this->mergeDefaults(array('_route' => 'r1') + $matches, array());
// r2 // r2
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
return $this->mergeDefaults(array('_route' => 'r2') + $matches, array()); return $this->mergeDefaults(array('_route' => 'r2') + $matches, array());
break; break;

View File

@ -53,7 +53,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
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(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$host = strtolower($context->getHost()); $host = strtolower($context->getHost());
@ -62,34 +62,38 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
default: default:
$routes = array( $routes = array(
'/test/baz' => array(array('_route' => 'baz'), null, null, null), '/test/baz' => array(array('_route' => 'baz'), null, null, null, false),
'/test/baz.html' => array(array('_route' => 'baz2'), null, null, null), '/test/baz.html' => array(array('_route' => 'baz2'), null, null, null, false),
'/test/baz3/' => array(array('_route' => 'baz3'), null, null, null), '/test/baz3' => array(array('_route' => 'baz3'), null, null, null, true),
'/foofoo' => array(array('_route' => 'foofoo', 'def' => 'test'), null, null, null), '/foofoo' => array(array('_route' => 'foofoo', 'def' => 'test'), null, null, null, false),
'/spa ce' => array(array('_route' => 'space'), null, null, null), '/spa ce' => array(array('_route' => 'space'), null, null, null, false),
'/multi/new' => array(array('_route' => 'overridden2'), null, null, null), '/multi/new' => array(array('_route' => 'overridden2'), null, null, null, false),
'/multi/hey/' => array(array('_route' => 'hey'), null, null, null), '/multi/hey' => array(array('_route' => 'hey'), null, null, null, true),
'/ababa' => array(array('_route' => 'ababa'), null, null, null), '/ababa' => array(array('_route' => 'ababa'), null, null, null, false),
'/route1' => array(array('_route' => 'route1'), 'a.example.com', null, null), '/route1' => array(array('_route' => 'route1'), 'a.example.com', null, null, false),
'/c2/route2' => array(array('_route' => 'route2'), 'a.example.com', null, null), '/c2/route2' => array(array('_route' => 'route2'), 'a.example.com', null, null, false),
'/route4' => array(array('_route' => 'route4'), 'a.example.com', null, null), '/route4' => array(array('_route' => 'route4'), 'a.example.com', null, null, false),
'/c2/route3' => array(array('_route' => 'route3'), 'b.example.com', null, null), '/c2/route3' => array(array('_route' => 'route3'), 'b.example.com', null, null, false),
'/route5' => array(array('_route' => 'route5'), 'c.example.com', null, null), '/route5' => array(array('_route' => 'route5'), 'c.example.com', null, null, false),
'/route6' => array(array('_route' => 'route6'), null, null, null), '/route6' => array(array('_route' => 'route6'), null, null, null, false),
'/route11' => array(array('_route' => 'route11'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null), '/route11' => array(array('_route' => 'route11'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null, false),
'/route12' => array(array('_route' => 'route12', 'var1' => 'val'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null), '/route12' => array(array('_route' => 'route12', 'var1' => 'val'), '#^(?P<var1>[^\\.]++)\\.example\\.com$#sDi', null, null, false),
'/route17' => array(array('_route' => 'route17'), null, null, null), '/route17' => array(array('_route' => 'route17'), null, null, null, false),
'/secure' => array(array('_route' => 'secure'), null, null, array('https' => 0)), '/secure' => array(array('_route' => 'secure'), null, null, array('https' => 0), false),
'/nonsecure' => array(array('_route' => 'nonsecure'), null, null, array('http' => 0)), '/nonsecure' => array(array('_route' => 'nonsecure'), null, null, array('http' => 0), false),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo]; list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
if ($requiredHost) { if ($requiredHost) {
if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) { if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
@ -125,57 +129,63 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
.'|/([^/]++)(*:70)' .'|/([^/]++)(*:70)'
.'|head/([^/]++)(*:90)' .'|head/([^/]++)(*:90)'
.')' .')'
.'|/test/([^/]++)/(?' .'|/test/([^/]++)(?'
.'|(*:116)' .'|(*:115)'
.')' .')'
.'|/([\']+)(*:132)' .'|/([\']+)(*:131)'
.'|/a/(?' .'|/a/(?'
.'|b\'b/([^/]++)(?' .'|b\'b/([^/]++)(?'
.'|(*:161)' .'|(*:160)'
.'|(*:169)' .'|(*:168)'
.')' .')'
.'|(.*)(*:182)' .'|(.*)(*:181)'
.'|b\'b/([^/]++)(?' .'|b\'b/([^/]++)(?'
.'|(*:205)' .'|(*:204)'
.'|(*:213)' .'|(*:212)'
.')' .')'
.')' .')'
.'|/multi/hello(?:/([^/]++))?(*:249)' .'|/multi/hello(?:/([^/]++))?(*:248)'
.'|/([^/]++)/b/([^/]++)(?' .'|/([^/]++)/b/([^/]++)(?'
.'|(*:280)' .'|(*:279)'
.'|(*:288)' .'|(*:287)'
.')' .')'
.'|/aba/([^/]++)(*:310)' .'|/aba/([^/]++)(*:309)'
.')|(?i:([^\\.]++)\\.example\\.com)\\.(?' .')|(?i:([^\\.]++)\\.example\\.com)\\.(?'
.'|/route1(?' .'|/route1(?'
.'|3/([^/]++)(*:372)' .'|3/([^/]++)(*:371)'
.'|4/([^/]++)(*:390)' .'|4/([^/]++)(*:389)'
.')' .')'
.')|(?i:c\\.example\\.com)\\.(?' .')|(?i:c\\.example\\.com)\\.(?'
.'|/route15/([^/]++)(*:442)' .'|/route15/([^/]++)(*:441)'
.')|(?:(?:[^./]*+\\.)++)(?' .')|(?:(?:[^./]*+\\.)++)(?'
.'|/route16/([^/]++)(*:490)' .'|/route16/([^/]++)(*:489)'
.'|/a/(?' .'|/a/(?'
.'|a\\.\\.\\.(*:511)' .'|a\\.\\.\\.(*:510)'
.'|b/(?' .'|b/(?'
.'|([^/]++)(*:532)' .'|([^/]++)(*:531)'
.'|c/([^/]++)(*:550)' .'|c/([^/]++)(*:549)'
.')' .')'
.')' .')'
.')' .')'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
while (preg_match($regex, $matchedPathinfo, $matches)) { while (preg_match($regex, $matchedPathinfo, $matches)) {
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
case 116: case 115:
$matches = array('foo' => $matches[1] ?? null); $matches = array('foo' => $matches[1] ?? null);
// baz4 // baz4
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
return null;
}
return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array()); return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
// baz5 // baz5
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
return null;
}
$ret = $this->mergeDefaults(array('_route' => 'baz5') + $matches, array()); $ret = $this->mergeDefaults(array('_route' => 'baz5') + $matches, array());
if (!isset(($a = array('POST' => 0))[$requestMethod])) { if (!isset(($a = array('POST' => 0))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -186,6 +196,9 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
not_baz5: not_baz5:
// baz.baz6 // baz.baz6
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
return null;
}
$ret = $this->mergeDefaults(array('_route' => 'baz.baz6') + $matches, array()); $ret = $this->mergeDefaults(array('_route' => 'baz.baz6') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) { if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -196,10 +209,13 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
not_bazbaz6: not_bazbaz6:
break; break;
case 161: case 160:
$matches = array('foo' => $matches[1] ?? null); $matches = array('foo' => $matches[1] ?? null);
// foo1 // foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
return null;
}
$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array()); $ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) { if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -210,42 +226,52 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
not_foo1: not_foo1:
break; break;
case 205: case 204:
$matches = array('foo1' => $matches[1] ?? null); $matches = array('foo1' => $matches[1] ?? null);
// foo2 // foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
return null;
}
return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array()); return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
break; break;
case 280: case 279:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null); $matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
// foo3 // foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
return null;
}
return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array()); return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
break; break;
default: default:
$routes = array( $routes = array(
47 => array(array('_route' => 'foo', 'def' => 'test'), array('bar'), null, null), 47 => array(array('_route' => 'foo', 'def' => 'test'), array('bar'), null, null, false),
70 => array(array('_route' => 'bar'), array('foo'), array('GET' => 0, 'HEAD' => 1), null), 70 => array(array('_route' => 'bar'), array('foo'), array('GET' => 0, 'HEAD' => 1), null, false),
90 => array(array('_route' => 'barhead'), array('foo'), array('GET' => 0), null), 90 => array(array('_route' => 'barhead'), array('foo'), array('GET' => 0), null, false),
132 => array(array('_route' => 'quoter'), array('quoter'), null, null), 131 => array(array('_route' => 'quoter'), array('quoter'), null, null, false),
169 => array(array('_route' => 'bar1'), array('bar'), null, null), 168 => array(array('_route' => 'bar1'), array('bar'), null, null, false),
182 => array(array('_route' => 'overridden'), array('var'), null, null), 181 => array(array('_route' => 'overridden'), array('var'), null, null, false),
213 => array(array('_route' => 'bar2'), array('bar1'), null, null), 212 => array(array('_route' => 'bar2'), array('bar1'), null, null, false),
249 => array(array('_route' => 'helloWorld', 'who' => 'World!'), array('who'), null, null), 248 => array(array('_route' => 'helloWorld', 'who' => 'World!'), array('who'), null, null, false),
288 => array(array('_route' => 'bar3'), array('_locale', 'bar'), null, null), 287 => array(array('_route' => 'bar3'), array('_locale', 'bar'), null, null, false),
310 => array(array('_route' => 'foo4'), array('foo'), null, null), 309 => array(array('_route' => 'foo4'), array('foo'), null, null, false),
372 => array(array('_route' => 'route13'), array('var1', 'name'), null, null), 371 => array(array('_route' => 'route13'), array('var1', 'name'), null, null, false),
390 => array(array('_route' => 'route14', 'var1' => 'val'), array('var1', 'name'), null, null), 389 => array(array('_route' => 'route14', 'var1' => 'val'), array('var1', 'name'), null, null, false),
442 => array(array('_route' => 'route15'), array('name'), null, null), 441 => array(array('_route' => 'route15'), array('name'), null, null, false),
490 => array(array('_route' => 'route16', 'var1' => 'val'), array('name'), null, null), 489 => array(array('_route' => 'route16', 'var1' => 'val'), array('name'), null, null, false),
511 => array(array('_route' => 'a'), array(), null, null), 510 => array(array('_route' => 'a'), array(), null, null, false),
532 => array(array('_route' => 'b'), array('var'), null, null), 531 => array(array('_route' => 'b'), array('var'), null, null, false),
550 => array(array('_route' => 'c'), array('var'), null, null), 549 => array(array('_route' => 'c'), array('var'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {
@ -268,7 +294,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
return $ret; return $ret;
} }
if (550 === $m) { if (549 === $m) {
break; break;
} }
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m)); $regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -26,22 +26,29 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
case '/with-condition': case '/with-condition':
// with-condition // with-condition
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
if (($context->getMethod() == "GET")) { if (($context->getMethod() == "GET")) {
return array('_route' => 'with-condition'); return array('_route' => 'with-condition');
} }
break; break;
default: default:
$routes = array( $routes = array(
'/rootprefix/test' => array(array('_route' => 'static'), null, null, null), '/rootprefix/test' => array(array('_route' => 'static'), null, null, null, false),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break;
}
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo];
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
@ -62,7 +69,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$regexList = array( $regexList = array(
0 => '{^(?' 0 => '{^(?'
.'|/rootprefix/([^/]++)(*:27)' .'|/rootprefix/([^/]++)(*:27)'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -70,10 +77,14 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
27 => array(array('_route' => 'dynamic'), array('var'), null, null), 27 => array(array('_route' => 'dynamic'), array('var'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -26,9 +26,12 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
case '/put_and_post': case '/put_and_post':
// put_and_post // put_and_post
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
$ret = array('_route' => 'put_and_post'); $ret = array('_route' => 'put_and_post');
if (!isset(($a = array('PUT' => 0, 'POST' => 1))[$requestMethod])) { if (!isset(($a = array('PUT' => 0, 'POST' => 1))[$requestMethod])) {
$allow += $a; $allow += $a;
@ -38,6 +41,9 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
return $ret; return $ret;
not_put_and_post: not_put_and_post:
// put_and_get_and_head // put_and_get_and_head
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
break;
}
$ret = array('_route' => 'put_and_get_and_head'); $ret = array('_route' => 'put_and_get_and_head');
if (!isset(($a = array('PUT' => 0, 'GET' => 1, 'HEAD' => 2))[$canonicalMethod])) { if (!isset(($a = array('PUT' => 0, 'GET' => 1, 'HEAD' => 2))[$canonicalMethod])) {
$allow += $a; $allow += $a;
@ -49,16 +55,20 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
break; break;
default: default:
$routes = array( $routes = array(
'/just_head' => array(array('_route' => 'just_head'), null, array('HEAD' => 0), null), '/just_head' => array(array('_route' => 'just_head'), null, array('HEAD' => 0), null, false),
'/head_and_get' => array(array('_route' => 'head_and_get'), null, array('HEAD' => 0, 'GET' => 1), null), '/head_and_get' => array(array('_route' => 'head_and_get'), null, array('HEAD' => 0, 'GET' => 1), null, false),
'/get_and_head' => array(array('_route' => 'get_and_head'), null, array('GET' => 0, 'HEAD' => 1), null), '/get_and_head' => array(array('_route' => 'get_and_head'), null, array('GET' => 0, 'HEAD' => 1), null, false),
'/post_and_head' => array(array('_route' => 'post_and_head'), null, array('POST' => 0, 'HEAD' => 1), null), '/post_and_head' => array(array('_route' => 'post_and_head'), null, array('POST' => 0, 'HEAD' => 1), null, false),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break;
}
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo];
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {

View File

@ -53,7 +53,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
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(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -61,27 +61,31 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
default: default:
$routes = array( $routes = array(
'/a/11' => array(array('_route' => 'a_first'), null, null, null), '/a/11' => array(array('_route' => 'a_first'), null, null, null, false),
'/a/22' => array(array('_route' => 'a_second'), null, null, null), '/a/22' => array(array('_route' => 'a_second'), null, null, null, false),
'/a/333' => array(array('_route' => 'a_third'), null, null, null), '/a/333' => array(array('_route' => 'a_third'), null, null, null, false),
'/a/44/' => array(array('_route' => 'a_fourth'), null, null, null), '/a/44' => array(array('_route' => 'a_fourth'), null, null, null, true),
'/a/55/' => array(array('_route' => 'a_fifth'), null, null, null), '/a/55' => array(array('_route' => 'a_fifth'), null, null, null, true),
'/a/66/' => array(array('_route' => 'a_sixth'), null, null, null), '/a/66' => array(array('_route' => 'a_sixth'), null, null, null, true),
'/nested/group/a/' => array(array('_route' => 'nested_a'), null, null, null), '/nested/group/a' => array(array('_route' => 'nested_a'), null, null, null, true),
'/nested/group/b/' => array(array('_route' => 'nested_b'), null, null, null), '/nested/group/b' => array(array('_route' => 'nested_b'), null, null, null, true),
'/nested/group/c/' => array(array('_route' => 'nested_c'), null, null, null), '/nested/group/c' => array(array('_route' => 'nested_c'), null, null, null, true),
'/slashed/group/' => array(array('_route' => 'slashed_a'), null, null, null), '/slashed/group' => array(array('_route' => 'slashed_a'), null, null, null, true),
'/slashed/group/b/' => array(array('_route' => 'slashed_b'), null, null, null), '/slashed/group/b' => array(array('_route' => 'slashed_b'), null, null, null, true),
'/slashed/group/c/' => array(array('_route' => 'slashed_c'), null, null, null), '/slashed/group/c' => array(array('_route' => 'slashed_c'), null, null, null, true),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo]; list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
@ -103,7 +107,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
0 => '{^(?' 0 => '{^(?'
.'|/([^/]++)(*:16)' .'|/([^/]++)(*:16)'
.'|/nested/([^/]++)(*:39)' .'|/nested/([^/]++)(*:39)'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -111,11 +115,15 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
16 => array(array('_route' => 'a_wildcard'), array('param'), null, null), 16 => array(array('_route' => 'a_wildcard'), array('param'), null, null, false),
39 => array(array('_route' => 'nested_wildcard'), array('param'), null, null), 39 => array(array('_route' => 'nested_wildcard'), array('param'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -26,23 +26,27 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
default: default:
$routes = array( $routes = array(
'/trailing/simple/no-methods/' => array(array('_route' => 'simple_trailing_slash_no_methods'), null, null, null), '/trailing/simple/no-methods' => array(array('_route' => 'simple_trailing_slash_no_methods'), null, null, null, true),
'/trailing/simple/get-method/' => array(array('_route' => 'simple_trailing_slash_GET_method'), null, array('GET' => 0), null), '/trailing/simple/get-method' => array(array('_route' => 'simple_trailing_slash_GET_method'), null, array('GET' => 0), null, true),
'/trailing/simple/head-method/' => array(array('_route' => 'simple_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null), '/trailing/simple/head-method' => array(array('_route' => 'simple_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null, true),
'/trailing/simple/post-method/' => array(array('_route' => 'simple_trailing_slash_POST_method'), null, array('POST' => 0), null), '/trailing/simple/post-method' => array(array('_route' => 'simple_trailing_slash_POST_method'), null, array('POST' => 0), null, true),
'/not-trailing/simple/no-methods' => array(array('_route' => 'simple_not_trailing_slash_no_methods'), null, null, null), '/not-trailing/simple/no-methods' => array(array('_route' => 'simple_not_trailing_slash_no_methods'), null, null, null, false),
'/not-trailing/simple/get-method' => array(array('_route' => 'simple_not_trailing_slash_GET_method'), null, array('GET' => 0), null), '/not-trailing/simple/get-method' => array(array('_route' => 'simple_not_trailing_slash_GET_method'), null, array('GET' => 0), null, false),
'/not-trailing/simple/head-method' => array(array('_route' => 'simple_not_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null), '/not-trailing/simple/head-method' => array(array('_route' => 'simple_not_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null, false),
'/not-trailing/simple/post-method' => array(array('_route' => 'simple_not_trailing_slash_POST_method'), null, array('POST' => 0), null), '/not-trailing/simple/post-method' => array(array('_route' => 'simple_not_trailing_slash_POST_method'), null, array('POST' => 0), null, false),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break;
}
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo];
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
@ -63,18 +67,18 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$regexList = array( $regexList = array(
0 => '{^(?' 0 => '{^(?'
.'|/trailing/regex/(?' .'|/trailing/regex/(?'
.'|no\\-methods/([^/]++)/(*:47)' .'|no\\-methods/([^/]++)(*:46)'
.'|get\\-method/([^/]++)/(*:75)' .'|get\\-method/([^/]++)(*:73)'
.'|head\\-method/([^/]++)/(*:104)' .'|head\\-method/([^/]++)(*:101)'
.'|post\\-method/([^/]++)/(*:134)' .'|post\\-method/([^/]++)(*:130)'
.')' .')'
.'|/not\\-trailing/regex/(?' .'|/not\\-trailing/regex/(?'
.'|no\\-methods/([^/]++)(*:187)' .'|no\\-methods/([^/]++)(*:183)'
.'|get\\-method/([^/]++)(*:215)' .'|get\\-method/([^/]++)(*:211)'
.'|head\\-method/([^/]++)(*:244)' .'|head\\-method/([^/]++)(*:240)'
.'|post\\-method/([^/]++)(*:273)' .'|post\\-method/([^/]++)(*:269)'
.')' .')'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -82,17 +86,21 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
47 => array(array('_route' => 'regex_trailing_slash_no_methods'), array('param'), null, null), 46 => array(array('_route' => 'regex_trailing_slash_no_methods'), array('param'), null, null, true),
75 => array(array('_route' => 'regex_trailing_slash_GET_method'), array('param'), array('GET' => 0), null), 73 => array(array('_route' => 'regex_trailing_slash_GET_method'), array('param'), array('GET' => 0), null, true),
104 => array(array('_route' => 'regex_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null), 101 => array(array('_route' => 'regex_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null, true),
134 => array(array('_route' => 'regex_trailing_slash_POST_method'), array('param'), array('POST' => 0), null), 130 => array(array('_route' => 'regex_trailing_slash_POST_method'), array('param'), array('POST' => 0), null, true),
187 => array(array('_route' => 'regex_not_trailing_slash_no_methods'), array('param'), null, null), 183 => array(array('_route' => 'regex_not_trailing_slash_no_methods'), array('param'), null, null, false),
215 => array(array('_route' => 'regex_not_trailing_slash_GET_method'), array('param'), array('GET' => 0), null), 211 => array(array('_route' => 'regex_not_trailing_slash_GET_method'), array('param'), array('GET' => 0), null, false),
244 => array(array('_route' => 'regex_not_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null), 240 => array(array('_route' => 'regex_not_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null, false),
273 => array(array('_route' => 'regex_not_trailing_slash_POST_method'), array('param'), array('POST' => 0), null), 269 => array(array('_route' => 'regex_not_trailing_slash_POST_method'), array('param'), array('POST' => 0), null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {
@ -115,7 +123,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
return $ret; return $ret;
} }
if (273 === $m) { if (269 === $m) {
break; break;
} }
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m)); $regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));

View File

@ -53,7 +53,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
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(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -61,23 +61,27 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
default: default:
$routes = array( $routes = array(
'/trailing/simple/no-methods/' => array(array('_route' => 'simple_trailing_slash_no_methods'), null, null, null), '/trailing/simple/no-methods' => array(array('_route' => 'simple_trailing_slash_no_methods'), null, null, null, true),
'/trailing/simple/get-method/' => array(array('_route' => 'simple_trailing_slash_GET_method'), null, array('GET' => 0), null), '/trailing/simple/get-method' => array(array('_route' => 'simple_trailing_slash_GET_method'), null, array('GET' => 0), null, true),
'/trailing/simple/head-method/' => array(array('_route' => 'simple_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null), '/trailing/simple/head-method' => array(array('_route' => 'simple_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null, true),
'/trailing/simple/post-method/' => array(array('_route' => 'simple_trailing_slash_POST_method'), null, array('POST' => 0), null), '/trailing/simple/post-method' => array(array('_route' => 'simple_trailing_slash_POST_method'), null, array('POST' => 0), null, true),
'/not-trailing/simple/no-methods' => array(array('_route' => 'simple_not_trailing_slash_no_methods'), null, null, null), '/not-trailing/simple/no-methods' => array(array('_route' => 'simple_not_trailing_slash_no_methods'), null, null, null, false),
'/not-trailing/simple/get-method' => array(array('_route' => 'simple_not_trailing_slash_GET_method'), null, array('GET' => 0), null), '/not-trailing/simple/get-method' => array(array('_route' => 'simple_not_trailing_slash_GET_method'), null, array('GET' => 0), null, false),
'/not-trailing/simple/head-method' => array(array('_route' => 'simple_not_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null), '/not-trailing/simple/head-method' => array(array('_route' => 'simple_not_trailing_slash_HEAD_method'), null, array('HEAD' => 0), null, false),
'/not-trailing/simple/post-method' => array(array('_route' => 'simple_not_trailing_slash_POST_method'), null, array('POST' => 0), null), '/not-trailing/simple/post-method' => array(array('_route' => 'simple_not_trailing_slash_POST_method'), null, array('POST' => 0), null, false),
); );
if (!isset($routes[$pathinfo])) { if (!isset($routes[$trimmedPathinfo])) {
break; break;
} }
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo]; list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
@ -98,18 +102,18 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$regexList = array( $regexList = array(
0 => '{^(?' 0 => '{^(?'
.'|/trailing/regex/(?' .'|/trailing/regex/(?'
.'|no\\-methods/([^/]++)/(*:47)' .'|no\\-methods/([^/]++)(*:46)'
.'|get\\-method/([^/]++)/(*:75)' .'|get\\-method/([^/]++)(*:73)'
.'|head\\-method/([^/]++)/(*:104)' .'|head\\-method/([^/]++)(*:101)'
.'|post\\-method/([^/]++)/(*:134)' .'|post\\-method/([^/]++)(*:130)'
.')' .')'
.'|/not\\-trailing/regex/(?' .'|/not\\-trailing/regex/(?'
.'|no\\-methods/([^/]++)(*:187)' .'|no\\-methods/([^/]++)(*:183)'
.'|get\\-method/([^/]++)(*:215)' .'|get\\-method/([^/]++)(*:211)'
.'|head\\-method/([^/]++)(*:244)' .'|head\\-method/([^/]++)(*:240)'
.'|post\\-method/([^/]++)(*:273)' .'|post\\-method/([^/]++)(*:269)'
.')' .')'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -117,17 +121,21 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
47 => array(array('_route' => 'regex_trailing_slash_no_methods'), array('param'), null, null), 46 => array(array('_route' => 'regex_trailing_slash_no_methods'), array('param'), null, null, true),
75 => array(array('_route' => 'regex_trailing_slash_GET_method'), array('param'), array('GET' => 0), null), 73 => array(array('_route' => 'regex_trailing_slash_GET_method'), array('param'), array('GET' => 0), null, true),
104 => array(array('_route' => 'regex_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null), 101 => array(array('_route' => 'regex_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null, true),
134 => array(array('_route' => 'regex_trailing_slash_POST_method'), array('param'), array('POST' => 0), null), 130 => array(array('_route' => 'regex_trailing_slash_POST_method'), array('param'), array('POST' => 0), null, true),
187 => array(array('_route' => 'regex_not_trailing_slash_no_methods'), array('param'), null, null), 183 => array(array('_route' => 'regex_not_trailing_slash_no_methods'), array('param'), null, null, false),
215 => array(array('_route' => 'regex_not_trailing_slash_GET_method'), array('param'), array('GET' => 0), null), 211 => array(array('_route' => 'regex_not_trailing_slash_GET_method'), array('param'), array('GET' => 0), null, false),
244 => array(array('_route' => 'regex_not_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null), 240 => array(array('_route' => 'regex_not_trailing_slash_HEAD_method'), array('param'), array('HEAD' => 0), null, false),
273 => array(array('_route' => 'regex_not_trailing_slash_POST_method'), array('param'), array('POST' => 0), null), 269 => array(array('_route' => 'regex_not_trailing_slash_POST_method'), array('param'), array('POST' => 0), null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
return null;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {
@ -150,7 +158,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
return $ret; return $ret;
} }
if (273 === $m) { if (269 === $m) {
break; break;
} }
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m)); $regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
@ -30,13 +30,13 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$regexList = array( $regexList = array(
0 => '{^(?' 0 => '{^(?'
.'|/(a)(*:11)' .'|/(a)(*:11)'
.')$}sD', .')(?:/?)$}sD',
11 => '{^(?' 11 => '{^(?'
.'|/(.)(*:22)' .'|/(.)(*:22)'
.')$}sDu', .')(?:/?)$}sDu',
22 => '{^(?' 22 => '{^(?'
.'|/(.)(*:33)' .'|/(.)(*:33)'
.')$}sD', .')(?:/?)$}sD',
); );
foreach ($regexList as $offset => $regex) { foreach ($regexList as $offset => $regex) {
@ -44,12 +44,16 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
switch ($m = (int) $matches['MARK']) { switch ($m = (int) $matches['MARK']) {
default: default:
$routes = array( $routes = array(
11 => array(array('_route' => 'a'), array('a'), null, null), 11 => array(array('_route' => 'a'), array('a'), null, null, false),
22 => array(array('_route' => 'b'), array('a'), null, null), 22 => array(array('_route' => 'b'), array('a'), null, null, false),
33 => array(array('_route' => 'c'), array('a'), null, null), 33 => array(array('_route' => 'c'), array('a'), null, null, false),
); );
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m]; list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];
if ('/' !== $pathinfo && $hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
foreach ($vars as $i => $v) { foreach ($vars as $i => $v) {
if (isset($matches[1 + $i])) { if (isset($matches[1 + $i])) {

View File

@ -18,7 +18,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
public function match($rawPathinfo) public function match($rawPathinfo)
{ {
$allow = $allowSchemes = array(); $allow = $allowSchemes = array();
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo) ?: '/';
$context = $this->context; $context = $this->context;
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$host = strtolower($context->getHost()); $host = strtolower($context->getHost());
@ -27,7 +27,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
switch ($pathinfo) { switch ($trimmedPathinfo = '/' !== $pathinfo && '/' === $pathinfo[-1] ? substr($pathinfo, 0, -1) : $pathinfo) {
case '/': case '/':
// a // a
if (preg_match('#^(?P<d>[^\\.]++)\\.e\\.c\\.b\\.a$#sDi', $host, $hostMatches)) { if (preg_match('#^(?P<d>[^\\.]++)\\.e\\.c\\.b\\.a$#sDi', $host, $hostMatches)) {

View File

@ -141,6 +141,25 @@ class RedirectableUrlMatcherTest extends UrlMatcherTest
$this->assertSame(array('_route' => 'foo'), $matcher->match('/foo')); $this->assertSame(array('_route' => 'foo'), $matcher->match('/foo'));
} }
public function testFallbackPage()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/'));
$coll->add('bar', new Route('/{name}'));
$matcher = $this->getUrlMatcher($coll);
$matcher->expects($this->once())->method('redirect')->with('/foo/', 'foo')->will($this->returnValue(array('_route' => 'foo')));
$this->assertSame(array('_route' => 'foo'), $matcher->match('/foo'));
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo'));
$coll->add('bar', new Route('/{name}/'));
$matcher = $this->getUrlMatcher($coll);
$matcher->expects($this->once())->method('redirect')->with('/foo', 'foo')->will($this->returnValue(array('_route' => 'foo')));
$this->assertSame(array('_route' => 'foo'), $matcher->match('/foo/'));
}
public function testMissingTrailingSlashAndScheme() public function testMissingTrailingSlashAndScheme()
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();