Merge pull request #1022 from silverstripe-iterators/pulls/redirector-proto-relative

Pulls/redirector proto relative
This commit is contained in:
Damian Mooyman 2014-06-13 16:01:20 +12:00
commit 1a85c7a1ec
2 changed files with 19 additions and 1 deletions

View File

@ -107,7 +107,11 @@ class RedirectorPage extends Page {
parent::onBeforeWrite(); parent::onBeforeWrite();
// Prefix the URL with "http://" if no prefix is found // Prefix the URL with "http://" if no prefix is found
if($this->ExternalURL && (strpos($this->ExternalURL, '://') === false)) { if(
$this->ExternalURL
&& !parse_url($this->ExternalURL, PHP_URL_SCHEME)
&& !preg_match('#^//#', $this->ExternalURL)
) {
$this->ExternalURL = 'http://' . $this->ExternalURL; $this->ExternalURL = 'http://' . $this->ExternalURL;
} }
} }

View File

@ -50,4 +50,18 @@ class RedirectorPageTest extends FunctionalTest {
$page->write(); $page->write();
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!'); $this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!');
} }
public function testAllowsProtocolRelative() {
$noProtocol = new RedirectorPage(array('ExternalURL' => 'mydomain.com'));
$noProtocol->write();
$this->assertEquals('http://mydomain.com', $noProtocol->ExternalURL);
$protocolAbsolute = new RedirectorPage(array('ExternalURL' => 'http://mydomain.com'));
$protocolAbsolute->write();
$this->assertEquals('http://mydomain.com', $protocolAbsolute->ExternalURL);
$protocolRelative = new RedirectorPage(array('ExternalURL' => '//mydomain.com'));
$protocolRelative->write();
$this->assertEquals('//mydomain.com', $protocolRelative->ExternalURL);
}
} }