-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] added support for hostname requirement to routes #3057
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Routing/Exception/HostnameNotAllowedException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Exception; | ||
|
||
/** | ||
* The resource was found but the hostname is not allowed. | ||
* | ||
* This exception should trigger an HTTP 404 response in your application code. | ||
* | ||
* @author Gunnar Lium <post@gunnarlium.com> | ||
* | ||
*/ | ||
class HostnameNotAllowedException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
protected $allowedHostnames; | ||
|
||
public function __construct(array $allowedHostnames, $message = null, $code = 0, \Exception $previous = null) | ||
{ | ||
$this->allowedHostnames = array_map('strtolower', $allowedHostnames); | ||
|
||
parent::__construct($message, $code, $previous); | ||
} | ||
|
||
public function getAllowedHostnames() | ||
{ | ||
return $this->allowedHostnames; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Routing/Exception/InvalidHostnameParameterException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Exception; | ||
|
||
/** | ||
* Exception thrown when hostname parameter doesn't match hostname from requirements | ||
* | ||
* @author Gunnar Lium <post@gunnarlium.com> | ||
* | ||
* @api | ||
*/ | ||
class InvalidHostnameParameterException extends \InvalidArgumentException implements ExceptionInterface | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,11 +62,14 @@ private function addMatcher($supportsRedirections) | |
public function match(\$pathinfo) | ||
{ | ||
\$allow = array(); | ||
\$hosts = array(); | ||
\$pathinfo = urldecode(\$pathinfo); | ||
|
||
$code | ||
|
||
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException(); | ||
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : | ||
0 < count(\$hosts) ? new HostnameNotAllowedException(array_unique(\$hosts)) : | ||
new ResourceNotFoundException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
} | ||
|
||
EOF; | ||
|
@@ -148,14 +151,17 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren | |
$hasTrailingSlash = false; | ||
$matches = false; | ||
$methods = array(); | ||
|
||
$hostnames = array(); | ||
if ($req = $route->getRequirement('_method')) { | ||
$methods = explode('|', strtoupper($req)); | ||
// GET and HEAD are equivalent | ||
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { | ||
$methods[] = 'HEAD'; | ||
} | ||
} | ||
if ($req = $route->getRequirement('_host')) { | ||
$hostnames = explode('|', strtolower($req)); | ||
} | ||
|
||
$supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('HEAD', $methods)); | ||
|
||
|
@@ -208,6 +214,27 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren | |
goto $gotoname; | ||
} | ||
|
||
EOF; | ||
} | ||
} | ||
|
||
if ($hostnames) { | ||
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one is duplicated now. you could use this above the corresponding code generation
|
||
if (1 === count($hostnames)) { | ||
$code .= <<<EOF | ||
if (\$this->context->getHost() != '$hostnames[0]') { | ||
\$hosts[] = '$hostnames[0]'; | ||
goto $gotoname; | ||
} | ||
EOF; | ||
} else { | ||
$hostnames = implode('\', \'', $hostnames); | ||
$code .= <<<EOF | ||
if (!in_array(\$this->context->getHost(), array('$hostnames'))) { | ||
\$hosts = array_merge(\$hosts, array('$hostnames')); | ||
goto $gotoname; | ||
} | ||
|
||
EOF; | ||
} | ||
} | ||
|
@@ -248,7 +275,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren | |
} | ||
$code .= " }\n"; | ||
|
||
if ($methods) { | ||
if ($methods || $hostnames) { | ||
$code .= " $gotoname:\n"; | ||
} | ||
|
||
|
@@ -261,6 +288,7 @@ private function startClass($class, $baseClass) | |
<?php | ||
|
||
use Symfony\Component\Routing\Exception\MethodNotAllowedException; | ||
use Symfony\Component\Routing\Exception\HostnameNotAllowedException; | ||
use Symfony\Component\Routing\Exception\ResourceNotFoundException; | ||
use Symfony\Component\Routing\RequestContext; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not enough. If the current host (the one in the context) does not match the requirement, the url should be generated as absolute even if the call to
generate
did not set the absolute parameter totrue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you agree that it makes sense that routes with host requirement always are generated as absolute? If so, I believe adding $absolute = true at line 105 should do the trick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, they should be generated as absolute only if the host needs to be changed. if the current host already match the requirement, the control should be left to the developer if he doesn't want an absolute url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oups sorry, I missed the line 163 which already implements it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, missed it myself too ;)