8000 [Routing] Don't throw 405 when scheme requirement doesn't match by nicolas-grekas · Pull Request #26312 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Don't throw 405 when scheme requirement doesn't match #26312

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

Merged
merged 1 commit into from
Feb 26, 2018
Merged
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
29 changes: 25 additions & 4 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,18 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
$code .= <<<EOF
if ($methods) {
$methods = implode("', '", $methods);
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
\$hasRequiredScheme = isset(\$requiredSchemes[\$this->context->getScheme()]);
if (!in_array(\$this->context->getMethod(), array('$methods'))) {
if (\$hasRequiredScheme) {
\$allow = array_merge(\$allow, array('$methods'));
}
goto $gotoname;
}
if (!\$hasRequiredScheme) {
if (!in_array(\$this->context->getMethod(), array('HEAD', 'GET'))) {
goto $gotoname;
}
Expand All @@ -290,9 +299,21 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren


EOF;
}
} else {
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
if (!in_array(\$this->context->getMethod(), array('HEAD', 'GET'))) {
goto $gotoname;
}

if ($methods) {
return \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes));
}


EOF;
}
} elseif ($methods) {
if (1 === count($methods)) {
$code .= <<<EOF
if (\$this->context->getMethod() != '$methods[0]') {
Expand Down
16 changes: 9 additions & 7 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
continue;
}

$status = $this->handleRouteRequirements($pathinfo, $name, $route);

if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}

// check HTTP method requirement
if ($requiredMethods = $route->getMethods()) {
// HEAD and GET are equivalent as per RFC
Expand All @@ -137,22 +143,18 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}

if (!in_array($method, $requiredMethods)) {
$this->allow = array_merge($this->allow, $requiredMethods);
if (self::REQUIREMENT_MATCH === $status[0]) {
$this->allow = array_merge($this->allow, $requiredMethods);
}

continue;
}
}

$status = $this->handleRouteRequirements($pathinfo, $name, $route);

if (self::ROUTE_MATCH === $status[0]) {
return $status[1];
}

if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}

return $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public function testSchemeRequirement()
parent::testSchemeRequirement();
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.
*/
public function testSchemeAndMethodMismatch()
{
parent::testSchemeRequirement();
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
static $i = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ public function testNestedCollections()
$this->assertEquals(array('_route' => 'buz'), $matcher->match('/prefix/buz'));
}

/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testSchemeAndMethodMismatch()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/', array(), array(), array(), null, array('https'), array('POST')));

$matcher = $this->getUrlMatcher($coll);
$matcher->match('/');
}

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