Uri.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\UriInterface;
  4. /**
  5. * PSR-7 URI implementation.
  6. *
  7. * @author Michael Dowling
  8. * @author Tobias Schultze
  9. * @author Matthew Weier O'Phinney
  10. */
  11. class Uri implements UriInterface
  12. {
  13. /**
  14. * Absolute http and https URIs require a host per RFC 7230 Section 2.7
  15. * but in generic URIs the host can be empty. So for http(s) URIs
  16. * we apply this default host when no host is given yet to form a
  17. * valid URI.
  18. */
  19. const HTTP_DEFAULT_HOST = 'localhost';
  20. private static $defaultPorts = [
  21. 'http' => 80,
  22. 'https' => 443,
  23. 'ftp' => 21,
  24. 'gopher' => 70,
  25. 'nntp' => 119,
  26. 'news' => 119,
  27. 'telnet' => 23,
  28. 'tn3270' => 23,
  29. 'imap' => 143,
  30. 'pop' => 110,
  31. 'ldap' => 389,
  32. ];
  33. private static $charUnreserved = 'a-zA-Z0-9_\-\.~';
  34. private static $charSubDelims = '!\$&\'\(\)\*\+,;=';
  35. private static $replaceQuery = ['=' => '%3D', '&' => '%26'];
  36. /** @var string Uri scheme. */
  37. private $scheme = '';
  38. /** @var string Uri user info. */
  39. private $userInfo = '';
  40. /** @var string Uri host. */
  41. private $host = '';
  42. /** @var int|null Uri port. */
  43. private $port;
  44. /** @var string Uri path. */
  45. private $path = '';
  46. /** @var string Uri query string. */
  47. private $query = '';
  48. /** @var string Uri fragment. */
  49. private $fragment = '';
  50. /**
  51. * @param string $uri URI to parse
  52. */
  53. public function __construct($uri = '')
  54. {
  55. // weak type check to also accept null until we can add scalar type hints
  56. if ($uri != '') {
  57. $parts = self::parse($uri);
  58. if ($parts === false) {
  59. throw new \InvalidArgumentException("Unable to parse URI: $uri");
  60. }
  61. $this->applyParts($parts);
  62. }
  63. }
  64. /**
  65. * UTF-8 aware \parse_url() replacement.
  66. *
  67. * The internal function produces broken output for non ASCII domain names
  68. * (IDN) when used with locales other than "C".
  69. *
  70. * On the other hand, cURL understands IDN correctly only when UTF-8 locale
  71. * is configured ("C.UTF-8", "en_US.UTF-8", etc.).
  72. *
  73. * @see https://bugs.php.net/bug.php?id=52923
  74. * @see https://www.php.net/manual/en/function.parse-url.php#114817
  75. * @see https://curl.haxx.se/libcurl/c/CURLOPT_URL.html#ENCODING
  76. *
  77. * @param string $url
  78. *
  79. * @return array|false
  80. */
  81. private static function parse($url)
  82. {
  83. // If IPv6
  84. $prefix = '';
  85. if (preg_match('%^(.*://\[[0-9:a-f]+\])(.*?)$%', $url, $matches)) {
  86. $prefix = $matches[1];
  87. $url = $matches[2];
  88. }
  89. $encodedUrl = preg_replace_callback(
  90. '%[^:/@?&=#]+%usD',
  91. static function ($matches) {
  92. return urlencode($matches[0]);
  93. },
  94. $url
  95. );
  96. $result = parse_url($prefix . $encodedUrl);
  97. if ($result === false) {
  98. return false;
  99. }
  100. return array_map('urldecode', $result);
  101. }
  102. public function __toString()
  103. {
  104. return self::composeComponents(
  105. $this->scheme,
  106. $this->getAuthority(),
  107. $this->path,
  108. $this->query,
  109. $this->fragment
  110. );
  111. }
  112. /**
  113. * Composes a URI reference string from its various components.
  114. *
  115. * Usually this method does not need to be called manually but instead is used indirectly via
  116. * `Psr\Http\Message\UriInterface::__toString`.
  117. *
  118. * PSR-7 UriInterface treats an empty component the same as a missing component as
  119. * getQuery(), getFragment() etc. always return a string. This explains the slight
  120. * difference to RFC 3986 Section 5.3.
  121. *
  122. * Another adjustment is that the authority separator is added even when the authority is missing/empty
  123. * for the "file" scheme. This is because PHP stream functions like `file_get_contents` only work with
  124. * `file:///myfile` but not with `file:/myfile` although they are equivalent according to RFC 3986. But
  125. * `file:///` is the more common syntax for the file scheme anyway (Chrome for example redirects to
  126. * that format).
  127. *
  128. * @param string $scheme
  129. * @param string $authority
  130. * @param string $path
  131. * @param string $query
  132. * @param string $fragment
  133. *
  134. * @return string
  135. *
  136. * @link https://tools.ietf.org/html/rfc3986#section-5.3
  137. */
  138. public static function composeComponents($scheme, $authority, $path, $query, $fragment)
  139. {
  140. $uri = '';
  141. // weak type checks to also accept null until we can add scalar type hints
  142. if ($scheme != '') {
  143. $uri .= $scheme . ':';
  144. }
  145. if ($authority != ''|| $scheme === 'file') {
  146. $uri .= '//' . $authority;
  147. }
  148. $uri .= $path;
  149. if ($query != '') {
  150. $uri .= '?' . $query;
  151. }
  152. if ($fragment != '') {
  153. $uri .= '#' . $fragment;
  154. }
  155. return $uri;
  156. }
  157. /**
  158. * Whether the URI has the default port of the current scheme.
  159. *
  160. * `Psr\Http\Message\UriInterface::getPort` may return null or the standard port. This method can be used
  161. * independently of the implementation.
  162. *
  163. * @param UriInterface $uri
  164. *
  165. * @return bool
  166. */
  167. public static function isDefaultPort(UriInterface $uri)
  168. {
  169. return $uri->getPort() === null
  170. || (isset(self::$defaultPorts[$uri->getScheme()]) && $uri->getPort() === self::$defaultPorts[$uri->getScheme()]);
  171. }
  172. /**
  173. * Whether the URI is absolute, i.e. it has a scheme.
  174. *
  175. * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true
  176. * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative
  177. * to another URI, the base URI. Relative references can be divided into several forms:
  178. * - network-path references, e.g. '//example.com/path'
  179. * - absolute-path references, e.g. '/path'
  180. * - relative-path references, e.g. 'subpath'
  181. *
  182. * @param UriInterface $uri
  183. *
  184. * @return bool
  185. *
  186. * @see Uri::isNetworkPathReference
  187. * @see Uri::isAbsolutePathReference
  188. * @see Uri::isRelativePathReference
  189. * @link https://tools.ietf.org/html/rfc3986#section-4
  190. */
  191. public static function isAbsolute(UriInterface $uri)
  192. {
  193. return $uri->getScheme() !== '';
  194. }
  195. /**
  196. * Whether the URI is a network-path reference.
  197. *
  198. * A relative reference that begins with two slash characters is termed an network-path reference.
  199. *
  200. * @param UriInterface $uri
  201. *
  202. * @return bool
  203. *
  204. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  205. */
  206. public static function isNetworkPathReference(UriInterface $uri)
  207. {
  208. return $uri->getScheme() === '' && $uri->getAuthority() !== '';
  209. }
  210. /**
  211. * Whether the URI is a absolute-path reference.
  212. *
  213. * A relative reference that begins with a single slash character is termed an absolute-path reference.
  214. *
  215. * @param UriInterface $uri
  216. *
  217. * @return bool
  218. *
  219. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  220. */
  221. public static function isAbsolutePathReference(UriInterface $uri)
  222. {
  223. return $uri->getScheme() === ''
  224. && $uri->getAuthority() === ''
  225. && isset($uri->getPath()[0])
  226. && $uri->getPath()[0] === '/';
  227. }
  228. /**
  229. * Whether the URI is a relative-path reference.
  230. *
  231. * A relative reference that does not begin with a slash character is termed a relative-path reference.
  232. *
  233. * @param UriInterface $uri
  234. *
  235. * @return bool
  236. *
  237. * @link https://tools.ietf.org/html/rfc3986#section-4.2
  238. */
  239. public static function isRelativePathReference(UriInterface $uri)
  240. {
  241. return $uri->getScheme() === ''
  242. && $uri->getAuthority() === ''
  243. && (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/');
  244. }
  245. /**
  246. * Whether the URI is a same-document reference.
  247. *
  248. * A same-document reference refers to a URI that is, aside from its fragment
  249. * component, identical to the base URI. When no base URI is given, only an empty
  250. * URI reference (apart from its fragment) is considered a same-document reference.
  251. *
  252. * @param UriInterface $uri The URI to check
  253. * @param UriInterface|null $base An optional base URI to compare against
  254. *
  255. * @return bool
  256. *
  257. * @link https://tools.ietf.org/html/rfc3986#section-4.4
  258. */
  259. public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null)
  260. {
  261. if ($base !== null) {
  262. $uri = UriResolver::resolve($base, $uri);
  263. return ($uri->getScheme() === $base->getScheme())
  264. && ($uri->getAuthority() === $base->getAuthority())
  265. && ($uri->getPath() === $base->getPath())
  266. && ($uri->getQuery() === $base->getQuery());
  267. }
  268. return $uri->getScheme() === '' && $uri->getAuthority() === '' && $uri->getPath() === '' && $uri->getQuery() === '';
  269. }
  270. /**
  271. * Removes dot segments from a path and returns the new path.
  272. *
  273. * @param string $path
  274. *
  275. * @return string
  276. *
  277. * @deprecated since version 1.4. Use UriResolver::removeDotSegments instead.
  278. * @see UriResolver::removeDotSegments
  279. */
  280. public static function removeDotSegments($path)
  281. {
  282. return UriResolver::removeDotSegments($path);
  283. }
  284. /**
  285. * Converts the relative URI into a new URI that is resolved against the base URI.
  286. *
  287. * @param UriInterface $base Base URI
  288. * @param string|UriInterface $rel Relative URI
  289. *
  290. * @return UriInterface
  291. *
  292. * @deprecated since version 1.4. Use UriResolver::resolve instead.
  293. * @see UriResolver::resolve
  294. */
  295. public static function resolve(UriInterface $base, $rel)
  296. {
  297. if (!($rel instanceof UriInterface)) {
  298. $rel = new self($rel);
  299. }
  300. return UriResolver::resolve($base, $rel);
  301. }
  302. /**
  303. * Creates a new URI with a specific query string value removed.
  304. *
  305. * Any existing query string values that exactly match the provided key are
  306. * removed.
  307. *
  308. * @param UriInterface $uri URI to use as a base.
  309. * @param string $key Query string key to remove.
  310. *
  311. * @return UriInterface
  312. */
  313. public static function withoutQueryValue(UriInterface $uri, $key)
  314. {
  315. $result = self::getFilteredQueryString($uri, [$key]);
  316. return $uri->withQuery(implode('&', $result));
  317. }
  318. /**
  319. * Creates a new URI with a specific query string value.
  320. *
  321. * Any existing query string values that exactly match the provided key are
  322. * removed and replaced with the given key value pair.
  323. *
  324. * A value of null will set the query string key without a value, e.g. "key"
  325. * instead of "key=value".
  326. *
  327. * @param UriInterface $uri URI to use as a base.
  328. * @param string $key Key to set.
  329. * @param string|null $value Value to set
  330. *
  331. * @return UriInterface
  332. */
  333. public static function withQueryValue(UriInterface $uri, $key, $value)
  334. {
  335. $result = self::getFilteredQueryString($uri, [$key]);
  336. $result[] = self::generateQueryString($key, $value);
  337. return $uri->withQuery(implode('&', $result));
  338. }
  339. /**
  340. * Creates a new URI with multiple specific query string values.
  341. *
  342. * It has the same behavior as withQueryValue() but for an associative array of key => value.
  343. *
  344. * @param UriInterface $uri URI to use as a base.
  345. * @param array $keyValueArray Associative array of key and values
  346. *
  347. * @return UriInterface
  348. */
  349. public static function withQueryValues(UriInterface $uri, array $keyValueArray)
  350. {
  351. $result = self::getFilteredQueryString($uri, array_keys($keyValueArray));
  352. foreach ($keyValueArray as $key => $value) {
  353. $result[] = self::generateQueryString($key, $value);
  354. }
  355. return $uri->withQuery(implode('&', $result));
  356. }
  357. /**
  358. * Creates a URI from a hash of `parse_url` components.
  359. *
  360. * @param array $parts
  361. *
  362. * @return UriInterface
  363. *
  364. * @link http://php.net/manual/en/function.parse-url.php
  365. *
  366. * @throws \InvalidArgumentException If the components do not form a valid URI.
  367. */
  368. public static function fromParts(array $parts)
  369. {
  370. $uri = new self();
  371. $uri->applyParts($parts);
  372. $uri->validateState();
  373. return $uri;
  374. }
  375. public function getScheme()
  376. {
  377. return $this->scheme;
  378. }
  379. public function getAuthority()
  380. {
  381. $authority = $this->host;
  382. if ($this->userInfo !== '') {
  383. $authority = $this->userInfo . '@' . $authority;
  384. }
  385. if ($this->port !== null) {
  386. $authority .= ':' . $this->port;
  387. }
  388. return $authority;
  389. }
  390. public function getUserInfo()
  391. {
  392. return $this->userInfo;
  393. }
  394. public function getHost()
  395. {
  396. return $this->host;
  397. }
  398. public function getPort()
  399. {
  400. return $this->port;
  401. }
  402. public function getPath()
  403. {
  404. return $this->path;
  405. }
  406. public function getQuery()
  407. {
  408. return $this->query;
  409. }
  410. public function getFragment()
  411. {
  412. return $this->fragment;
  413. }
  414. public function withScheme($scheme)
  415. {
  416. $scheme = $this->filterScheme($scheme);
  417. if ($this->scheme === $scheme) {
  418. return $this;
  419. }
  420. $new = clone $this;
  421. $new->scheme = $scheme;
  422. $new->removeDefaultPort();
  423. $new->validateState();
  424. return $new;
  425. }
  426. public function withUserInfo($user, $password = null)
  427. {
  428. $info = $this->filterUserInfoComponent($user);
  429. if ($password !== null) {
  430. $info .= ':' . $this->filterUserInfoComponent($password);
  431. }
  432. if ($this->userInfo === $info) {
  433. return $this;
  434. }
  435. $new = clone $this;
  436. $new->userInfo = $info;
  437. $new->validateState();
  438. return $new;
  439. }
  440. public function withHost($host)
  441. {
  442. $host = $this->filterHost($host);
  443. if ($this->host === $host) {
  444. return $this;
  445. }
  446. $new = clone $this;
  447. $new->host = $host;
  448. $new->validateState();
  449. return $new;
  450. }
  451. public function withPort($port)
  452. {
  453. $port = $this->filterPort($port);
  454. if ($this->port === $port) {
  455. return $this;
  456. }
  457. $new = clone $this;
  458. $new->port = $port;
  459. $new->removeDefaultPort();
  460. $new->validateState();
  461. return $new;
  462. }
  463. public function withPath($path)
  464. {
  465. $path = $this->filterPath($path);
  466. if ($this->path === $path) {
  467. return $this;
  468. }
  469. $new = clone $this;
  470. $new->path = $path;
  471. $new->validateState();
  472. return $new;
  473. }
  474. public function withQuery($query)
  475. {
  476. $query = $this->filterQueryAndFragment($query);
  477. if ($this->query === $query) {
  478. return $this;
  479. }
  480. $new = clone $this;
  481. $new->query = $query;
  482. return $new;
  483. }
  484. public function withFragment($fragment)
  485. {
  486. $fragment = $this->filterQueryAndFragment($fragment);
  487. if ($this->fragment === $fragment) {
  488. return $this;
  489. }
  490. $new = clone $this;
  491. $new->fragment = $fragment;
  492. return $new;
  493. }
  494. /**
  495. * Apply parse_url parts to a URI.
  496. *
  497. * @param array $parts Array of parse_url parts to apply.
  498. */
  499. private function applyParts(array $parts)
  500. {
  501. $this->scheme = isset($parts['scheme'])
  502. ? $this->filterScheme($parts['scheme'])
  503. : '';
  504. $this->userInfo = isset($parts['user'])
  505. ? $this->filterUserInfoComponent($parts['user'])
  506. : '';
  507. $this->host = isset($parts['host'])
  508. ? $this->filterHost($parts['host'])
  509. : '';
  510. $this->port = isset($parts['port'])
  511. ? $this->filterPort($parts['port'])
  512. : null;
  513. $this->path = isset($parts['path'])
  514. ? $this->filterPath($parts['path'])
  515. : '';
  516. $this->query = isset($parts['query'])
  517. ? $this->filterQueryAndFragment($parts['query'])
  518. : '';
  519. $this->fragment = isset($parts['fragment'])
  520. ? $this->filterQueryAndFragment($parts['fragment'])
  521. : '';
  522. if (isset($parts['pass'])) {
  523. $this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']);
  524. }
  525. $this->removeDefaultPort();
  526. }
  527. /**
  528. * @param string $scheme
  529. *
  530. * @return string
  531. *
  532. * @throws \InvalidArgumentException If the scheme is invalid.
  533. */
  534. private function filterScheme($scheme)
  535. {
  536. if (!is_string($scheme)) {
  537. throw new \InvalidArgumentException('Scheme must be a string');
  538. }
  539. return \strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  540. }
  541. /**
  542. * @param string $component
  543. *
  544. * @return string
  545. *
  546. * @throws \InvalidArgumentException If the user info is invalid.
  547. */
  548. private function filterUserInfoComponent($component)
  549. {
  550. if (!is_string($component)) {
  551. throw new \InvalidArgumentException('User info must be a string');
  552. }
  553. return preg_replace_callback(
  554. '/(?:[^%' . self::$charUnreserved . self::$charSubDelims . ']+|%(?![A-Fa-f0-9]{2}))/',
  555. [$this, 'rawurlencodeMatchZero'],
  556. $component
  557. );
  558. }
  559. /**
  560. * @param string $host
  561. *
  562. * @return string
  563. *
  564. * @throws \InvalidArgumentException If the host is invalid.
  565. */
  566. private function filterHost($host)
  567. {
  568. if (!is_string($host)) {
  569. throw new \InvalidArgumentException('Host must be a string');
  570. }
  571. return \strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  572. }
  573. /**
  574. * @param int|null $port
  575. *
  576. * @return int|null
  577. *
  578. * @throws \InvalidArgumentException If the port is invalid.
  579. */
  580. private function filterPort($port)
  581. {
  582. if ($port === null) {
  583. return null;
  584. }
  585. $port = (int) $port;
  586. if (0 > $port || 0xffff < $port) {
  587. throw new \InvalidArgumentException(
  588. sprintf('Invalid port: %d. Must be between 0 and 65535', $port)
  589. );
  590. }
  591. return $port;
  592. }
  593. /**
  594. * @param UriInterface $uri
  595. * @param array $keys
  596. *
  597. * @return array
  598. */
  599. private static function getFilteredQueryString(UriInterface $uri, array $keys)
  600. {
  601. $current = $uri->getQuery();
  602. if ($current === '') {
  603. return [];
  604. }
  605. $decodedKeys = array_map('rawurldecode', $keys);
  606. return array_filter(explode('&', $current), function ($part) use ($decodedKeys) {
  607. return !in_array(rawurldecode(explode('=', $part)[0]), $decodedKeys, true);
  608. });
  609. }
  610. /**
  611. * @param string $key
  612. * @param string|null $value
  613. *
  614. * @return string
  615. */
  616. private static function generateQueryString($key, $value)
  617. {
  618. // Query string separators ("=", "&") within the key or value need to be encoded
  619. // (while preventing double-encoding) before setting the query string. All other
  620. // chars that need percent-encoding will be encoded by withQuery().
  621. $queryString = strtr($key, self::$replaceQuery);
  622. if ($value !== null) {
  623. $queryString .= '=' . strtr($value, self::$replaceQuery);
  624. }
  625. return $queryString;
  626. }
  627. private function removeDefaultPort()
  628. {
  629. if ($this->port !== null && self::isDefaultPort($this)) {
  630. $this->port = null;
  631. }
  632. }
  633. /**
  634. * Filters the path of a URI
  635. *
  636. * @param string $path
  637. *
  638. * @return string
  639. *
  640. * @throws \InvalidArgumentException If the path is invalid.
  641. */
  642. private function filterPath($path)
  643. {
  644. if (!is_string($path)) {
  645. throw new \InvalidArgumentException('Path must be a string');
  646. }
  647. return preg_replace_callback(
  648. '/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/',
  649. [$this, 'rawurlencodeMatchZero'],
  650. $path
  651. );
  652. }
  653. /**
  654. * Filters the query string or fragment of a URI.
  655. *
  656. * @param string $str
  657. *
  658. * @return string
  659. *
  660. * @throws \InvalidArgumentException If the query or fragment is invalid.
  661. */
  662. private function filterQueryAndFragment($str)
  663. {
  664. if (!is_string($str)) {
  665. throw new \InvalidArgumentException('Query and fragment must be a string');
  666. }
  667. return preg_replace_callback(
  668. '/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/',
  669. [$this, 'rawurlencodeMatchZero'],
  670. $str
  671. );
  672. }
  673. private function rawurlencodeMatchZero(array $match)
  674. {
  675. return rawurlencode($match[0]);
  676. }
  677. private function validateState()
  678. {
  679. if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) {
  680. $this->host = self::HTTP_DEFAULT_HOST;
  681. }
  682. if ($this->getAuthority() === '') {
  683. if (0 === strpos($this->path, '//')) {
  684. throw new \InvalidArgumentException('The path of a URI without an authority must not start with two slashes "//"');
  685. }
  686. if ($this->scheme === '' && false !== strpos(explode('/', $this->path, 2)[0], ':')) {
  687. throw new \InvalidArgumentException('A relative URI must not have a path beginning with a segment containing a colon');
  688. }
  689. } elseif (isset($this->path[0]) && $this->path[0] !== '/') {
  690. @trigger_error(
  691. 'The path of a URI with an authority must start with a slash "/" or be empty. Automagically fixing the URI ' .
  692. 'by adding a leading slash to the path is deprecated since version 1.4 and will throw an exception instead.',
  693. E_USER_DEPRECATED
  694. );
  695. $this->path = '/' . $this->path;
  696. //throw new \InvalidArgumentException('The path of a URI with an authority must start with a slash "/" or be empty');
  697. }
  698. }
  699. }