BaseUri.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri;
  12. use Deprecated;
  13. use JsonSerializable;
  14. use League\Uri\Contracts\UriAccess;
  15. use League\Uri\Contracts\UriInterface;
  16. use League\Uri\Exceptions\MissingFeature;
  17. use League\Uri\Idna\Converter as IdnaConverter;
  18. use League\Uri\IPv4\Converter as IPv4Converter;
  19. use League\Uri\IPv6\Converter as IPv6Converter;
  20. use Psr\Http\Message\UriFactoryInterface;
  21. use Psr\Http\Message\UriInterface as Psr7UriInterface;
  22. use Stringable;
  23. use function array_pop;
  24. use function array_reduce;
  25. use function count;
  26. use function explode;
  27. use function implode;
  28. use function in_array;
  29. use function preg_match;
  30. use function rawurldecode;
  31. use function sort;
  32. use function str_contains;
  33. use function str_repeat;
  34. use function str_replace;
  35. use function strpos;
  36. use function substr;
  37. /**
  38. * @phpstan-import-type ComponentMap from UriInterface
  39. * @deprecated since version 7.6.0
  40. *
  41. * @see Modifier
  42. * @see Uri
  43. */
  44. class BaseUri implements Stringable, JsonSerializable, UriAccess
  45. {
  46. /** @var array<string,int> */
  47. final protected const WHATWG_SPECIAL_SCHEMES = ['ftp' => 1, 'http' => 1, 'https' => 1, 'ws' => 1, 'wss' => 1];
  48. /** @var array<string,int> */
  49. final protected const DOT_SEGMENTS = ['.' => 1, '..' => 1];
  50. protected readonly Psr7UriInterface|UriInterface|null $origin;
  51. protected readonly ?string $nullValue;
  52. /**
  53. * @param UriFactoryInterface|null $uriFactory Deprecated, will be removed in the next major release
  54. */
  55. final protected function __construct(
  56. protected readonly Psr7UriInterface|UriInterface $uri,
  57. protected readonly ?UriFactoryInterface $uriFactory
  58. ) {
  59. $this->nullValue = $this->uri instanceof Psr7UriInterface ? '' : null;
  60. $this->origin = $this->computeOrigin($this->uri, $this->nullValue);
  61. }
  62. public static function from(Stringable|string $uri, ?UriFactoryInterface $uriFactory = null): static
  63. {
  64. $uri = static::formatHost(static::filterUri($uri, $uriFactory));
  65. return new static($uri, $uriFactory);
  66. }
  67. public function withUriFactory(UriFactoryInterface $uriFactory): static
  68. {
  69. return new static($this->uri, $uriFactory);
  70. }
  71. public function withoutUriFactory(): static
  72. {
  73. return new static($this->uri, null);
  74. }
  75. public function getUri(): Psr7UriInterface|UriInterface
  76. {
  77. return $this->uri;
  78. }
  79. public function getUriString(): string
  80. {
  81. return $this->uri->__toString();
  82. }
  83. public function jsonSerialize(): string
  84. {
  85. return $this->uri->__toString();
  86. }
  87. public function __toString(): string
  88. {
  89. return $this->uri->__toString();
  90. }
  91. public function origin(): ?self
  92. {
  93. return match (null) {
  94. $this->origin => null,
  95. default => new self($this->origin, $this->uriFactory),
  96. };
  97. }
  98. /**
  99. * Returns the Unix filesystem path.
  100. *
  101. * The method will return null if a scheme is present and is not the `file` scheme
  102. */
  103. public function unixPath(): ?string
  104. {
  105. return match ($this->uri->getScheme()) {
  106. 'file', $this->nullValue => rawurldecode($this->uri->getPath()),
  107. default => null,
  108. };
  109. }
  110. /**
  111. * Returns the Windows filesystem path.
  112. *
  113. * The method will return null if a scheme is present and is not the `file` scheme
  114. */
  115. public function windowsPath(): ?string
  116. {
  117. static $regexpWindowsPath = ',^(?<root>[a-zA-Z]:),';
  118. if (!in_array($this->uri->getScheme(), ['file', $this->nullValue], true)) {
  119. return null;
  120. }
  121. $originalPath = $this->uri->getPath();
  122. $path = $originalPath;
  123. if ('/' === ($path[0] ?? '')) {
  124. $path = substr($path, 1);
  125. }
  126. if (1 === preg_match($regexpWindowsPath, $path, $matches)) {
  127. $root = $matches['root'];
  128. $path = substr($path, strlen($root));
  129. return $root.str_replace('/', '\\', rawurldecode($path));
  130. }
  131. $host = $this->uri->getHost();
  132. return match ($this->nullValue) {
  133. $host => str_replace('/', '\\', rawurldecode($originalPath)),
  134. default => '\\\\'.$host.'\\'.str_replace('/', '\\', rawurldecode($path)),
  135. };
  136. }
  137. /**
  138. * Returns a string representation of a File URI according to RFC8089.
  139. *
  140. * The method will return null if the URI scheme is not the `file` scheme
  141. */
  142. public function toRfc8089(): ?string
  143. {
  144. $path = $this->uri->getPath();
  145. return match (true) {
  146. 'file' !== $this->uri->getScheme() => null,
  147. in_array($this->uri->getAuthority(), ['', null, 'localhost'], true) => 'file:'.match (true) {
  148. '' === $path,
  149. '/' === $path[0] => $path,
  150. default => '/'.$path,
  151. },
  152. default => (string) $this->uri,
  153. };
  154. }
  155. /**
  156. * Tells whether the `file` scheme base URI represents a local file.
  157. */
  158. public function isLocalFile(): bool
  159. {
  160. return match (true) {
  161. 'file' !== $this->uri->getScheme() => false,
  162. in_array($this->uri->getAuthority(), ['', null, 'localhost'], true) => true,
  163. default => false,
  164. };
  165. }
  166. /**
  167. * Tells whether the URI is opaque or not.
  168. *
  169. * A URI is opaque if and only if it is absolute
  170. * and does not have an authority path.
  171. */
  172. public function isOpaque(): bool
  173. {
  174. return $this->nullValue === $this->uri->getAuthority()
  175. && $this->isAbsolute();
  176. }
  177. /**
  178. * Tells whether two URI do not share the same origin.
  179. */
  180. public function isCrossOrigin(Stringable|string $uri): bool
  181. {
  182. if (null === $this->origin) {
  183. return true;
  184. }
  185. $uri = static::filterUri($uri);
  186. $uriOrigin = $this->computeOrigin($uri, $uri instanceof Psr7UriInterface ? '' : null);
  187. return match(true) {
  188. null === $uriOrigin,
  189. $uriOrigin->__toString() !== $this->origin->__toString() => true,
  190. default => false,
  191. };
  192. }
  193. /**
  194. * Tells whether the URI is absolute.
  195. */
  196. public function isAbsolute(): bool
  197. {
  198. return $this->nullValue !== $this->uri->getScheme();
  199. }
  200. /**
  201. * Tells whether the URI is a network path.
  202. */
  203. public function isNetworkPath(): bool
  204. {
  205. return $this->nullValue === $this->uri->getScheme()
  206. && $this->nullValue !== $this->uri->getAuthority();
  207. }
  208. /**
  209. * Tells whether the URI is an absolute path.
  210. */
  211. public function isAbsolutePath(): bool
  212. {
  213. return $this->nullValue === $this->uri->getScheme()
  214. && $this->nullValue === $this->uri->getAuthority()
  215. && '/' === ($this->uri->getPath()[0] ?? '');
  216. }
  217. /**
  218. * Tells whether the URI is a relative path.
  219. */
  220. public function isRelativePath(): bool
  221. {
  222. return $this->nullValue === $this->uri->getScheme()
  223. && $this->nullValue === $this->uri->getAuthority()
  224. && '/' !== ($this->uri->getPath()[0] ?? '');
  225. }
  226. /**
  227. * Tells whether both URI refers to the same document.
  228. */
  229. public function isSameDocument(Stringable|string $uri): bool
  230. {
  231. return self::normalizedUri($this->uri)->equals(self::normalizedUri($uri));
  232. }
  233. private static function normalizedUri(Stringable|string $uri): Uri
  234. {
  235. // Normalize the URI according to RFC3986
  236. $uri = ($uri instanceof Uri ? $uri : Uri::new($uri))->normalize();
  237. return $uri
  238. //Normalization as per WHATWG URL standard
  239. //only meaningful for WHATWG Special URI scheme protocol
  240. ->when(
  241. condition: '' === $uri->getPath() && null !== $uri->getAuthority(),
  242. onSuccess: fn (Uri $uri) => $uri->withPath('/'),
  243. )
  244. //Sorting as per WHATWG URLSearchParams class
  245. //not included on any equivalence algorithm
  246. ->when(
  247. condition: null !== ($query = $uri->getQuery()) && str_contains($query, '&'),
  248. onSuccess: function (Uri $uri) use ($query) {
  249. $pairs = explode('&', (string) $query);
  250. sort($pairs);
  251. return $uri->withQuery(implode('&', $pairs));
  252. }
  253. );
  254. }
  255. /**
  256. * Tells whether the URI contains an Internationalized Domain Name (IDN).
  257. */
  258. public function hasIdn(): bool
  259. {
  260. return IdnaConverter::isIdn($this->uri->getHost());
  261. }
  262. /**
  263. * Tells whether the URI contains an IPv4 regardless if it is mapped or native.
  264. */
  265. public function hasIPv4(): bool
  266. {
  267. return IPv4Converter::fromEnvironment()->isIpv4($this->uri->getHost());
  268. }
  269. /**
  270. * Resolves a URI against a base URI using RFC3986 rules.
  271. *
  272. * This method MUST retain the state of the submitted URI instance, and return
  273. * a URI instance of the same type that contains the applied modifications.
  274. *
  275. * This method MUST be transparent when dealing with error and exceptions.
  276. * It MUST not alter or silence them apart from validating its own parameters.
  277. */
  278. public function resolve(Stringable|string $uri): static
  279. {
  280. $resolved = UriString::resolve($uri, $this->uri);
  281. return new static(match ($this->uriFactory) {
  282. null => Uri::new($resolved),
  283. default => $this->uriFactory->createUri($resolved),
  284. }, $this->uriFactory);
  285. }
  286. /**
  287. * Relativize a URI according to a base URI.
  288. *
  289. * This method MUST retain the state of the submitted URI instance, and return
  290. * a URI instance of the same type that contains the applied modifications.
  291. *
  292. * This method MUST be transparent when dealing with error and exceptions.
  293. * It MUST not alter of silence them apart from validating its own parameters.
  294. */
  295. public function relativize(Stringable|string $uri): static
  296. {
  297. $uri = static::formatHost(static::filterUri($uri, $this->uriFactory));
  298. if ($this->canNotBeRelativize($uri)) {
  299. return new static($uri, $this->uriFactory);
  300. }
  301. $null = $uri instanceof Psr7UriInterface ? '' : null;
  302. $uri = $uri->withScheme($null)->withPort(null)->withUserInfo($null)->withHost($null);
  303. $targetPath = $uri->getPath();
  304. $basePath = $this->uri->getPath();
  305. return new static(
  306. match (true) {
  307. $targetPath !== $basePath => $uri->withPath(static::relativizePath($targetPath, $basePath)),
  308. static::componentEquals('query', $uri) => $uri->withPath('')->withQuery($null),
  309. $null === $uri->getQuery() => $uri->withPath(static::formatPathWithEmptyBaseQuery($targetPath)),
  310. default => $uri->withPath(''),
  311. },
  312. $this->uriFactory
  313. );
  314. }
  315. final protected function computeOrigin(Psr7UriInterface|UriInterface $uri, ?string $nullValue): Psr7UriInterface|UriInterface|null
  316. {
  317. if ($uri instanceof Uri) {
  318. $origin = $uri->getOrigin();
  319. if (null === $origin) {
  320. return null;
  321. }
  322. return Uri::tryNew($origin);
  323. }
  324. $origin = Uri::tryNew($uri)?->getOrigin();
  325. if (null === $origin) {
  326. return null;
  327. }
  328. $components = UriString::parse($origin);
  329. return $uri
  330. ->withFragment($nullValue)
  331. ->withQuery($nullValue)
  332. ->withPath('')
  333. ->withScheme('localhost')
  334. ->withHost((string) $components['host'])
  335. ->withPort($components['port'])
  336. ->withScheme((string) $components['scheme'])
  337. ->withUserInfo($nullValue);
  338. }
  339. /**
  340. * Input URI normalization to allow Stringable and string URI.
  341. */
  342. final protected static function filterUri(Stringable|string $uri, UriFactoryInterface|null $uriFactory = null): Psr7UriInterface|UriInterface
  343. {
  344. return match (true) {
  345. $uri instanceof UriAccess => $uri->getUri(),
  346. $uri instanceof Psr7UriInterface,
  347. $uri instanceof UriInterface => $uri,
  348. $uriFactory instanceof UriFactoryInterface => $uriFactory->createUri((string) $uri),
  349. default => Uri::new($uri),
  350. };
  351. }
  352. /**
  353. * Tells whether the component value from both URI object equals.
  354. *
  355. * @pqram 'query'|'authority'|'scheme' $property
  356. */
  357. final protected function componentEquals(string $property, Psr7UriInterface|UriInterface $uri): bool
  358. {
  359. $getComponent = function (string $property, Psr7UriInterface|UriInterface $uri): ?string {
  360. $component = match ($property) {
  361. 'query' => $uri->getQuery(),
  362. 'authority' => $uri->getAuthority(),
  363. default => $uri->getScheme(),
  364. };
  365. return match (true) {
  366. $uri instanceof UriInterface, '' !== $component => $component,
  367. default => null,
  368. };
  369. };
  370. return $getComponent($property, $uri) === $getComponent($property, $this->uri);
  371. }
  372. /**
  373. * Filter the URI object.
  374. */
  375. final protected static function formatHost(Psr7UriInterface|UriInterface $uri): Psr7UriInterface|UriInterface
  376. {
  377. $host = $uri->getHost();
  378. try {
  379. $converted = IPv4Converter::fromEnvironment()->toDecimal($host);
  380. } catch (MissingFeature) {
  381. $converted = null;
  382. }
  383. if (false === filter_var($converted, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  384. $converted = IPv6Converter::compress($host);
  385. }
  386. return match (true) {
  387. null !== $converted => $uri->withHost($converted),
  388. '' === $host,
  389. $uri instanceof UriInterface => $uri,
  390. default => $uri->withHost((string) Uri::fromComponents(['host' => $host])->getHost()),
  391. };
  392. }
  393. /**
  394. * Tells whether the submitted URI object can be relativized.
  395. */
  396. final protected function canNotBeRelativize(Psr7UriInterface|UriInterface $uri): bool
  397. {
  398. return !static::componentEquals('scheme', $uri)
  399. || !static::componentEquals('authority', $uri)
  400. || static::from($uri)->isRelativePath();
  401. }
  402. /**
  403. * Relatives the URI for an authority-less target URI.
  404. */
  405. final protected static function relativizePath(string $path, string $basePath): string
  406. {
  407. $baseSegments = static::getSegments($basePath);
  408. $targetSegments = static::getSegments($path);
  409. $targetBasename = array_pop($targetSegments);
  410. array_pop($baseSegments);
  411. foreach ($baseSegments as $offset => $segment) {
  412. if (!isset($targetSegments[$offset]) || $segment !== $targetSegments[$offset]) {
  413. break;
  414. }
  415. unset($baseSegments[$offset], $targetSegments[$offset]);
  416. }
  417. $targetSegments[] = $targetBasename;
  418. return static::formatPath(
  419. str_repeat('../', count($baseSegments)).implode('/', $targetSegments),
  420. $basePath
  421. );
  422. }
  423. /**
  424. * returns the path segments.
  425. *
  426. * @return string[]
  427. */
  428. final protected static function getSegments(string $path): array
  429. {
  430. return explode('/', match (true) {
  431. '' === $path,
  432. '/' !== $path[0] => $path,
  433. default => substr($path, 1),
  434. });
  435. }
  436. /**
  437. * Formatting the path to keep a valid URI.
  438. */
  439. final protected static function formatPath(string $path, string $basePath): string
  440. {
  441. $colonPosition = strpos($path, ':');
  442. $slashPosition = strpos($path, '/');
  443. return match (true) {
  444. '' === $path => match (true) {
  445. '' === $basePath,
  446. '/' === $basePath => $basePath,
  447. default => './',
  448. },
  449. false === $colonPosition => $path,
  450. false === $slashPosition,
  451. $colonPosition < $slashPosition => "./$path",
  452. default => $path,
  453. };
  454. }
  455. /**
  456. * Formatting the path to keep a resolvable URI.
  457. */
  458. final protected static function formatPathWithEmptyBaseQuery(string $path): string
  459. {
  460. $targetSegments = static::getSegments($path);
  461. $basename = $targetSegments[array_key_last($targetSegments)];
  462. return '' === $basename ? './' : $basename;
  463. }
  464. /**
  465. * Normalizes a URI for comparison; this URI string representation is not suitable for usage as per RFC guidelines.
  466. *
  467. * @deprecated since version 7.6.0
  468. *
  469. * @codeCoverageIgnore
  470. */
  471. #[Deprecated(message:'no longer used by the isSameDocument method', since:'league/uri-interfaces:7.6.0')]
  472. final protected function normalize(Psr7UriInterface|UriInterface $uri): string
  473. {
  474. $newUri = $uri->withScheme($uri instanceof Psr7UriInterface ? '' : null);
  475. if ('' === $newUri->__toString()) {
  476. return '';
  477. }
  478. return UriString::normalize($newUri);
  479. }
  480. /**
  481. * Remove dot segments from the URI path as per RFC specification.
  482. *
  483. * @deprecated since version 7.6.0
  484. *
  485. * @codeCoverageIgnore
  486. */
  487. #[Deprecated(message:'no longer used by the isSameDocument method', since:'league/uri-interfaces:7.6.0')]
  488. final protected function removeDotSegments(string $path): string
  489. {
  490. if (!str_contains($path, '.')) {
  491. return $path;
  492. }
  493. $reducer = function (array $carry, string $segment): array {
  494. if ('..' === $segment) {
  495. array_pop($carry);
  496. return $carry;
  497. }
  498. if (!isset(static::DOT_SEGMENTS[$segment])) {
  499. $carry[] = $segment;
  500. }
  501. return $carry;
  502. };
  503. $oldSegments = explode('/', $path);
  504. $newPath = implode('/', array_reduce($oldSegments, $reducer(...), []));
  505. if (isset(static::DOT_SEGMENTS[$oldSegments[array_key_last($oldSegments)]])) {
  506. $newPath .= '/';
  507. }
  508. // @codeCoverageIgnoreStart
  509. // added because some PSR-7 implementations do not respect RFC3986
  510. if (str_starts_with($path, '/') && !str_starts_with($newPath, '/')) {
  511. return '/'.$newPath;
  512. }
  513. // @codeCoverageIgnoreEnd
  514. return $newPath;
  515. }
  516. /**
  517. * Resolves an URI path and query component.
  518. *
  519. * @return array{0:string, 1:string|null}
  520. *
  521. * @deprecated since version 7.6.0
  522. *
  523. * @codeCoverageIgnore
  524. */
  525. #[Deprecated(message:'no longer used by the isSameDocument method', since:'league/uri-interfaces:7.6.0')]
  526. final protected function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array
  527. {
  528. $targetPath = $uri->getPath();
  529. $null = $uri instanceof Psr7UriInterface ? '' : null;
  530. if (str_starts_with($targetPath, '/')) {
  531. return [$targetPath, $uri->getQuery()];
  532. }
  533. if ('' === $targetPath) {
  534. $targetQuery = $uri->getQuery();
  535. if ($null === $targetQuery) {
  536. $targetQuery = $this->uri->getQuery();
  537. }
  538. $targetPath = $this->uri->getPath();
  539. //@codeCoverageIgnoreStart
  540. //because some PSR-7 Uri implementations allow this RFC3986 forbidden construction
  541. if (null !== $this->uri->getAuthority() && !str_starts_with($targetPath, '/')) {
  542. $targetPath = '/'.$targetPath;
  543. }
  544. //@codeCoverageIgnoreEnd
  545. return [$targetPath, $targetQuery];
  546. }
  547. $basePath = $this->uri->getPath();
  548. if (null !== $this->uri->getAuthority() && '' === $basePath) {
  549. $targetPath = '/'.$targetPath;
  550. }
  551. if ('' !== $basePath) {
  552. $segments = explode('/', $basePath);
  553. array_pop($segments);
  554. if ([] !== $segments) {
  555. $targetPath = implode('/', $segments).'/'.$targetPath;
  556. }
  557. }
  558. return [$targetPath, $uri->getQuery()];
  559. }
  560. }