| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace GuzzleHttp\Psr7; |
| 6 |
|
| 7 |
use Psr\Http\Message\UriInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
final class UriNormalizer |
| 17 |
{ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public const PRESERVING_NORMALIZATIONS = |
| 22 |
self::CAPITALIZE_PERCENT_ENCODING | |
| 23 |
self::DECODE_UNRESERVED_CHARACTERS | |
| 24 |
self::CONVERT_EMPTY_PATH | |
| 25 |
self::REMOVE_DEFAULT_HOST | |
| 26 |
self::REMOVE_DEFAULT_PORT | |
| 27 |
self::REMOVE_DOT_SEGMENTS; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
1b |
| 33 |
|
| 34 |
public const CAPITALIZE_PERCENT_ENCODING = 1; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
–%39), |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
e/ |
| 44 |
|
| 45 |
public const DECODE_UNRESERVED_CHARACTERS = 2; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
g/ |
| 51 |
|
| 52 |
public const CONVERT_EMPTY_PATH = 4; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
le |
| 64 |
|
| 65 |
public const REMOVE_DEFAULT_HOST = 8; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
g/ |
| 71 |
|
| 72 |
public const REMOVE_DEFAULT_PORT = 16; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ml |
| 81 |
|
| 82 |
public const REMOVE_DOT_SEGMENTS = 32; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ml |
| 92 |
|
| 93 |
public const REMOVE_DUPLICATE_SLASHES = 64; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
en |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public const SORT_QUERY_PARAMETERS = 128; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface |
| 125 |
{ |
| 126 |
if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { |
| 127 |
$uri = self::capitalizePercentEncoding($uri); |
| 128 |
} |
| 129 |
|
| 130 |
if ($flags & self::DECODE_UNRESERVED_CHARACTERS) { |
| 131 |
$uri = self::decodeUnreservedCharacters($uri); |
| 132 |
} |
| 133 |
|
| 134 |
if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' |
| 135 |
&& ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') |
| 136 |
) { |
| 137 |
$uri = $uri->withPath('/'); |
| 138 |
} |
| 139 |
|
| 140 |
if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { |
| 141 |
$uri = $uri->withHost(''); |
| 142 |
} |
| 143 |
|
| 144 |
if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { |
| 145 |
$uri = $uri->withPort(null); |
| 146 |
} |
| 147 |
|
| 148 |
if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { |
| 149 |
$uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); |
| 150 |
} |
| 151 |
|
| 152 |
if ($flags & self::REMOVE_DUPLICATE_SLASHES) { |
| 153 |
$uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); |
| 154 |
} |
| 155 |
|
| 156 |
if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { |
| 157 |
$queryKeyValues = explode('&', $uri->getQuery()); |
| 158 |
sort($queryKeyValues); |
| 159 |
$uri = $uri->withQuery(implode('&', $queryKeyValues)); |
| 160 |
} |
| 161 |
|
| 162 |
return $uri; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
| 180 |
{ |
| 181 |
return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
| 182 |
} |
| 183 |
|
| 184 |
private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
| 185 |
{ |
| 186 |
$regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
| 187 |
|
| 188 |
$callback = function (array $match): string { |
| 189 |
return strtoupper($match[0]); |
| 190 |
}; |
| 191 |
|
| 192 |
return $uri |
| 193 |
->withPath(self::normalizePercentEncodingInComponent($uri->getPath(), $regex, $callback)) |
| 194 |
->withQuery(self::normalizePercentEncodingInComponent($uri->getQuery(), $regex, $callback)) |
| 195 |
->withFragment(self::normalizePercentEncodingInComponent($uri->getFragment(), $regex, $callback)); |
| 196 |
} |
| 197 |
|
| 198 |
private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface |
| 199 |
{ |
| 200 |
$regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
| 201 |
|
| 202 |
$callback = function (array $match): string { |
| 203 |
return rawurldecode($match[0]); |
| 204 |
}; |
| 205 |
|
| 206 |
return $uri |
| 207 |
->withPath(self::normalizePercentEncodingInComponent($uri->getPath(), $regex, $callback)) |
| 208 |
->withQuery(self::normalizePercentEncodingInComponent($uri->getQuery(), $regex, $callback)) |
| 209 |
->withFragment(self::normalizePercentEncodingInComponent($uri->getFragment(), $regex, $callback)); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private static function normalizePercentEncodingInComponent(string $component, string $regex, callable $callback): string |
| 216 |
{ |
| 217 |
$normalized = preg_replace_callback($regex, $callback, $component); |
| 218 |
|
| 219 |
if ($normalized === null) { |
| 220 |
throw new \RuntimeException('Unable to normalize URI component percent-encoding'); |
| 221 |
} |
| 222 |
|
| 223 |
return $normalized; |
| 224 |
} |
| 225 |
|
| 226 |
private function __construct() |
| 227 |
{ |
| 228 |
|
| 229 |
} |
| 230 |
} |