8000 [Routing] Add a '_host' requirement for routes by sebhoerl · Pull Request #3002 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Add a '_host' requirement for routes #3002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Symfony/Component/Routing/Generator/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,15 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa

if ($this->context->getHost()) {
$scheme = $this->context->getScheme();
$host = $this->context->getHost();
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
$absolute = true;
$scheme = $req;
}
if (isset($requirements['_host']) && ($req = $requirements['_host']) && $host != $req) {
$absolute = true;
$host = $req;
}

if ($absolute) {
$port = '';
Expand All @@ -159,7 +164,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$port = ':'.$this->context->getHttpsPort();
}

$url = $scheme.'://'.$this->context->getHost().$port.$url;
$url = $scheme.'://'.$host.$port.$url;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function dump(array $options = array())

$rule = array("# $name");

if ($hostreq = $route->getRequirement('_host')) {
$rule[] = "RewriteCond %{HTTP_HOST} $hostreq";
}

// method mismatch
if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtoupper($req));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
if ($conditions) {
EOF;

if ($hostreq = $route->getRequirement('_host')) {
$code[] = <<<EOF
if(\$this->context->getHost() !== '$hostreq') {
goto $gotoname;
}
EOF;
}

if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtoupper($req));
// GET and HEAD are equivalent
Expand Down Expand Up @@ -239,7 +247,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
}
$code[] = " }";

if ($req) {
if ($req || $hostreq) {
$code[] = " $gotoname:";
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
continue;
}

// check host requirement
if (($req = $route->getRequirement('_host')) && $req !== $this->context->getHost()) {
continue;
}

// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ RewriteRule .* - [QSA,L]
RewriteCond %{REQUEST_URI} ^/foo/(baz|symfony)$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING_def:test]

# hostbar
RewriteCond %{HTTP_HOST} example.org
RewriteCond %{REQUEST_URI} ^/hostbar$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:hostbar]

# bar
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [NC]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public function match($pathinfo)
}
not_bar:

// barhost
if ($pathinfo === '/bar/') {
if($this->context->getHost() !== 'example.org') {
goto not_barhost;
}
return array('_route' => 'barhost');
}
not_barhost:

// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#xs', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
Expand Down
A36C
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public function match($pathinfo)
}
not_bar:

// barhost
if (rtrim($pathinfo, '/') === '/bar') {
if($this->context->getHost() !== 'example.org') {
goto not_barhost;
}
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'barhost');
}
return array('_route' => 'barhost');
}
not_barhost:

// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#xs', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testAbsoluteUrlWithHost()
{
$routes = $this->getRoutes('test', new Route( '/testing', array(), array('_host' => 'example.org')));
$url = $this->getGenerator($routes)->generate('test', array(), true);

$this->assertEquals('http://example.org/app.php/testing', $url);
}

public function testAbsoluteUrlWithPort80()
{
$routes = $this->getRoutes('test', new Route('/testing'));
Expand Down
Original file line number Diff line number Diff line change Expand Up @@ -34,6 +34,12 @@ public function testDump() array('def' => 'test'), array('bar' => 'baz|symfony') )); // host requirement $collection->add('hostbar', new Route( '/hostbar', array(), array('_host' => 'example.org') )); // method requirement $collection->add('bar', new Route( '/bar/{foo}', Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ protected function getRouteCollection()
array(),
array('_method' => 'GET|head')
));
// host requirement
$collection->add('barhost', new Route(
'/bar/',
array(),
array('_host' => 'example.org')
));
// GET method requirement automatically adds HEAD as valid
$collection->add('barhead', new Route(
'/barhead/{foo}',
Expand Down
16 changes: 16 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public function testNoMethodSoAllowed()
$matcher->match('/foo');
}

public function testHostRequirement()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array('_host' => 'example.org')));

$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'example.org'));
$this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo'));

$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'other.host'));

try {
$matcher->match('/foo');
$this->fail();
} catch (ResourceNotFoundException $e) {}
}

public function testMethodNotAllowed()
{
$coll = new RouteCollection();
Expand Down
0