From 0c051152b6d7e21450b76dad27ee8accbe8b26a4 Mon Sep 17 00:00:00 2001 From: mattab <matthieu.aubry@gmail.com> Date: Tue, 2 Dec 2014 13:46:25 +1300 Subject: [PATCH] Accept URLs formatted as //domain/path Fixes https://github.com/piwik/piwik/pull/6652 --- core/UrlHelper.php | 4 ++-- tests/PHPUnit/Unit/UrlHelperTest.php | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/core/UrlHelper.php b/core/UrlHelper.php index b1dfa46d12..5efba99996 100644 --- a/core/UrlHelper.php +++ b/core/UrlHelper.php @@ -101,8 +101,8 @@ class UrlHelper */ 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; } /** diff --git a/tests/PHPUnit/Unit/UrlHelperTest.php b/tests/PHPUnit/Unit/UrlHelperTest.php index 9a006c7a40..dc4a97047b 100644 --- a/tests/PHPUnit/Unit/UrlHelperTest.php +++ b/tests/PHPUnit/Unit/UrlHelperTest.php @@ -28,9 +28,17 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase 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), @@ -46,7 +54,7 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase */ public function testIsUrl($url, $isValid) { - $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url)); + $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url), "$url failed test"); } /** @@ -196,6 +204,8 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase $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')); -- GitLab