This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/filter-uri-normalization'
- Loading branch information
53 parents
09f49e2
+
d1304e2
+
d3d5a56
+
533570c
+
6e30cb0
+
82c88d1
+
88c4f3b
+
c96ebb3
+
f0320b1
+
cb0afaa
+
128be4b
+
34cd6a3
+
ec2aab9
+
809bd88
+
77026e3
+
c2796da
+
0ee8602
+
bf4e728
+
cd33c2d
+
32497e5
+
79eb7d0
+
8cc5d58
+
869ab1c
+
436e613
+
ab7d56c
+
6dbeef6
+
84670b1
+
cccfba9
+
2717653
+
b4dd364
+
460818e
+
b5ac15d
+
2d218aa
+
046d669
+
5597f80
+
98dc299
+
01b3149
+
b0177b5
+
74f8270
+
62799e5
+
512426e
+
5a9a928
+
3b3e691
+
6f57ae9
+
116cf37
+
bb991fd
+
1ce1893
+
5000327
+
32beb38
+
fe88f0e
+
da64ec7
+
b52dd7f
+
d9d5638
commit 0ab180c
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Filter | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
use Zend\Filter\AbstractFilter; | ||
use Zend\Filter\Exception\InvalidArgumentException; | ||
use Zend\Uri\UriFactory; | ||
use Zend\Uri\Uri; | ||
use Zend\Uri\Exception\ExceptionInterface as UriException; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Filter | ||
*/ | ||
class UriNormalize extends AbstractFilter | ||
{ | ||
/** | ||
* The default scheme to use when parsing scheme-less URIs | ||
* | ||
* @var string | ||
*/ | ||
protected $defaultScheme = null; | ||
|
||
/** | ||
* Enforced scheme for scheme-less URIs. See setEnforcedScheme docs for info | ||
* | ||
* @var string | ||
*/ | ||
protected $enforcedScheme = null; | ||
|
||
/** | ||
* Sets filter options | ||
* | ||
* @param string|array|\Zend\Config\Config $options | ||
* @return void | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Set the default scheme to use when parsing scheme-less URIs | ||
* | ||
* The scheme used when parsing URIs may affect the specific object used to | ||
* normalize the URI and thus may affect the resulting normalize URI. | ||
* | ||
* @param string $defaultScheme | ||
* @return \Zend\Filter\UriNormalize | ||
*/ | ||
public function setDefaultScheme($defaultScheme) | ||
{ | ||
$this->defaultScheme = $defaultScheme; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set a URI scheme to enforce on schemeless URIs | ||
* | ||
* This allows forcing input values such as 'www.example.com/foo' into | ||
* 'http://www.example.com/foo'. | ||
* | ||
* This should be used with caution, as a standard-compliant URI parser | ||
* would regard 'www.example.com' in the above input URI to be the path and | ||
* not host part of the URI. While this option can assist in solving | ||
* real-world user mishaps, it may yield unexpected results at times. | ||
* | ||
* @param string $enforcedScheme | ||
* @return \Zend\Filter\UriNormalize | ||
*/ | ||
public function setEnforcedScheme($enforcedScheme) | ||
{ | ||
$this->enforcedScheme = $enforcedScheme; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Filter the URL by normalizing it and applying a default scheme if set | ||
* | ||
* @param string $value | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
$defaultScheme = $this->defaultScheme ?: $this->enforcedScheme; | ||
|
||
// Reset default scheme if it is not a known scheme | ||
if (!UriFactory::getRegisteredSchemeClass($defaultScheme)) { | ||
$defaultScheme = null; | ||
} | ||
|
||
try { | ||
$uri = UriFactory::factory($value, $defaultScheme); | ||
if ($this->enforcedScheme && (!$uri->getScheme())) { | ||
$this->enforceScheme($uri); | ||
} | ||
|
||
} catch (UriException $ex) { | ||
// We are unable to parse / enfore scheme with the given config and input | ||
return $value; | ||
} | ||
|
||
$uri->normalize(); | ||
|
||
if (!$uri->isValid()) { | ||
return $value; | ||
} | ||
|
||
return $uri->toString(); | ||
} | ||
|
||
/** | ||
* Enforce the defined scheme on the URI | ||
* | ||
* This will also adjust the host and path parts of the URI as expected in | ||
* the case of scheme-less network URIs | ||
* | ||
* @param Uri $uri | ||
*/ | ||
protected function enforceScheme(Uri $uri) | ||
{ | ||
$path = $uri->getPath(); | ||
if (strpos($path, '/') !== false) { | ||
list($host, $path) = explode('/', $path, 2); | ||
$path = '/' . $path; | ||
} else { | ||
$host = $path; | ||
$path = ''; | ||
} | ||
|
||
// We have nothing to do if we have no host | ||
if (!$host) { | ||
return; | ||
} | ||
|
||
$uri->setScheme($this->enforcedScheme) | ||
->setHost($host) | ||
->setPath($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Filter | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use Zend\Filter\UriNormalize; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Filter | ||
* @subpackage UnitTests | ||
* @group Zend_Filter | ||
*/ | ||
class UriNormalizeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider abnormalUriProvider | ||
*/ | ||
public function testUrisAreNormalized($url, $expected) | ||
{ | ||
$filter = new UriNormalize(); | ||
$result = $filter->filter($url); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testDefaultSchemeAffectsNormalization() | ||
{ | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
/** | ||
* @dataProvider enforcedSchemeTestcaseProvider | ||
*/ | ||
public function testEnforcedScheme($scheme, $input, $expected) | ||
{ | ||
$filter = new UriNormalize(array('enforcedScheme' => $scheme)); | ||
$result = $filter->filter($input); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public static function abnormalUriProvider() | ||
{ | ||
return array( | ||
array('http://www.example.com', 'http://www.example.com/'), | ||
array('hTTp://www.example.com/ space', 'http://www.example.com/%20space'), | ||
array('file:///www.example.com/foo/bar', 'file:///www.example.com/foo/bar'), // this should not be affected | ||
array('file:///home/shahar/secret/../../otherguy/secret', 'file:///home/otherguy/secret'), | ||
array('https://www.example.com:443/hasport', 'https://www.example.com/hasport'), | ||
array('/foo/bar?q=%711', '/foo/bar?q=q1'), // no scheme enforced | ||
); | ||
} | ||
|
||
public static function enforcedSchemeTestcaseProvider() | ||
{ | ||
return array( | ||
array('ftp', 'http://www.example.com', 'http://www.example.com/'), // no effect - this one has a scheme | ||
array('mailto', 'mailto:shahar@example.com', 'mailto:shahar@example.com'), | ||
array('http', 'www.example.com/foo/bar?q=q', 'http://www.example.com/foo/bar?q=q'), | ||
array('ftp', 'www.example.com/path/to/file.ext', 'ftp://www.example.com/path/to/file.ext'), | ||
array('http', '/just/a/path', '/just/a/path') // cannot be enforced, no host | ||
); | ||
} | ||
} |