diff --git a/src/Symfony/Component/HttpFoundation/RequestMatcher.php b/src/Symfony/Component/HttpFoundation/RequestMatcher.php index da95c3acc14d8..c571c604b4f12 100644 --- a/src/Symfony/Component/HttpFoundation/RequestMatcher.php +++ b/src/Symfony/Component/HttpFoundation/RequestMatcher.php @@ -45,24 +45,42 @@ class RequestMatcher implements RequestMatcherInterface */ private $attributes = array(); + /** + * @var string[] + */ + private $schemes = array(); + /** * @param string|null $path * @param string|null $host * @param string|string[]|null $methods * @param string|string[]|null $ips * @param array $attributes + * @param string|string[]|null $schemes */ - public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array()) + public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array(), $schemes = null) { $this->matchPath($path); $this->matchHost($host); $this->matchMethod($methods); $this->matchIps($ips); + $this->matchScheme($schemes); + foreach ($attributes as $k => $v) { $this->matchAttribute($k, $v); } } + /** + * Adds a check for the HTTP scheme. + * + * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes + */ + public function matchScheme($scheme) + { + $this->schemes = array_map('strtolower', (array) $scheme); + } + /** * Adds a check for the URL host name. * @@ -106,7 +124,7 @@ public function matchIps($ips) /** * Adds a check for the HTTP method. * - * @param string|string[]|null $method An HTTP method or an array of HTTP methods + * @param string|string[] $method An HTTP method or an array of HTTP methods */ public function matchMethod($method) { @@ -131,6 +149,10 @@ public function matchAttribute($key, $regexp) */ public function matches(Request $request) { + if ($this->schemes && !in_array($request->getScheme(), $this->schemes)) { + return false; + } + if ($this->methods && !in_array($request->getMethod(), $this->methods)) { return false; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php index 0e1a0f5caf363..d4b1dbc20b81b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php @@ -43,6 +43,25 @@ public function testMethodFixtures() ); } + public function testScheme() + { + $httpRequest = $request = $request = Request::create(''); + $httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on')); + + $matcher = new RequestMatcher(); + $matcher->matchScheme('https'); + $this->assertFalse($matcher->matches($httpRequest)); + $this->assertTrue($matcher->matches($httpsRequest)); + + $matcher->matchScheme('http'); + $this->assertFalse($matcher->matches($httpsRequest)); + $this->assertTrue($matcher->matches($httpRequest)); + + $matcher = new RequestMatcher(); + $this->assertTrue($matcher->matches($httpsRequest)); + $this->assertTrue($matcher->matches($httpRequest)); + } + /** * @dataProvider testHostFixture */ @@ -68,7 +87,8 @@ public function testHostFixture() array('.*\.example\.COM', true), array('\.example\.COM$', true), array('^.*\.example\.COM$', true), - array('.*\.sensio\.COM', false), ); + array('.*\.sensio\.COM', false), + ); } public function testPath()