first commit
This commit is contained in:
7
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/LICENSE
vendored
Normal file
7
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/LICENSE
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
Copyright (c) 2013 Glen Scott
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
51
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/README.md
vendored
Normal file
51
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/README.md
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
# Syntax based normalization of URI's
|
||||
|
||||
This normalizes URI's based on the specification RFC 3986
|
||||
https://tools.ietf.org/html/rfc3986
|
||||
|
||||
Example usage:
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
$url = 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d';
|
||||
$un = new URL\Normalizer( $url );
|
||||
echo $un->normalize();
|
||||
|
||||
// result: "example://a/b/c/%7Bfoo%7D"
|
||||
|
||||
The normalization process preserves semantics so, for example, the following URL's are all equivalent:
|
||||
|
||||
HTTP://www.Example.com/ and http://www.example.com/
|
||||
http://www.example.com/a%c2%b1b and http://www.example.com/a%C2%B1b
|
||||
http://www.example.com/%7Eusername/ and http://www.example.com/~username/
|
||||
http://www.example.com and http://www.example.com/
|
||||
http://www.example.com:80/bar.html and http://www.example.com/bar.html
|
||||
http://www.example.com/../a/b/../c/./d.html and http://www.example.com/a/c/d.html
|
||||
http://www.example.com/?array[key]=value and http://www.example.com/?array%5Bkey%5D=value
|
||||
|
||||
The following normalizations are performed:
|
||||
|
||||
1. Converting the scheme and host to lower case
|
||||
2. Capitalizing letters in escape sequences
|
||||
3. Decoding percent-encoded octets of unreserved characters
|
||||
4. Adding trailing /
|
||||
5. Removing the default port
|
||||
6. Removing dot-segments
|
||||
|
||||
For more information about these normalizations, please see the following Wikipedia article:
|
||||
|
||||
http://en.wikipedia.org/wiki/URL_normalization#Normalizations_that_Preserve_Semantics
|
||||
|
||||
For license information, please see LICENSE file.
|
||||
|
||||
## Options
|
||||
|
||||
Two options are available when normalizing URLs which are disabled by default:
|
||||
|
||||
1. Remove empty delimiters. Enabling this option would normalize http://www.example.com/? to http://www.example.com/ Currently, only the query string delimiter (?) is supported by this option.
|
||||
2. Sort query parameters. Enabling this option sorts the query parameters by key alphabetically. For example, http://www.example.com/?c=3&b=2&a=1 becomes http://www.example.com/?a=1&b=2&c=3
|
||||
|
||||
## TODO
|
||||
|
||||
Add further scheme-based normalization steps, as detailed in section 6.2.3 of the RFC.
|
||||
20
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/composer.json
vendored
Normal file
20
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/composer.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "glenscott/url-normalizer",
|
||||
"description": "Syntax based normalization of URL's",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Glen Scott",
|
||||
"email": "glen@glenscott.co.uk"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"ext-mbstring": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"URL\\" : "src/URL"
|
||||
}
|
||||
}
|
||||
}
|
||||
389
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/src/URL/Normalizer.php
vendored
Normal file
389
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/src/URL/Normalizer.php
vendored
Normal file
@@ -0,0 +1,389 @@
|
||||
<?php
|
||||
|
||||
namespace URL;
|
||||
|
||||
/**
|
||||
* Syntax based normalization of URI's
|
||||
*
|
||||
* This normalises URI's based on the specification RFC 3986
|
||||
* https://tools.ietf.org/html/rfc3986
|
||||
*
|
||||
* Example usage:
|
||||
* <code>
|
||||
* require_once 'Normalizer.php';
|
||||
*
|
||||
* $url = 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d';
|
||||
* $un = new URLNormalizer();
|
||||
* $un->setUrl( $url );
|
||||
* echo $un->normalize();
|
||||
*
|
||||
* // result: "example://a/b/c/%7Bfoo%7D"
|
||||
* </code>
|
||||
*
|
||||
* @author Glen Scott <glen@glenscott.co.uk>
|
||||
*/
|
||||
class Normalizer
|
||||
{
|
||||
private $url;
|
||||
private $scheme;
|
||||
private $host;
|
||||
private $port;
|
||||
private $user;
|
||||
private $pass;
|
||||
private $path;
|
||||
private $query;
|
||||
private $fragment;
|
||||
private $default_scheme_ports = array( 'http:' => 80, 'https:' => 443, );
|
||||
private $components = array( 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment', );
|
||||
private $remove_empty_delimiters;
|
||||
private $sort_query_params;
|
||||
|
||||
/**
|
||||
* Does the original URL have a ? query delimiter
|
||||
*/
|
||||
private $query_delimiter;
|
||||
|
||||
public function __construct($url = null, $remove_empty_delimiters = false, $sort_query_params = false)
|
||||
{
|
||||
if ($url) {
|
||||
$this->setUrl($url);
|
||||
}
|
||||
|
||||
$this->remove_empty_delimiters = $remove_empty_delimiters;
|
||||
$this->sort_query_params = $sort_query_params;
|
||||
}
|
||||
|
||||
private function getQuery($query)
|
||||
{
|
||||
$qs = array();
|
||||
foreach ($query as $qk => $qv) {
|
||||
if (is_array($qv)) {
|
||||
$qs[rawurldecode($qk)] = $this->getQuery($qv);
|
||||
} else {
|
||||
$qs[rawurldecode($qk)] = rawurldecode($qv);
|
||||
}
|
||||
}
|
||||
return $qs;
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
|
||||
if (strpos($this->url, '?') !== false) {
|
||||
$this->query_delimiter = true;
|
||||
} else {
|
||||
$this->query_delimiter = false;
|
||||
}
|
||||
|
||||
// parse URL into respective parts
|
||||
$url_components = $this->mbParseUrl($this->url);
|
||||
|
||||
if (! $url_components) {
|
||||
// Reset URL
|
||||
$this->url = '';
|
||||
|
||||
// Flush properties
|
||||
foreach ($this->components as $key) {
|
||||
if (property_exists($this, $key)) {
|
||||
$this->$key = '';
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} else {
|
||||
// Update properties
|
||||
foreach ($url_components as $key => $value) {
|
||||
if (property_exists($this, $key)) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Flush missing components
|
||||
$missing_components = array_diff(
|
||||
array_values($this->components),
|
||||
array_keys($url_components)
|
||||
);
|
||||
|
||||
foreach ($missing_components as $key) {
|
||||
if (property_exists($this, $key)) {
|
||||
$this->$key = '';
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function normalize()
|
||||
{
|
||||
|
||||
// URI Syntax Components
|
||||
// scheme authority path query fragment
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3
|
||||
|
||||
// Scheme
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.1
|
||||
|
||||
if ($this->scheme) {
|
||||
// Converting the scheme to lower case
|
||||
$this->scheme = strtolower($this->scheme) . ':';
|
||||
}
|
||||
|
||||
// Authority
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.2
|
||||
|
||||
$authority = '';
|
||||
if ($this->host) {
|
||||
$authority .= '//';
|
||||
|
||||
// User Information
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.2.1
|
||||
|
||||
if ($this->user) {
|
||||
if ($this->pass) {
|
||||
$authority .= $this->user . ':' . $this->pass . '@';
|
||||
} else {
|
||||
$authority .= $this->user . '@';
|
||||
}
|
||||
}
|
||||
|
||||
// Host
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.2.2
|
||||
|
||||
// Converting the host to lower case
|
||||
if (mb_detect_encoding($this->host) == 'UTF-8') {
|
||||
$authority .= mb_strtolower($this->host, 'UTF-8');
|
||||
} else {
|
||||
$authority .= strtolower($this->host);
|
||||
}
|
||||
|
||||
// Port
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.2.3
|
||||
|
||||
// Removing the default port
|
||||
if (isset($this->default_scheme_ports[$this->scheme] )
|
||||
&& $this->port == $this->default_scheme_ports[$this->scheme]) {
|
||||
$this->port = '';
|
||||
}
|
||||
|
||||
if ($this->port) {
|
||||
$authority .= ':' . $this->port;
|
||||
}
|
||||
}
|
||||
|
||||
// Path
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.3
|
||||
|
||||
if ($this->path) {
|
||||
$this->path = $this->removeAdditionalPathPrefixSlashes($this->path);
|
||||
$this->path = $this->removeDotSegments($this->path);
|
||||
$this->path = $this->urlDecodeUnreservedChars($this->path);
|
||||
$this->path = $this->urlDecodeReservedSubDelimChars($this->path);
|
||||
} elseif ($this->url) {
|
||||
// Add default path only when valid URL is present
|
||||
// Adding trailing /
|
||||
$this->path = '/';
|
||||
}
|
||||
|
||||
// Query
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.4
|
||||
|
||||
if ($this->query) {
|
||||
$query = $this->parseStr($this->query);
|
||||
|
||||
//encodes every parameter correctly
|
||||
$qs = $this->getQuery($query);
|
||||
|
||||
$this->query = '?';
|
||||
|
||||
if ($this->sort_query_params) {
|
||||
ksort($qs);
|
||||
}
|
||||
|
||||
foreach ($qs as $key => $val) {
|
||||
if (strlen($this->query) > 1) {
|
||||
$this->query .= '&';
|
||||
}
|
||||
|
||||
if (is_array($val)) {
|
||||
for ($i = 0; $i < count($val); $i++) {
|
||||
if ($i > 0) {
|
||||
$this->query .= '&';
|
||||
}
|
||||
$this->query .= rawurlencode($key) . '=' . rawurlencode($val[$i]);
|
||||
}
|
||||
} else {
|
||||
$this->query .= rawurlencode($key) . '=' . rawurlencode($val);
|
||||
}
|
||||
}
|
||||
|
||||
// Fix http_build_query adding equals sign to empty keys
|
||||
$this->query = str_replace('=&', '&', rtrim($this->query, '='));
|
||||
} else {
|
||||
if ($this->query_delimiter && ! $this->remove_empty_delimiters) {
|
||||
$this->query = '?';
|
||||
}
|
||||
}
|
||||
|
||||
// Fragment
|
||||
// @link https://tools.ietf.org/html/rfc3986#section-3.5
|
||||
|
||||
if ($this->fragment) {
|
||||
$this->fragment = rawurldecode($this->fragment);
|
||||
$this->fragment = rawurlencode($this->fragment);
|
||||
$this->fragment = '#' . $this->fragment;
|
||||
}
|
||||
|
||||
$this->setUrl($this->scheme . $authority . $this->path . $this->query . $this->fragment);
|
||||
|
||||
return $this->getUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path segment normalization
|
||||
* https://tools.ietf.org/html/rfc3986#section-5.2.4
|
||||
*/
|
||||
public function removeDotSegments($path)
|
||||
{
|
||||
$new_path = '';
|
||||
|
||||
while (! empty($path)) {
|
||||
// A
|
||||
$pattern_a = '!^(\.\./|\./)!x';
|
||||
$pattern_b_1 = '!^(/\./)!x';
|
||||
$pattern_b_2 = '!^(/\.)$!x';
|
||||
$pattern_c = '!^(/\.\./|/\.\.)!x';
|
||||
$pattern_d = '!^(\.|\.\.)$!x';
|
||||
$pattern_e = '!(/*[^/]*)!x';
|
||||
|
||||
if (preg_match($pattern_a, $path)) {
|
||||
// remove prefix from $path
|
||||
$path = preg_replace($pattern_a, '', $path);
|
||||
} elseif (preg_match($pattern_b_1, $path, $matches) || preg_match($pattern_b_2, $path, $matches)) {
|
||||
$path = preg_replace("!^" . $matches[1] . "!", '/', $path);
|
||||
} elseif (preg_match($pattern_c, $path, $matches)) {
|
||||
$path = preg_replace('!^' . preg_quote($matches[1], '!') . '!x', '/', $path);
|
||||
|
||||
// remove the last segment and its preceding "/" (if any) from output buffer
|
||||
$new_path = preg_replace('!/([^/]+)$!x', '', $new_path);
|
||||
} elseif (preg_match($pattern_d, $path)) {
|
||||
$path = preg_replace($pattern_d, '', $path);
|
||||
} else {
|
||||
if (preg_match($pattern_e, $path, $matches)) {
|
||||
$first_path_segment = $matches[1];
|
||||
|
||||
$path = preg_replace('/^' . preg_quote($first_path_segment, '/') . '/', '', $path, 1);
|
||||
|
||||
$new_path .= $first_path_segment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $new_path;
|
||||
}
|
||||
|
||||
public function getScheme()
|
||||
{
|
||||
return $this->scheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode unreserved characters
|
||||
*
|
||||
* @link https://tools.ietf.org/html/rfc3986#section-2.3
|
||||
*/
|
||||
public function urlDecodeUnreservedChars($string)
|
||||
{
|
||||
$string = rawurldecode($string);
|
||||
$string = rawurlencode($string);
|
||||
$string = str_replace(array( '%2F', '%3A', '%40' ), array( '/', ':', '@' ), $string);
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode reserved sub-delims
|
||||
*
|
||||
* @link https://tools.ietf.org/html/rfc3986#section-2.2
|
||||
*/
|
||||
public function urlDecodeReservedSubDelimChars($string)
|
||||
{
|
||||
return str_replace(
|
||||
array( '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D' ),
|
||||
array( '!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=' ),
|
||||
$string
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replacement for PHP's parse_string which does not deal with spaces or dots in key names
|
||||
*
|
||||
* @param string $string URL query string
|
||||
* @return array key value pairs
|
||||
*/
|
||||
private function parseStr($string)
|
||||
{
|
||||
$params = array();
|
||||
|
||||
$pairs = explode('&', $string);
|
||||
|
||||
foreach ($pairs as $pair) {
|
||||
if (! $pair) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$var = explode('=', $pair, 2);
|
||||
$val = ( isset( $var[1] ) ? $var[1] : '' );
|
||||
|
||||
if (isset($params[$var[0]])) {
|
||||
if (is_array($params[$var[0]])) {
|
||||
$params[$var[0]][] = $val;
|
||||
} else {
|
||||
$params[$var[0]] = array($params[$var[0]], $val);
|
||||
}
|
||||
} else {
|
||||
$params[$var[0]] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
private function mbParseUrl($url)
|
||||
{
|
||||
$result = array();
|
||||
|
||||
// Build arrays of values we need to decode before parsing
|
||||
$entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D');
|
||||
$replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "$", ",", "/", "?", "#", "[", "]");
|
||||
|
||||
// Create encoded URL with special URL characters decoded so it can be parsed
|
||||
// All other characters will be encoded
|
||||
$encodedURL = str_replace($entities, $replacements, urlencode($url));
|
||||
|
||||
// Parse the encoded URL
|
||||
$encodedParts = parse_url($encodedURL);
|
||||
|
||||
// Now, decode each value of the resulting array
|
||||
if ($encodedParts) {
|
||||
foreach ($encodedParts as $key => $value) {
|
||||
$result[$key] = urldecode(str_replace($replacements, $entities, $value));
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Converts ////foo to /foo within each path segment
|
||||
*/
|
||||
private function removeAdditionalPathPrefixSlashes($path)
|
||||
{
|
||||
return preg_replace('/(\/)+/', '/', $path);
|
||||
}
|
||||
}
|
||||
84
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/test-client.php
vendored
Normal file
84
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/test-client.php
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
echo '<pre>';
|
||||
|
||||
require_once 'src/URL/Normalizer.php';
|
||||
|
||||
$un = new URL\Normalizer();
|
||||
|
||||
test('eXAMPLE://a/./b/../b/%63/%7bfoo%7d', 'example://a/b/c/%7Bfoo%7D');
|
||||
test('http://www.example.com', 'http://www.example.com/');
|
||||
test('http://www.yahoo.com/%a1', 'http://www.yahoo.com/%A1');
|
||||
|
||||
test('HTTP://www.Example.com/', 'http://www.example.com/');
|
||||
test('http://www.example.com/a%c2%b1b', 'http://www.example.com/a%C2%B1b');
|
||||
test('http://www.example.com/%7Eusername/', 'http://www.example.com/~username/');
|
||||
test('http://www.example.com:80/bar.html', 'http://www.example.com/bar.html');
|
||||
|
||||
test('http://www.example.com/../a/b/../c/./d.html', 'http://www.example.com/a/c/d.html');
|
||||
test('../', '' );
|
||||
test('./', '' );
|
||||
test('/./', '/' );
|
||||
test('/.', '/' );
|
||||
test('/a/b/c/./../../g', '/a/g' );
|
||||
test('mid/content=5/../6', 'mid/6' );
|
||||
test('/foo/bar/.', '/foo/bar/' );
|
||||
test('/foo/bar/./', '/foo/bar/' );
|
||||
test('/foo/bar/..', '/foo/' );
|
||||
test('/foo/bar/../', '/foo/' );
|
||||
test('/foo/bar/../baz', '/foo/baz' );
|
||||
test('/foo/bar/../..', '/');
|
||||
test('/foo/bar/../../' , '/');
|
||||
test('/foo/bar/../../baz' , '/baz');
|
||||
//test('/foo/bar/../../../baz' , '/../baz');
|
||||
test('a/./b/../b/', 'a/b/' );
|
||||
test('.', '' );
|
||||
test('..', '' );
|
||||
|
||||
test('%63', 'c');
|
||||
test('%63/%7b', 'c/%7B');
|
||||
|
||||
test('http://example.com', 'http://example.com/');
|
||||
test('http://example.com/', 'http://example.com/');
|
||||
test('http://example.com:/', 'http://example.com/');
|
||||
test('http://example.com:80/', 'http://example.com/');
|
||||
|
||||
test('https://example.com', 'https://example.com/');
|
||||
test('https://example.com/', 'https://example.com/');
|
||||
test('https://example.com:/', 'https://example.com/');
|
||||
test('https://example.com:443/', 'https://example.com/');
|
||||
|
||||
test('http://fancysite.nl/links/doit.pl?id=2029', 'http://fancysite.nl/links/doit.pl?id=2029');
|
||||
test('http://example.com/index.html#fragment', 'http://example.com/index.html#fragment');
|
||||
test('http://example.com:81/index.html', 'http://example.com:81/index.html');
|
||||
test('HtTp://User:Pass@www.ExAmPle.com:80/Blah', 'http://User:Pass@www.example.com/Blah');
|
||||
test('/test:2/', '');
|
||||
test('mailto:mail@example.com', 'mailto:mail@example.com');
|
||||
test('http://user@example.com/', 'http://user@example.com/');
|
||||
test('http://example.com/path/?query#fragment', 'http://example.com/path/?query#fragment');
|
||||
test('http://example.com/path/?q1&q2&q3&q4', 'http://example.com/path/?q1&q2&q3&q4');
|
||||
test('http://example.com:400/', 'http://example.com:400/');
|
||||
test('http://example.com/', 'http://example.com/');
|
||||
|
||||
test('http://example.com/path/?query=space value', 'http://example.com/path/?query=space%20value');
|
||||
|
||||
test('http://www.example.com/?array[key]=value', 'http://www.example.com/?array%5Bkey%5D=value');
|
||||
|
||||
/**
|
||||
* Test URL Normalization
|
||||
*
|
||||
* @param string $input URL to normalize
|
||||
* @param string $expected Anticipated result of normalization
|
||||
* @return void
|
||||
* @author emojka
|
||||
**/
|
||||
function test($input, $expected) {
|
||||
global $un;
|
||||
$un->setUrl($input);
|
||||
$result = $un->normalize();
|
||||
if ($result === $expected) {
|
||||
printf("✔ %s → %s\n", $input, $result);
|
||||
} else {
|
||||
printf("%s ✘ %s → %s\n", $expected, $input, $result);
|
||||
}
|
||||
}
|
||||
320
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/tests/URL/NormalizerTest.php
vendored
Normal file
320
wp-content/plugins/pixelyoursite/vendor/glenscott/url-normalizer/tests/URL/NormalizerTest.php
vendored
Normal file
@@ -0,0 +1,320 @@
|
||||
<?php
|
||||
|
||||
require_once dirname( __FILE__ ) . '/../../src/URL/Normalizer.php';
|
||||
|
||||
use URL\Normalizer;
|
||||
|
||||
class NormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $fixture;
|
||||
private $test_url = 'http://www.yahoo.com/';
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->fixture = new Normalizer();
|
||||
}
|
||||
|
||||
public function testClassCanBeInstantiated() {
|
||||
$this->assertTrue( is_object( $this->fixture ) );
|
||||
}
|
||||
|
||||
public function testObjectIsOfCorrectType() {
|
||||
$this->assertTrue( get_class( $this->fixture ) == 'URL\Normalizer' );
|
||||
}
|
||||
|
||||
public function testObjectHasGetUrlMethod() {
|
||||
$this->assertTrue( method_exists( $this->fixture, 'getUrl' ) );
|
||||
}
|
||||
|
||||
public function testSetUrlFromConstructor() {
|
||||
$this->fixture = new Normalizer( 'http://www.example.com/' );
|
||||
$this->assertTrue( $this->fixture->getUrl() == 'http://www.example.com/' );
|
||||
}
|
||||
|
||||
public function testSetUrl() {
|
||||
$this->fixture->setUrl( $this->test_url );
|
||||
$this->assertTrue( $this->fixture->getUrl() == $this->test_url );
|
||||
}
|
||||
|
||||
public function testObjectHasGetSchemeMethod() {
|
||||
$this->assertTrue( method_exists( $this->fixture, 'getScheme' ) );
|
||||
}
|
||||
|
||||
public function testSchemeExtractedFromUrl() {
|
||||
$this->fixture->setUrl( $this->test_url );
|
||||
$this->assertTrue( $this->fixture->getScheme() == 'http' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provider
|
||||
*/
|
||||
public function testUrlsAreNormalised( $url, $normalised_url ) {
|
||||
$this->fixture->setUrl( $url );
|
||||
|
||||
$this->assertEquals( $normalised_url, $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provider
|
||||
*/
|
||||
public function testUrlsAreNormalisedAgain( $url, $normalised_url ) {
|
||||
$this->fixture->setUrl( $url );
|
||||
|
||||
// normalize once
|
||||
$this->fixture->normalize();
|
||||
|
||||
// then normalize again
|
||||
$this->assertEquals( $normalised_url, $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function provider() {
|
||||
// tests from http://en.wikipedia.org/wiki/URL_normalization#Normalizations_that_Preserve_Semantics
|
||||
return array(
|
||||
array( 'HTTP://www.Example.com/', 'http://www.example.com/' ),
|
||||
array( 'http://www.example.com/a%c2%b1b', 'http://www.example.com/a%C2%B1b' ),
|
||||
array( 'http://www.example.com/%7Eusername/', 'http://www.example.com/~username/' ),
|
||||
array( 'http://www.example.com', 'http://www.example.com/' ),
|
||||
array( 'http://www.example.com:80/bar.html', 'http://www.example.com/bar.html' ),
|
||||
array( 'http://www.example.com/../a/b/../c/./d.html', 'http://www.example.com/a/c/d.html' ),
|
||||
array( 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d', 'example://a/b/c/%7Bfoo%7D' ),
|
||||
);
|
||||
}
|
||||
|
||||
public function testCaseIsNormalization() {
|
||||
$this->fixture->setUrl( 'http://www.yahoo.com/%a1' );
|
||||
$this->assertEquals( 'http://www.yahoo.com/%A1', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dotSegmentProvider
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc3986#section-5.2.4
|
||||
*/
|
||||
public function testRemoveDotSegments( $path, $normalised_path ) {
|
||||
$this->assertEquals( $normalised_path, $this->fixture->removeDotSegments( $path ) );
|
||||
}
|
||||
|
||||
public function dotSegmentProvider() {
|
||||
return array(
|
||||
array( '../', '' ),
|
||||
array( './', '' ),
|
||||
array( '/./', '/' ),
|
||||
array( '/.', '/' ),
|
||||
array( '/a/b/c/./../../g', '/a/g' ),
|
||||
array( 'mid/content=5/../6', 'mid/6' ),
|
||||
array( '/foo/bar/.', '/foo/bar/' ),
|
||||
array( '/foo/bar/./', '/foo/bar/' ),
|
||||
array( '/foo/bar/..', '/foo/' ),
|
||||
array( '/foo/bar/../', '/foo/' ),
|
||||
array( '/foo/bar/../baz', '/foo/baz' ),
|
||||
array('/foo/bar/../..', '/'),
|
||||
array('/foo/bar/../../' , '/'),
|
||||
array('/foo/bar/../../baz' , '/baz'),
|
||||
//array('/foo/bar/../../../baz' , '/../baz'),
|
||||
array( 'a/./b/../b/', 'a/b/' ),
|
||||
array( '.', '' ),
|
||||
array( '..', '' ),
|
||||
);
|
||||
}
|
||||
|
||||
public function testDecodingUnreservedUrlChars() {
|
||||
$this->assertEquals( 'c', $this->fixture->urlDecodeUnreservedChars( '%63' ) );
|
||||
$this->assertEquals( 'c/%7B', $this->fixture->urlDecodeUnreservedChars( '%63/%7b' ) );
|
||||
$this->assertEquals( 'eXAMPLE://a/./b/../b/c/%7Bfoo%7D', $this->fixture->urlDecodeUnreservedChars( 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider schemeData
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc3986#section-6.2.3
|
||||
*/
|
||||
public function testSchemeBasedNormalization( $url ) {
|
||||
$expected_uri = 'http://example.com/';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( $expected_uri, $this->fixture->normalize() );
|
||||
|
||||
}
|
||||
|
||||
public function schemeData() {
|
||||
return array( array( 'http://example.com' ),
|
||||
array( 'http://example.com/' ),
|
||||
array( 'http://example.com:/' ),
|
||||
array( 'http://example.com:80/' ), );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider schemeDataSSL
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc3986#section-6.2.3
|
||||
*/
|
||||
public function testSchemeBasedNormalizationSSL( $url ) {
|
||||
$expected_uri = 'https://example.com/';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( $expected_uri, $this->fixture->normalize() );
|
||||
|
||||
}
|
||||
|
||||
public function schemeDataSSL() {
|
||||
return array( array( 'https://example.com' ),
|
||||
array( 'https://example.com/' ),
|
||||
array( 'https://example.com:/' ),
|
||||
array( 'https://example.com:443/' ), );
|
||||
}
|
||||
|
||||
public function testQueryParametersArePreserved() {
|
||||
$url = 'http://fancysite.nl/links/doit.pl?id=2029';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( $url, $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testFragmentIdentifiersArePreserved() {
|
||||
$url = 'http://example.com/index.html#fragment';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( $url, $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testPortNumbersArePreserved() {
|
||||
$url = 'http://example.com:81/index.html';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( $url, $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testCaseSensitiveElementsArePreserved() {
|
||||
$url = 'HtTp://User:Pass@www.ExAmPle.com:80/Blah';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( 'http://User:Pass@www.example.com/Blah', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testSetUrlReturnsFalseWithUnparseableUrl() {
|
||||
$this->assertFalse( $this->fixture->setUrl( '/test:2/' ) );
|
||||
}
|
||||
|
||||
public function testTrailingSlashIsAdded() {
|
||||
$url = 'http://example.com';
|
||||
|
||||
$this->fixture->setUrl( $url );
|
||||
$this->assertEquals( 'http://example.com/', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testDoubleSlashNotAddedToSchemeIfNoHost() {
|
||||
$uri = 'mailto:mail@example.com';
|
||||
|
||||
$this->fixture->setUrl( $uri );
|
||||
$this->assertEquals( 'mailto:mail@example.com', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testColonNotAddedToUsernameWhenNoPassword() {
|
||||
$uri = 'http://user@example.com/';
|
||||
|
||||
$this->fixture->setUrl( $uri );
|
||||
$this->assertEquals( 'http://user@example.com/', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testPortAndFragmentDoNotPersistBetweenCalls() {
|
||||
$this->fixture->setUrl( 'http://example.com/path/?query#fragment' );
|
||||
$this->fixture->normalize();
|
||||
|
||||
$uri = 'http://example.com:400/';
|
||||
$this->fixture->setUrl( $uri );
|
||||
$this->assertEquals( $uri, $this->fixture->normalize() );
|
||||
|
||||
$uri = 'http://example.com/';
|
||||
$this->fixture->setUrl( $uri );
|
||||
$this->assertEquals( $uri, $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testEbayImageUrl() {
|
||||
$this->fixture->setUrl( 'http://i.ebayimg.com/t/O05520-Adidas-OM-Olympique-Marseille-Jacket-Hooded-UK-S-/00/s/NDAwWDQwMA==/$(KGrHqF,!lMF!iFJh4nmBQflyg7GSw~~60_12.JPG' );
|
||||
$this->assertEquals( 'http://i.ebayimg.com/t/O05520-Adidas-OM-Olympique-Marseille-Jacket-Hooded-UK-S-/00/s/NDAwWDQwMA==/$(KGrHqF,!lMF!iFJh4nmBQflyg7GSw~~60_12.JPG',
|
||||
$this->fixture->normalize() );
|
||||
|
||||
}
|
||||
|
||||
public function testReservedCharactersInPathSegmentAreNotEncoded() {
|
||||
$this->fixture->setUrl( "http://www.example.com/!$&'()*+,;=/" );
|
||||
$this->assertEquals( "http://www.example.com/!$&'()*+,;=/", $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testQueryWithArray() {
|
||||
$this->fixture->setUrl('http://www.example.com/?array[key]=value');
|
||||
$this->assertEquals('http://www.example.com/?array%5Bkey%5D=value', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testSpacesInQueryStringAreCorrectlyEncoded() {
|
||||
$this->fixture->setUrl( 'http://www.example.com/?a space' );
|
||||
$this->assertEquals( 'http://www.example.com/?a%20space', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testQueryValuesThatContainEqualsSignsArePreserved() {
|
||||
$this->fixture->setUrl( 'http://www.example.com/?key=v1=v2' );
|
||||
$this->assertEquals( 'http://www.example.com/?key=v1%3Dv2', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testUtf8HostNames() {
|
||||
$this->fixture->setUrl('http://www.Яндекс.РФ');
|
||||
$this->assertEquals( 'http://www.яндекс.рф/', $this->fixture->normalize() );
|
||||
|
||||
$this->fixture->setUrl('http://dev.ŽiŪrKėNaS.lt');
|
||||
$this->assertEquals( 'http://dev.žiūrkėnas.lt/', $this->fixture->normalize() );
|
||||
|
||||
}
|
||||
|
||||
public function testTrimMultipleSlashes() {
|
||||
$this->fixture->setUrl('http://www.яндекс.рф/////');
|
||||
$this->assertEquals( 'http://www.яндекс.рф/', $this->fixture->normalize() );
|
||||
|
||||
$this->fixture->setUrl('http://www.яндекс.рф/////index.php');
|
||||
$this->assertEquals( 'http://www.яндекс.рф/index.php', $this->fixture->normalize() );
|
||||
|
||||
$this->fixture->setUrl('http://www.яндекс.рф///index////subdir////');
|
||||
$this->assertEquals( 'http://www.яндекс.рф/index/subdir/', $this->fixture->normalize() );
|
||||
|
||||
$this->fixture->setUrl('http://www.яндекс.рф/index/../../subdir');
|
||||
$this->assertEquals( 'http://www.яндекс.рф/subdir', $this->fixture->normalize() );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider unnamedKeys
|
||||
*/
|
||||
public function testUnnamedKeysInQueryStringArePreserved($url, $expected) {
|
||||
$this->fixture->setUrl($url);
|
||||
$this->assertEquals($expected, $this->fixture->normalize());
|
||||
}
|
||||
|
||||
public function unnamedKeys() {
|
||||
return array(
|
||||
array('http://www.example.com/?foo[]=bar&foo[]=baz', 'http://www.example.com/?foo%5B%5D=bar&foo%5B%5D=baz'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testDelimitersArePreservedIfAssociatedComponentIsEmpty()
|
||||
{
|
||||
$this->fixture->setUrl('http://www.example.com/?');
|
||||
$this->assertEquals( 'http://www.example.com/?', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testEmptyDelimitersAreRemovedOption()
|
||||
{
|
||||
$this->fixture = new Normalizer('http://www.example.com/?', true);
|
||||
$this->assertEquals( 'http://www.example.com/', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testEmptyParametersAreNotPreserved()
|
||||
{
|
||||
$this->fixture->setUrl('http://www.example.com/?a&');
|
||||
$this->assertEquals( 'http://www.example.com/?a', $this->fixture->normalize() );
|
||||
}
|
||||
|
||||
public function testAlphabeticalSortingOfQueryParameters()
|
||||
{
|
||||
$this->fixture = new Normalizer('http://www.example.com/?c=3&b=2&a=1', false, true);
|
||||
$this->assertEquals( 'http://www.example.com/?a=1&b=2&c=3', $this->fixture->normalize() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user