Uri.php 21 KB

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