Skip to content

Commit

Permalink
Percent-encode unreserved characters in URL path (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
webignition committed Oct 9, 2018
1 parent 7c0bf4d commit d26f181
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 150 deletions.
3 changes: 3 additions & 0 deletions src/NormalisedUrl/Path/Normaliser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace webignition\NormalisedUrl\Path;

use webignition\Url\PercentEncoder;

class Normaliser
{
/**
Expand All @@ -14,6 +16,7 @@ class Normaliser
*/
public function __construct($path)
{
$path = PercentEncoder::encodeUnreservedCharacters($path);
$this->path = (string)$path;
$this->normalise();
}
Expand Down
2 changes: 1 addition & 1 deletion src/NormalisedUrl/Path/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct($path)
{
parent::__construct($path);

$normaliser = new Normaliser($path);
$normaliser = new Normaliser(parent::get());

$this->set($normaliser->get());
}
Expand Down
26 changes: 0 additions & 26 deletions src/Url/Encoder.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Url/Path/Encoder.php

This file was deleted.

4 changes: 4 additions & 0 deletions src/Url/Path/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace webignition\Url\Path;

use webignition\Url\PercentEncoder;

/**
* Represents the path part of a URL
*/
Expand All @@ -19,6 +21,8 @@ class Path
*/
public function __construct($path)
{
$path = PercentEncoder::encodeUnreservedCharacters($path);

$this->set($path);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Url/PercentEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace webignition\Url;

class PercentEncoder
{
private static $charUnreserved = 'a-zA-Z0-9_\-\.~';
private static $charSubDelims = '!\$&\'\(\)\*\+,;=';

public static function encodeUnreservedCharacters($path)
{
return preg_replace_callback(
'/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/',
function (array $match) {
return rawurlencode($match[0]);
},
$path
);
}
}
4 changes: 4 additions & 0 deletions tests/DataProvider/PathNormalisationDataProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public function pathNormalisationDataProvider()
'path' => '/one//two',
'expectedNormalisedPath' => '/one//two',
],
'percent-encode unicode characters' => [
'path' => '/Nattō',
'expectedNormalisedPath' => '/Natt%C5%8D',
],
];
}
}
78 changes: 0 additions & 78 deletions tests/Url/EncoderTest.php

This file was deleted.

25 changes: 0 additions & 25 deletions tests/Url/Path/EncoderTest.php

This file was deleted.

16 changes: 12 additions & 4 deletions tests/Url/Path/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class PathTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider createDataProvider
*
* @param $pathString
* @param string|null $path
* @param string $expectedPath
*/
public function testCreate($pathString)
public function testCreate($path, $expectedPath)
{
$path = new Path($pathString);
$path = new Path($path);

$this->assertEquals($pathString, (string)$path);
$this->assertEquals($expectedPath, (string)$path);
}

/**
Expand All @@ -26,12 +27,19 @@ public function createDataProvider()
return [
'null' => [
'pathString' => null,
'expectedPath' => '',
],
'empty' => [
'pathString' => '',
'expectedPath' => '',
],
'non-empty' => [
'pathString' => '/foo',
'expectedPath' => '/foo',
],
'percent-encode unicode characters' => [
'path' => '/Nattō',
'expectedPath' => '/Natt%C5%8D',
],
];
}
Expand Down

0 comments on commit d26f181

Please sign in to comment.