From 65e916590534596770e81d079677acfa7ca6218b Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 16 Sep 2008 20:37:46 +0000 Subject: [PATCH] BUGFIX Allowing HTTPRequest::match() to match rules with extensions (e.g. /sitemap.xml used for GoogleSitemap) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@62471 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/HTTPRequest.php | 10 +++++++--- tests/RequestHandlingTest.php | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/control/HTTPRequest.php b/core/control/HTTPRequest.php index 3cf32378e..fa15651c9 100644 --- a/core/control/HTTPRequest.php +++ b/core/control/HTTPRequest.php @@ -233,6 +233,7 @@ class HTTPRequest extends Object implements ArrayAccess { $pattern = str_replace('//', '/', $pattern); $patternParts = explode('/', $pattern); + } else { $patternParts = explode('/', $pattern); $shiftCount = sizeof($patternParts); @@ -240,10 +241,9 @@ class HTTPRequest extends Object implements ArrayAccess { $matched = true; $arguments = array(); - foreach($patternParts as $i => $part) { $part = trim($part); - + // Match a variable if(isset($part[0]) && $part[0] == '$') { // A variable ending in ! is required @@ -262,7 +262,11 @@ class HTTPRequest extends Object implements ArrayAccess { if($part == '$Controller' && !class_exists($arguments['Controller'])) { return false; } - + + // Literal parts with extension + } else if(isset($this->dirParts[$i]) && $this->dirParts[$i] . '.' . $this->extension == $part) { + continue; + // Literal parts must always be there } else if(!isset($this->dirParts[$i]) || $this->dirParts[$i] != $part) { return false; diff --git a/tests/RequestHandlingTest.php b/tests/RequestHandlingTest.php index 14bde7282..9aa6fff4a 100644 --- a/tests/RequestHandlingTest.php +++ b/tests/RequestHandlingTest.php @@ -56,6 +56,16 @@ class RequestHandlingTest extends SapphireTest { $response = Director::test("testBadBase/TestForm/fields/MyField"); $this->assertNotEquals("MyField requested", $response->getBody()); } + + function testBaseWithExtension() { + /* Rules with an extension always default to the index() action */ + $response = Director::test("testBaseWithExtension/virtualfile.xml"); + $this->assertEquals("This is the controller", $response->getBody()); + + /* Without the extension, the methodname should be matched */ + $response = Director::test("testBaseWithExtension/virtualfile"); + $this->assertEquals("This is the virtualfile method", $response->getBody()); + } } /** @@ -73,6 +83,12 @@ Director::addRules(50, array( // By default, the entire URL will be shifted off. This creates a bit of backward-incompatability, but makes the // URL rules much more explicit. 'testBadBase/$Action/$ID/$OtherID' => "RequestHandlingTest_Controller", + + // Rules with an extension always default to the index() action + 'testBaseWithExtension/virtualfile.xml' => "RequestHandlingTest_Controller", + + // Without the extension, the methodname should be matched + 'testBaseWithExtension//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller", )); /** @@ -96,6 +112,10 @@ class RequestHandlingTest_Controller extends Controller { return "\$this->urlParams can be used, for backward compatibility: " . $this->urlParams['ID'] . ', ' . $this->urlParams['OtherID']; } + function virtualfile($request) { + return "This is the virtualfile method"; + } + function TestForm() { return new RequestHandlingTest_Form($this, "TestForm", new FieldSet( new RequestHandlingTest_FormField("MyField")