Skip to content

Commit

Permalink
Accept URLs formatted as //domain/path
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Dec 2, 2014
1 parent a0d3bf9 commit 0c05115
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public static function getLossyUrl($url)
*/
public static function isLookLikeUrl($url)
{
return preg_match('~^(ftp|news|http|https)?://(.*)$~D', $url, $matches) !== 0
&& strlen($matches[2]) > 0;
return preg_match('~^((ftp|news|http|https)?:)?//(.*)$~D', $url, $matches) !== 0
&& strlen($matches[3]) > 0;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion tests/PHPUnit/Unit/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ public function getUrls()
array('news://www.pi-wik.org', true),
array('https://www.tëteâ.org', true),
array('http://汉语/漢語.cn', true), //chinese

// valid network-path reference RFC3986
array('//piwik.org', true),
array('//piwik/hello?world=test&test', true),
array('//piwik.org/hello?world=test&test', true),

// invalid urls
array('it doesnt look like url', false),
array('/index?page=test', false),
array('http:/index?page=test', false),
array('http/index?page=test', false),
array('test.html', false),
array('/\/\/\/\/\/\\\http://test.com////', false),
array('jmleslangues.php', false),
Expand All @@ -46,7 +54,7 @@ public function getUrls()
*/
public function testIsUrl($url, $isValid)
{
$this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url));
$this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url), "$url failed test");
}

/**
Expand Down Expand Up @@ -196,6 +204,8 @@ public function test_getHostFromUrl()
$this->assertEquals('', UrlHelper::getHostFromUrl(null));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost/path'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('//localhost/path'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('//localhost/path?test=test2'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('localhost/path'));
$this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('sub.localhost/path'));
$this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('http://sub.localhost/path/?query=test'));
Expand Down

0 comments on commit 0c05115

Please sign in to comment.