| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\GuzzleHttp\Psr7\Exception\MalformedUriException; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\UriInterface; |
| 8 |
/** |
| 9 |
* PSR-7 URI implementation. |
| 10 |
* |
| 11 |
* @author Michael Dowling |
| 12 |
* @author Tobias Schultze |
| 13 |
* @author Matthew Weier O'Phinney |
| 14 |
*/ |
| 15 |
class Uri implements UriInterface, \JsonSerializable |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Absolute http and https URIs require a host per RFC 9110 Section 4.2.1 |
| 19 |
* but in generic URIs the host can be empty. So for http(s) URIs we apply |
| 20 |
* this default host when no host is given yet to form a valid URI. |
| 21 |
*/ |
| 22 |
private const HTTP_DEFAULT_HOST = 'localhost'; |
| 23 |
private const DEFAULT_PORTS = ['http' => 80, 'https' => 443, 'ftp' => 21, 'gopher' => 70, 'nntp' => 119, 'news' => 119, 'telnet' => 23, 'tn3270' => 23, 'imap' => 143, 'pop' => 110, 'ldap' => 389, 'ws' => 80, 'wss' => 443]; |
| 24 |
private const QUERY_SEPARATORS_REPLACEMENT = ['=' => '%3D', '&' => '%26', '+' => '%2B']; |
| 25 |
/** @var string Uri scheme. */ |
| 26 |
private string $scheme = ''; |
| 27 |
/** @var string Uri user info. */ |
| 28 |
private string $userInfo = ''; |
| 29 |
/** @var string Uri host. */ |
| 30 |
private string $host = ''; |
| 31 |
/** @var int|null Uri port. */ |
| 32 |
private ?int $port = null; |
| 33 |
/** @var string Uri path. */ |
| 34 |
private string $path = ''; |
| 35 |
/** @var string Uri query string. */ |
| 36 |
private string $query = ''; |
| 37 |
/** @var string Uri fragment. */ |
| 38 |
private string $fragment = ''; |
| 39 |
public function __construct(#[\SensitiveParameter] string $uri = '') |
| 40 |
{ |
| 41 |
if ($uri !== '') { |
| 42 |
$parts = UriParser::parse($uri); |
| 43 |
if ($parts === \false) { |
| 44 |
throw new MalformedUriException(\sprintf('Unable to parse URI: %s', Utils::redactUriStringForMessage($uri))); |
| 45 |
} |
| 46 |
try { |
| 47 |
$this->applyParts($parts); |
| 48 |
} catch (MalformedUriException $e) { |
| 49 |
throw $e; |
| 50 |
} catch (\InvalidArgumentException $e) { |
| 51 |
throw new MalformedUriException($e->getMessage(), 0, $e); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
public function __toString() : string |
| 56 |
{ |
| 57 |
return self::composeComponents($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
| 58 |
} |
| 59 |
/** |
| 60 |
* Composes a URI reference string from its various components according to |
| 61 |
* RFC 3986 Section 5.3. |
| 62 |
* |
| 63 |
* Usually this method does not need to be called manually but instead is |
| 64 |
* used indirectly via `Psr\Http\Message\UriInterface::__toString`. |
| 65 |
* |
| 66 |
* PSR-7 UriInterface treats an empty component the same as a missing |
| 67 |
* component as `getQuery()`, `getFragment()` etc. always return a string. |
| 68 |
* This explains the slight difference to RFC 3986 Section 5.3. |
| 69 |
* |
| 70 |
* Another adjustment is that the authority separator is added even when the |
| 71 |
* authority is missing/empty for the "file" scheme. This is because PHP |
| 72 |
* stream functions like `file_get_contents` only work with `file:///myfile` |
| 73 |
* but not with `file:/myfile` although they are equivalent according to RFC |
| 74 |
* 3986. But `file:///` is the more common syntax for the file scheme anyway |
| 75 |
* (Chrome for example redirects to that format). The separator is omitted |
| 76 |
* when such a URI has a rootless or empty path: adding it would turn the |
| 77 |
* first path segment into the authority of the composed URI, or compose the |
| 78 |
* string `file://`, which cannot be parsed back into a URI. |
| 79 |
* |
| 80 |
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.3 |
| 81 |
*/ |
| 82 |
public static function composeComponents(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment) : string |
| 83 |
{ |
| 84 |
$uri = ''; |
| 85 |
// weak type checks to also accept null until we can add scalar type hints |
| 86 |
if ($scheme != '') { |
| 87 |
$uri .= $scheme . ':'; |
| 88 |
} |
| 89 |
if ($authority != '' || $scheme === 'file' && \str_starts_with($path, '/')) { |
| 90 |
$uri .= '//' . $authority; |
| 91 |
} |
| 92 |
if ($authority != '' && $path != '' && !\str_starts_with($path, '/')) { |
| 93 |
$path = '/' . $path; |
| 94 |
} |
| 95 |
$uri .= $path; |
| 96 |
if ($query != '') { |
| 97 |
$uri .= '?' . $query; |
| 98 |
} |
| 99 |
if ($fragment != '') { |
| 100 |
$uri .= '#' . $fragment; |
| 101 |
} |
| 102 |
return $uri; |
| 103 |
} |
| 104 |
/** |
| 105 |
* Whether the URI has the default port of the current scheme. |
| 106 |
* |
| 107 |
* `Psr\Http\Message\UriInterface::getPort` may return null or the standard |
| 108 |
* port. This method can be used independently of the implementation. |
| 109 |
*/ |
| 110 |
public static function isDefaultPort(UriInterface $uri) : bool |
| 111 |
{ |
| 112 |
return $uri->getPort() === null || isset(self::DEFAULT_PORTS[$uri->getScheme()]) && $uri->getPort() === self::DEFAULT_PORTS[$uri->getScheme()]; |
| 113 |
} |
| 114 |
/** |
| 115 |
* Whether the URI is absolute, i.e. it has a scheme. |
| 116 |
* |
| 117 |
* An instance of UriInterface can either be an absolute URI or a relative |
| 118 |
* reference. An absolute URI has a scheme. A relative reference is used to |
| 119 |
* express a URI relative to another URI, the base URI. Relative references |
| 120 |
* can be divided into several forms according to RFC 3986 Section 4.2: |
| 121 |
* - network-path references, e.g. `//example.com/path` |
| 122 |
* - absolute-path references, e.g. `/path` |
| 123 |
* - relative-path references, e.g. `subpath` |
| 124 |
* |
| 125 |
* @see Uri::isNetworkPathReference |
| 126 |
* @see Uri::isAbsolutePathReference |
| 127 |
* @see Uri::isRelativePathReference |
| 128 |
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
| 129 |
*/ |
| 130 |
public static function isAbsolute(UriInterface $uri) : bool |
| 131 |
{ |
| 132 |
return $uri->getScheme() !== ''; |
| 133 |
} |
| 134 |
/** |
| 135 |
* Whether the URI is a network-path reference. |
| 136 |
* |
| 137 |
* A relative reference that begins with two slash characters is termed a |
| 138 |
* network-path reference. |
| 139 |
* |
| 140 |
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
| 141 |
*/ |
| 142 |
public static function isNetworkPathReference(UriInterface $uri) : bool |
| 143 |
{ |
| 144 |
return $uri->getScheme() === '' && $uri->getAuthority() !== ''; |
| 145 |
} |
| 146 |
/** |
| 147 |
* Whether the URI is an absolute-path reference. |
| 148 |
* |
| 149 |
* A relative reference that begins with a single slash character is termed |
| 150 |
* an absolute-path reference. |
| 151 |
* |
| 152 |
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
| 153 |
*/ |
| 154 |
public static function isAbsolutePathReference(UriInterface $uri) : bool |
| 155 |
{ |
| 156 |
return $uri->getScheme() === '' && $uri->getAuthority() === '' && isset($uri->getPath()[0]) && $uri->getPath()[0] === '/'; |
| 157 |
} |
| 158 |
/** |
| 159 |
* Whether the URI is a relative-path reference. |
| 160 |
* |
| 161 |
* A relative reference that does not begin with a slash character is termed |
| 162 |
* a relative-path reference. |
| 163 |
* |
| 164 |
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
| 165 |
*/ |
| 166 |
public static function isRelativePathReference(UriInterface $uri) : bool |
| 167 |
{ |
| 168 |
return $uri->getScheme() === '' && $uri->getAuthority() === '' && (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/'); |
| 169 |
} |
| 170 |
/** |
| 171 |
* Whether the URI is a same-document reference. |
| 172 |
* |
| 173 |
* A same-document reference refers to a URI that is, aside from its |
| 174 |
* fragment component, identical to the base URI. When no base URI is given, |
| 175 |
* only an empty URI reference (apart from its fragment) is considered a |
| 176 |
* same-document reference. |
| 177 |
* |
| 178 |
* @param UriInterface $uri The URI to check |
| 179 |
* @param UriInterface|null $base An optional base URI to compare against |
| 180 |
* |
| 181 |
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.4 |
| 182 |
*/ |
| 183 |
public static function isSameDocumentReference(UriInterface $uri, ?UriInterface $base = null) : bool |
| 184 |
{ |
| 185 |
if ($base !== null) { |
| 186 |
$uri = UriResolver::resolve($base, $uri); |
| 187 |
return $uri->getScheme() === $base->getScheme() && $uri->getAuthority() === $base->getAuthority() && self::rawPath($uri) === self::rawPath($base) && $uri->getQuery() === $base->getQuery(); |
| 188 |
} |
| 189 |
return $uri->getScheme() === '' && $uri->getAuthority() === '' && $uri->getPath() === '' && $uri->getQuery() === ''; |
| 190 |
} |
| 191 |
/** |
| 192 |
* Creates a new URI with a specific query string value removed. |
| 193 |
* |
| 194 |
* Any existing query string values that exactly match the provided key are |
| 195 |
* removed. |
| 196 |
* |
| 197 |
* @param UriInterface $uri URI to use as a base. |
| 198 |
* @param string $key Query string key to remove. |
| 199 |
*/ |
| 200 |
public static function withoutQueryValue(UriInterface $uri, string $key) : UriInterface |
| 201 |
{ |
| 202 |
$result = self::getFilteredQueryString($uri, [$key]); |
| 203 |
return $uri->withQuery(\implode('&', $result)); |
| 204 |
} |
| 205 |
/** |
| 206 |
* Creates a new URI with a specific query string value. |
| 207 |
* |
| 208 |
* Any existing query string values that exactly match the provided key are |
| 209 |
* removed and replaced with the given key value pair. A value of null will |
| 210 |
* set the query string key without a value, e.g. "key" instead of |
| 211 |
* "key=value". |
| 212 |
* |
| 213 |
* @param UriInterface $uri URI to use as a base. |
| 214 |
* @param string $key Key to set. |
| 215 |
* @param string|null $value Value to set |
| 216 |
*/ |
| 217 |
public static function withQueryValue(UriInterface $uri, string $key, ?string $value) : UriInterface |
| 218 |
{ |
| 219 |
$result = self::getFilteredQueryString($uri, [$key]); |
| 220 |
$result[] = self::generateQueryString($key, $value); |
| 221 |
return $uri->withQuery(\implode('&', $result)); |
| 222 |
} |
| 223 |
/** |
| 224 |
* Creates a new URI with multiple query string values. |
| 225 |
* |
| 226 |
* It has the same behavior as `withQueryValue()` but for an associative |
| 227 |
* array of key => value. |
| 228 |
* |
| 229 |
* @param UriInterface $uri URI to use as a base. |
| 230 |
* @param (string|null)[] $keyValueArray Associative array of key and values |
| 231 |
*/ |
| 232 |
public static function withQueryValues(UriInterface $uri, array $keyValueArray) : UriInterface |
| 233 |
{ |
| 234 |
$result = self::getFilteredQueryString($uri, \array_keys($keyValueArray)); |
| 235 |
foreach ($keyValueArray as $key => $value) { |
| 236 |
self::assertStringOrNullQueryValue($value); |
| 237 |
$result[] = self::generateQueryString((string) $key, $value !== null ? (string) $value : null); |
| 238 |
} |
| 239 |
return $uri->withQuery(\implode('&', $result)); |
| 240 |
} |
| 241 |
/** |
| 242 |
* @param mixed $value |
| 243 |
*/ |
| 244 |
private static function assertStringOrNullQueryValue($value) : void |
| 245 |
{ |
| 246 |
if ($value !== null && !\is_string($value)) { |
| 247 |
throw new \InvalidArgumentException(\sprintf('Query string values must be a string or null, %s given.', \get_debug_type($value))); |
| 248 |
} |
| 249 |
} |
| 250 |
/** |
| 251 |
* Creates a URI from a hash of `parse_url` components. |
| 252 |
* |
| 253 |
* @see https://www.php.net/manual/en/function.parse-url.php |
| 254 |
* |
| 255 |
* @throws MalformedUriException If the components do not form a valid URI. |
| 256 |
*/ |
| 257 |
public static function fromParts(#[\SensitiveParameter] array $parts) : UriInterface |
| 258 |
{ |
| 259 |
$uri = new self(); |
| 260 |
try { |
| 261 |
$uri->applyParts($parts); |
| 262 |
$uri->validateState(); |
| 263 |
} catch (MalformedUriException $e) { |
| 264 |
throw $e; |
| 265 |
} catch (\InvalidArgumentException $e) { |
| 266 |
throw new MalformedUriException($e->getMessage(), 0, $e); |
| 267 |
} |
| 268 |
return $uri; |
| 269 |
} |
| 270 |
/** |
| 271 |
* @throws \InvalidArgumentException If the host is invalid. |
| 272 |
* |
| 273 |
* @internal |
| 274 |
*/ |
| 275 |
public static function assertValidHost(string $host) : void |
| 276 |
{ |
| 277 |
if (!Rfc3986::isValidHost($host)) { |
| 278 |
throw new \InvalidArgumentException(\sprintf('Invalid host: %s', DiagnosticValue::escape($host))); |
| 279 |
} |
| 280 |
} |
| 281 |
public function getScheme() : string |
| 282 |
{ |
| 283 |
return $this->scheme; |
| 284 |
} |
| 285 |
public function getAuthority() : string |
| 286 |
{ |
| 287 |
$authority = $this->host; |
| 288 |
if ($this->userInfo !== '') { |
| 289 |
$authority = $this->userInfo . '@' . $authority; |
| 290 |
} |
| 291 |
if ($this->port !== null) { |
| 292 |
$authority .= ':' . $this->port; |
| 293 |
} |
| 294 |
return $authority; |
| 295 |
} |
| 296 |
public function getUserInfo() : string |
| 297 |
{ |
| 298 |
return $this->userInfo; |
| 299 |
} |
| 300 |
public function getHost() : string |
| 301 |
{ |
| 302 |
return $this->host; |
| 303 |
} |
| 304 |
public function getPort() : ?int |
| 305 |
{ |
| 306 |
return $this->port; |
| 307 |
} |
| 308 |
public function getPath() : string |
| 309 |
{ |
| 310 |
if (\str_starts_with($this->path, '//')) { |
| 311 |
return '/' . \ltrim($this->path, '/'); |
| 312 |
} |
| 313 |
return $this->path; |
| 314 |
} |
| 315 |
/** |
| 316 |
* Returns the path as it appears within a URI's string form. |
| 317 |
* |
| 318 |
* getPath() collapses multiple leading slashes so that a path used in |
| 319 |
* isolation cannot be mistaken for a protocol-relative URL. Whole-URI |
| 320 |
* operations like reference resolution and normalization (RFC 3986 |
| 321 |
* Sections 5 and 6) are defined on the URI string form, where the path |
| 322 |
* stays verbatim, so they must read the path through this method instead. |
| 323 |
* For direct instances of this class the path is derived from the stored |
| 324 |
* components, including the leading slash the string form adds to a |
| 325 |
* rootless path when an authority is present; for subclasses and other |
| 326 |
* implementations the path is split from the string form per RFC 3986 |
| 327 |
* Appendix B, without validating or decoding any other component. The |
| 328 |
* scheme is only split off when the instance reports one, as a relative |
| 329 |
* reference can begin with a segment containing a colon that the Appendix |
| 330 |
* B expression would otherwise read as a scheme. |
| 331 |
* |
| 332 |
* @throws \RuntimeException If the path cannot be split from the string form. |
| 333 |
* |
| 334 |
* @internal |
| 335 |
*/ |
| 336 |
public static function rawPath(UriInterface $uri) : string |
| 337 |
{ |
| 338 |
if (\get_class($uri) === self::class) { |
| 339 |
if ($uri->path !== '' && !\str_starts_with($uri->path, '/') && $uri->getAuthority() !== '') { |
| 340 |
// composeComponents() prepends a slash to a rootless path when |
| 341 |
// an authority is present, so the string form uses this path. |
| 342 |
return '/' . $uri->path; |
| 343 |
} |
| 344 |
return $uri->path; |
| 345 |
} |
| 346 |
$pattern = $uri->getScheme() === '' ? '%^(?://[^/?#]*)?([^?#]*)%' : '%^(?:[^:/?#]+:)?(?://[^/?#]*)?([^?#]*)%'; |
| 347 |
$count = \preg_match($pattern, (string) $uri, $matches); |
| 348 |
if ($count === \false) { |
| 349 |
throw new \RuntimeException('Unable to read the URI path: ' . \preg_last_error_msg()); |
| 350 |
} |
| 351 |
return $matches[1] ?? ''; |
| 352 |
} |
| 353 |
public function getQuery() : string |
| 354 |
{ |
| 355 |
return $this->query; |
| 356 |
} |
| 357 |
public function getFragment() : string |
| 358 |
{ |
| 359 |
return $this->fragment; |
| 360 |
} |
| 361 |
public function withScheme(string $scheme) : UriInterface |
| 362 |
{ |
| 363 |
$scheme = $this->filterScheme($scheme); |
| 364 |
if ($this->scheme === $scheme) { |
| 365 |
return $this; |
| 366 |
} |
| 367 |
$new = clone $this; |
| 368 |
$new->scheme = $scheme; |
| 369 |
$new->removeDefaultPort(); |
| 370 |
$new->validateState(); |
| 371 |
return $new; |
| 372 |
} |
| 373 |
public function withUserInfo(string $user, #[\SensitiveParameter] ?string $password = null) : UriInterface |
| 374 |
{ |
| 375 |
$info = $this->filterUserInfoComponent($user); |
| 376 |
if ($password !== null) { |
| 377 |
$info .= ':' . $this->filterUserInfoComponent($password); |
| 378 |
} |
| 379 |
if ($this->userInfo === $info) { |
| 380 |
return $this; |
| 381 |
} |
| 382 |
$new = clone $this; |
| 383 |
$new->userInfo = $info; |
| 384 |
$new->validateState(); |
| 385 |
return $new; |
| 386 |
} |
| 387 |
public function withHost(string $host) : UriInterface |
| 388 |
{ |
| 389 |
$host = $this->filterHost($host); |
| 390 |
if ($this->host === $host) { |
| 391 |
return $this; |
| 392 |
} |
| 393 |
$new = clone $this; |
| 394 |
$new->host = $host; |
| 395 |
$new->validateState(); |
| 396 |
return $new; |
| 397 |
} |
| 398 |
public function withPort(?int $port) : UriInterface |
| 399 |
{ |
| 400 |
$port = $this->filterPort($port); |
| 401 |
if ($this->port === $port) { |
| 402 |
return $this; |
| 403 |
} |
| 404 |
$new = clone $this; |
| 405 |
$new->port = $port; |
| 406 |
$new->removeDefaultPort(); |
| 407 |
$new->validateState(); |
| 408 |
return $new; |
| 409 |
} |
| 410 |
public function withPath(string $path) : UriInterface |
| 411 |
{ |
| 412 |
$path = $this->filterPath($path); |
| 413 |
if ($this->path === $path) { |
| 414 |
return $this; |
| 415 |
} |
| 416 |
$new = clone $this; |
| 417 |
$new->path = $path; |
| 418 |
$new->validateState(); |
| 419 |
return $new; |
| 420 |
} |
| 421 |
public function withQuery(string $query) : UriInterface |
| 422 |
{ |
| 423 |
$query = $this->filterQueryAndFragment($query); |
| 424 |
if ($this->query === $query) { |
| 425 |
return $this; |
| 426 |
} |
| 427 |
$new = clone $this; |
| 428 |
$new->query = $query; |
| 429 |
return $new; |
| 430 |
} |
| 431 |
public function withFragment(string $fragment) : UriInterface |
| 432 |
{ |
| 433 |
$fragment = $this->filterQueryAndFragment($fragment); |
| 434 |
if ($this->fragment === $fragment) { |
| 435 |
return $this; |
| 436 |
} |
| 437 |
$new = clone $this; |
| 438 |
$new->fragment = $fragment; |
| 439 |
return $new; |
| 440 |
} |
| 441 |
public function jsonSerialize() : string |
| 442 |
{ |
| 443 |
return $this->__toString(); |
| 444 |
} |
| 445 |
/** |
| 446 |
* Apply parse_url parts to a URI. |
| 447 |
* |
| 448 |
* @param array $parts Array of parse_url parts to apply. |
| 449 |
*/ |
| 450 |
private function applyParts(#[\SensitiveParameter] array $parts) : void |
| 451 |
{ |
| 452 |
$this->scheme = isset($parts['scheme']) ? $this->filterScheme($parts['scheme']) : ''; |
| 453 |
$this->userInfo = isset($parts['user']) ? $this->filterUserInfoComponent($parts['user']) : ''; |
| 454 |
$this->host = isset($parts['host']) ? $this->filterHost($parts['host']) : ''; |
| 455 |
$this->port = isset($parts['port']) ? $this->filterPortPart($parts['port']) : null; |
| 456 |
$this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
| 457 |
$this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
| 458 |
$this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
| 459 |
if (isset($parts['pass'])) { |
| 460 |
$this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']); |
| 461 |
} |
| 462 |
$this->removeDefaultPort(); |
| 463 |
} |
| 464 |
/** |
| 465 |
* @throws \InvalidArgumentException If the scheme is invalid. |
| 466 |
*/ |
| 467 |
private function filterScheme(string $scheme) : string |
| 468 |
{ |
| 469 |
$scheme = Utils::asciiToLower($scheme); |
| 470 |
if (!Rfc3986::isValidScheme($scheme)) { |
| 471 |
throw new \InvalidArgumentException(\sprintf('Invalid scheme: %s', DiagnosticValue::escape($scheme))); |
| 472 |
} |
| 473 |
return $scheme; |
| 474 |
} |
| 475 |
/** |
| 476 |
* @throws \InvalidArgumentException If the user info is invalid. |
| 477 |
*/ |
| 478 |
private function filterUserInfoComponent(#[\SensitiveParameter] string $component) : string |
| 479 |
{ |
| 480 |
return $this->filterComponent('/(?:[^%' . Rfc3986::CHAR_UNRESERVED . Rfc3986::CHAR_SUB_DELIMS . ']++|%(?!' . Rfc3986::HEX_OCTET . '))/', $component, 'Unable to filter URI user info'); |
| 481 |
} |
| 482 |
/** |
| 483 |
* @throws \InvalidArgumentException If the host is invalid. |
| 484 |
*/ |
| 485 |
private function filterHost(string $host) : string |
| 486 |
{ |
| 487 |
$host = Utils::asciiToLower($host); |
| 488 |
$filtered = \preg_replace_callback('/%' . Rfc3986::HEX_OCTET . '/', static function (array $m) : string { |
| 489 |
return Utils::asciiToUpper($m[0]); |
| 490 |
}, $host); |
| 491 |
if ($filtered === null) { |
| 492 |
throw new \RuntimeException('Unable to normalize URI host percent-encoding: ' . \preg_last_error_msg()); |
| 493 |
} |
| 494 |
self::assertValidHost($filtered); |
| 495 |
if (\str_starts_with($filtered, '[') && !\str_starts_with($filtered, '[v')) { |
| 496 |
// assertValidHost() accepted this bracketed value with the same |
| 497 |
// filter_var() predicate tryCanonicalizeIpv6() validates with, and |
| 498 |
// its pure-PHP parse cannot fail on filter-accepted text, so the |
| 499 |
// null guard is defense in depth only. |
| 500 |
$canonical = Rfc3986::tryCanonicalizeIpv6(\substr($filtered, 1, -1)); |
| 501 |
if ($canonical !== null) { |
| 502 |
$filtered = '[' . $canonical . ']'; |
| 503 |
} |
| 504 |
} |
| 505 |
return $filtered; |
| 506 |
} |
| 507 |
/** |
| 508 |
* @throws \InvalidArgumentException If the port is invalid. |
| 509 |
*/ |
| 510 |
private function filterPort(?int $port) : ?int |
| 511 |
{ |
| 512 |
if ($port === null) { |
| 513 |
return null; |
| 514 |
} |
| 515 |
if (0 > $port || 0xffff < $port) { |
| 516 |
throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
| 517 |
} |
| 518 |
return $port; |
| 519 |
} |
| 520 |
/** |
| 521 |
* @param mixed $port |
| 522 |
* |
| 523 |
* @throws \InvalidArgumentException If the port is invalid. |
| 524 |
*/ |
| 525 |
private function filterPortPart($port) : ?int |
| 526 |
{ |
| 527 |
if (\is_int($port)) { |
| 528 |
return $this->filterPort($port); |
| 529 |
} |
| 530 |
if (\is_string($port) && \ctype_digit($port)) { |
| 531 |
// A zero port is accepted here; only Rfc9112::parsePort() rejects |
| 532 |
// it for HTTP Host/authority parsing. |
| 533 |
if (Rfc3986::isValidPort($port)) { |
| 534 |
return (int) \ltrim($port, '0'); |
| 535 |
} |
| 536 |
throw new \InvalidArgumentException(\sprintf('Invalid port: %s. Must be between 0 and 65535', \ltrim($port, '0'))); |
| 537 |
} |
| 538 |
throw new \InvalidArgumentException(\sprintf('Invalid port: %s. Must be between 0 and 65535', self::describeInvalidPort($port))); |
| 539 |
} |
| 540 |
/** |
| 541 |
* @param mixed $port |
| 542 |
*/ |
| 543 |
private static function describeInvalidPort($port) : string |
| 544 |
{ |
| 545 |
if (\is_string($port)) { |
| 546 |
return DiagnosticValue::escape($port); |
| 547 |
} |
| 548 |
if (\is_int($port)) { |
| 549 |
return (string) $port; |
| 550 |
} |
| 551 |
if (\is_bool($port)) { |
| 552 |
return $port ? 'true' : 'false'; |
| 553 |
} |
| 554 |
if ($port === null) { |
| 555 |
return 'null'; |
| 556 |
} |
| 557 |
if (\is_float($port)) { |
| 558 |
if (\is_nan($port)) { |
| 559 |
return 'NAN'; |
| 560 |
} |
| 561 |
if (\is_infinite($port)) { |
| 562 |
return $port > 0 ? 'INF' : '-INF'; |
| 563 |
} |
| 564 |
return \sprintf('%.14G', $port); |
| 565 |
} |
| 566 |
return \get_debug_type($port); |
| 567 |
} |
| 568 |
/** |
| 569 |
* @param (string|int)[] $keys |
| 570 |
* |
| 571 |
* @return string[] |
| 572 |
*/ |
| 573 |
private static function getFilteredQueryString(UriInterface $uri, array $keys) : array |
| 574 |
{ |
| 575 |
$current = $uri->getQuery(); |
| 576 |
if ($current === '') { |
| 577 |
return []; |
| 578 |
} |
| 579 |
$decodedKeys = \array_map(function ($k) : string { |
| 580 |
return \rawurldecode((string) $k); |
| 581 |
}, $keys); |
| 582 |
return \array_filter(\explode('&', $current), static function (string $part) use($decodedKeys) : bool { |
| 583 |
return !\in_array(\rawurldecode(\explode('=', $part)[0]), $decodedKeys, \true); |
| 584 |
}); |
| 585 |
} |
| 586 |
private static function generateQueryString(string $key, ?string $value) : string |
| 587 |
{ |
| 588 |
// Query string separators ("=", "&") and literal plus signs ("+") within the |
| 589 |
// key or value need to be encoded |
| 590 |
// (while preventing double-encoding) before setting the query string. All other |
| 591 |
// chars that need percent-encoding will be encoded by withQuery(). |
| 592 |
$queryString = \strtr($key, self::QUERY_SEPARATORS_REPLACEMENT); |
| 593 |
if ($value !== null) { |
| 594 |
$queryString .= '=' . \strtr($value, self::QUERY_SEPARATORS_REPLACEMENT); |
| 595 |
} |
| 596 |
return $queryString; |
| 597 |
} |
| 598 |
private function removeDefaultPort() : void |
| 599 |
{ |
| 600 |
if ($this->port !== null && self::isDefaultPort($this)) { |
| 601 |
$this->port = null; |
| 602 |
} |
| 603 |
} |
| 604 |
/** |
| 605 |
* Filters the path of a URI |
| 606 |
* |
| 607 |
* @throws \InvalidArgumentException If the path is invalid. |
| 608 |
*/ |
| 609 |
private function filterPath(string $path) : string |
| 610 |
{ |
| 611 |
return $this->filterComponent('/(?:[^' . Rfc3986::CHAR_UNRESERVED . Rfc3986::CHAR_SUB_DELIMS . '%:@\\/]++|%(?!' . Rfc3986::HEX_OCTET . '))/', $path, 'Unable to filter URI path'); |
| 612 |
} |
| 613 |
/** |
| 614 |
* Filters the query string or fragment of a URI. |
| 615 |
* |
| 616 |
* @throws \InvalidArgumentException If the query or fragment is invalid. |
| 617 |
*/ |
| 618 |
private function filterQueryAndFragment(string $str) : string |
| 619 |
{ |
| 620 |
return $this->filterComponent('/(?:[^' . Rfc3986::CHAR_UNRESERVED . Rfc3986::CHAR_SUB_DELIMS . '%:@\\/\\?]++|%(?!' . Rfc3986::HEX_OCTET . '))/', $str, 'Unable to filter URI query or fragment'); |
| 621 |
} |
| 622 |
private function filterComponent(string $pattern, #[\SensitiveParameter] string $component, string $context) : string |
| 623 |
{ |
| 624 |
$filtered = \preg_replace_callback($pattern, [$this, 'rawurlencodeMatchZero'], $component); |
| 625 |
if ($filtered === null) { |
| 626 |
throw new \RuntimeException($context . ': ' . \preg_last_error_msg()); |
| 627 |
} |
| 628 |
return $filtered; |
| 629 |
} |
| 630 |
private function rawurlencodeMatchZero(array $match) : string |
| 631 |
{ |
| 632 |
return \rawurlencode($match[0]); |
| 633 |
} |
| 634 |
private function validateState() : void |
| 635 |
{ |
| 636 |
if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) { |
| 637 |
$this->host = self::HTTP_DEFAULT_HOST; |
| 638 |
} |
| 639 |
if ($this->getAuthority() === '') { |
| 640 |
if (\str_starts_with($this->path, '//')) { |
| 641 |
throw new MalformedUriException('The path of a URI without an authority must not start with two slashes "//"'); |
| 642 |
} |
| 643 |
if ($this->scheme === '' && \str_contains(\explode('/', $this->path, 2)[0], ':')) { |
| 644 |
throw new MalformedUriException('A relative URI must not have a path beginning with a segment containing a colon'); |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
|