You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 23 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. # PSR-7 Message Implementation
  2. This repository contains a full [PSR-7](http://www.php-fig.org/psr/psr-7/)
  3. message implementation, several stream decorators, and some helpful
  4. functionality like query string parsing.
  5. [![Build Status](https://travis-ci.org/guzzle/psr7.svg?branch=master)](https://travis-ci.org/guzzle/psr7)
  6. # Stream implementation
  7. This package comes with a number of stream implementations and stream
  8. decorators.
  9. ## AppendStream
  10. `GuzzleHttp\Psr7\AppendStream`
  11. Reads from multiple streams, one after the other.
  12. ```php
  13. use GuzzleHttp\Psr7;
  14. $a = Psr7\stream_for('abc, ');
  15. $b = Psr7\stream_for('123.');
  16. $composed = new Psr7\AppendStream([$a, $b]);
  17. $composed->addStream(Psr7\stream_for(' Above all listen to me'));
  18. echo $composed; // abc, 123. Above all listen to me.
  19. ```
  20. ## BufferStream
  21. `GuzzleHttp\Psr7\BufferStream`
  22. Provides a buffer stream that can be written to fill a buffer, and read
  23. from to remove bytes from the buffer.
  24. This stream returns a "hwm" metadata value that tells upstream consumers
  25. what the configured high water mark of the stream is, or the maximum
  26. preferred size of the buffer.
  27. ```php
  28. use GuzzleHttp\Psr7;
  29. // When more than 1024 bytes are in the buffer, it will begin returning
  30. // false to writes. This is an indication that writers should slow down.
  31. $buffer = new Psr7\BufferStream(1024);
  32. ```
  33. ## CachingStream
  34. The CachingStream is used to allow seeking over previously read bytes on
  35. non-seekable streams. This can be useful when transferring a non-seekable
  36. entity body fails due to needing to rewind the stream (for example, resulting
  37. from a redirect). Data that is read from the remote stream will be buffered in
  38. a PHP temp stream so that previously read bytes are cached first in memory,
  39. then on disk.
  40. ```php
  41. use GuzzleHttp\Psr7;
  42. $original = Psr7\stream_for(fopen('http://www.google.com', 'r'));
  43. $stream = new Psr7\CachingStream($original);
  44. $stream->read(1024);
  45. echo $stream->tell();
  46. // 1024
  47. $stream->seek(0);
  48. echo $stream->tell();
  49. // 0
  50. ```
  51. ## DroppingStream
  52. `GuzzleHttp\Psr7\DroppingStream`
  53. Stream decorator that begins dropping data once the size of the underlying
  54. stream becomes too full.
  55. ```php
  56. use GuzzleHttp\Psr7;
  57. // Create an empty stream
  58. $stream = Psr7\stream_for();
  59. // Start dropping data when the stream has more than 10 bytes
  60. $dropping = new Psr7\DroppingStream($stream, 10);
  61. $dropping->write('01234567890123456789');
  62. echo $stream; // 0123456789
  63. ```
  64. ## FnStream
  65. `GuzzleHttp\Psr7\FnStream`
  66. Compose stream implementations based on a hash of functions.
  67. Allows for easy testing and extension of a provided stream without needing
  68. to create a concrete class for a simple extension point.
  69. ```php
  70. use GuzzleHttp\Psr7;
  71. $stream = Psr7\stream_for('hi');
  72. $fnStream = Psr7\FnStream::decorate($stream, [
  73. 'rewind' => function () use ($stream) {
  74. echo 'About to rewind - ';
  75. $stream->rewind();
  76. echo 'rewound!';
  77. }
  78. ]);
  79. $fnStream->rewind();
  80. // Outputs: About to rewind - rewound!
  81. ```
  82. ## InflateStream
  83. `GuzzleHttp\Psr7\InflateStream`
  84. Uses PHP's zlib.inflate filter to inflate deflate or gzipped content.
  85. This stream decorator skips the first 10 bytes of the given stream to remove
  86. the gzip header, converts the provided stream to a PHP stream resource,
  87. then appends the zlib.inflate filter. The stream is then converted back
  88. to a Guzzle stream resource to be used as a Guzzle stream.
  89. ## LazyOpenStream
  90. `GuzzleHttp\Psr7\LazyOpenStream`
  91. Lazily reads or writes to a file that is opened only after an IO operation
  92. take place on the stream.
  93. ```php
  94. use GuzzleHttp\Psr7;
  95. $stream = new Psr7\LazyOpenStream('/path/to/file', 'r');
  96. // The file has not yet been opened...
  97. echo $stream->read(10);
  98. // The file is opened and read from only when needed.
  99. ```
  100. ## LimitStream
  101. `GuzzleHttp\Psr7\LimitStream`
  102. LimitStream can be used to read a subset or slice of an existing stream object.
  103. This can be useful for breaking a large file into smaller pieces to be sent in
  104. chunks (e.g. Amazon S3's multipart upload API).
  105. ```php
  106. use GuzzleHttp\Psr7;
  107. $original = Psr7\stream_for(fopen('/tmp/test.txt', 'r+'));
  108. echo $original->getSize();
  109. // >>> 1048576
  110. // Limit the size of the body to 1024 bytes and start reading from byte 2048
  111. $stream = new Psr7\LimitStream($original, 1024, 2048);
  112. echo $stream->getSize();
  113. // >>> 1024
  114. echo $stream->tell();
  115. // >>> 0
  116. ```
  117. ## MultipartStream
  118. `GuzzleHttp\Psr7\MultipartStream`
  119. Stream that when read returns bytes for a streaming multipart or
  120. multipart/form-data stream.
  121. ## NoSeekStream
  122. `GuzzleHttp\Psr7\NoSeekStream`
  123. NoSeekStream wraps a stream and does not allow seeking.
  124. ```php
  125. use GuzzleHttp\Psr7;
  126. $original = Psr7\stream_for('foo');
  127. $noSeek = new Psr7\NoSeekStream($original);
  128. echo $noSeek->read(3);
  129. // foo
  130. var_export($noSeek->isSeekable());
  131. // false
  132. $noSeek->seek(0);
  133. var_export($noSeek->read(3));
  134. // NULL
  135. ```
  136. ## PumpStream
  137. `GuzzleHttp\Psr7\PumpStream`
  138. Provides a read only stream that pumps data from a PHP callable.
  139. When invoking the provided callable, the PumpStream will pass the amount of
  140. data requested to read to the callable. The callable can choose to ignore
  141. this value and return fewer or more bytes than requested. Any extra data
  142. returned by the provided callable is buffered internally until drained using
  143. the read() function of the PumpStream. The provided callable MUST return
  144. false when there is no more data to read.
  145. ## Implementing stream decorators
  146. Creating a stream decorator is very easy thanks to the
  147. `GuzzleHttp\Psr7\StreamDecoratorTrait`. This trait provides methods that
  148. implement `Psr\Http\Message\StreamInterface` by proxying to an underlying
  149. stream. Just `use` the `StreamDecoratorTrait` and implement your custom
  150. methods.
  151. For example, let's say we wanted to call a specific function each time the last
  152. byte is read from a stream. This could be implemented by overriding the
  153. `read()` method.
  154. ```php
  155. use Psr\Http\Message\StreamInterface;
  156. use GuzzleHttp\Psr7\StreamDecoratorTrait;
  157. class EofCallbackStream implements StreamInterface
  158. {
  159. use StreamDecoratorTrait;
  160. private $callback;
  161. public function __construct(StreamInterface $stream, callable $cb)
  162. {
  163. $this->stream = $stream;
  164. $this->callback = $cb;
  165. }
  166. public function read($length)
  167. {
  168. $result = $this->stream->read($length);
  169. // Invoke the callback when EOF is hit.
  170. if ($this->eof()) {
  171. call_user_func($this->callback);
  172. }
  173. return $result;
  174. }
  175. }
  176. ```
  177. This decorator could be added to any existing stream and used like so:
  178. ```php
  179. use GuzzleHttp\Psr7;
  180. $original = Psr7\stream_for('foo');
  181. $eofStream = new EofCallbackStream($original, function () {
  182. echo 'EOF!';
  183. });
  184. $eofStream->read(2);
  185. $eofStream->read(1);
  186. // echoes "EOF!"
  187. $eofStream->seek(0);
  188. $eofStream->read(3);
  189. // echoes "EOF!"
  190. ```
  191. ## PHP StreamWrapper
  192. You can use the `GuzzleHttp\Psr7\StreamWrapper` class if you need to use a
  193. PSR-7 stream as a PHP stream resource.
  194. Use the `GuzzleHttp\Psr7\StreamWrapper::getResource()` method to create a PHP
  195. stream from a PSR-7 stream.
  196. ```php
  197. use GuzzleHttp\Psr7\StreamWrapper;
  198. $stream = GuzzleHttp\Psr7\stream_for('hello!');
  199. $resource = StreamWrapper::getResource($stream);
  200. echo fread($resource, 6); // outputs hello!
  201. ```
  202. # Function API
  203. There are various functions available under the `GuzzleHttp\Psr7` namespace.
  204. ## `function str`
  205. `function str(MessageInterface $message)`
  206. Returns the string representation of an HTTP message.
  207. ```php
  208. $request = new GuzzleHttp\Psr7\Request('GET', 'http://example.com');
  209. echo GuzzleHttp\Psr7\str($request);
  210. ```
  211. ## `function uri_for`
  212. `function uri_for($uri)`
  213. This function accepts a string or `Psr\Http\Message\UriInterface` and returns a
  214. UriInterface for the given value. If the value is already a `UriInterface`, it
  215. is returned as-is.
  216. ```php
  217. $uri = GuzzleHttp\Psr7\uri_for('http://example.com');
  218. assert($uri === GuzzleHttp\Psr7\uri_for($uri));
  219. ```
  220. ## `function stream_for`
  221. `function stream_for($resource = '', array $options = [])`
  222. Create a new stream based on the input type.
  223. Options is an associative array that can contain the following keys:
  224. * - metadata: Array of custom metadata.
  225. * - size: Size of the stream.
  226. This method accepts the following `$resource` types:
  227. - `Psr\Http\Message\StreamInterface`: Returns the value as-is.
  228. - `string`: Creates a stream object that uses the given string as the contents.
  229. - `resource`: Creates a stream object that wraps the given PHP stream resource.
  230. - `Iterator`: If the provided value implements `Iterator`, then a read-only
  231. stream object will be created that wraps the given iterable. Each time the
  232. stream is read from, data from the iterator will fill a buffer and will be
  233. continuously called until the buffer is equal to the requested read size.
  234. Subsequent read calls will first read from the buffer and then call `next`
  235. on the underlying iterator until it is exhausted.
  236. - `object` with `__toString()`: If the object has the `__toString()` method,
  237. the object will be cast to a string and then a stream will be returned that
  238. uses the string value.
  239. - `NULL`: When `null` is passed, an empty stream object is returned.
  240. - `callable` When a callable is passed, a read-only stream object will be
  241. created that invokes the given callable. The callable is invoked with the
  242. number of suggested bytes to read. The callable can return any number of
  243. bytes, but MUST return `false` when there is no more data to return. The
  244. stream object that wraps the callable will invoke the callable until the
  245. number of requested bytes are available. Any additional bytes will be
  246. buffered and used in subsequent reads.
  247. ```php
  248. $stream = GuzzleHttp\Psr7\stream_for('foo');
  249. $stream = GuzzleHttp\Psr7\stream_for(fopen('/path/to/file', 'r'));
  250. $generator = function ($bytes) {
  251. for ($i = 0; $i < $bytes; $i++) {
  252. yield ' ';
  253. }
  254. }
  255. $stream = GuzzleHttp\Psr7\stream_for($generator(100));
  256. ```
  257. ## `function parse_header`
  258. `function parse_header($header)`
  259. Parse an array of header values containing ";" separated data into an array of
  260. associative arrays representing the header key value pair data of the header.
  261. When a parameter does not contain a value, but just contains a key, this
  262. function will inject a key with a '' string value.
  263. ## `function normalize_header`
  264. `function normalize_header($header)`
  265. Converts an array of header values that may contain comma separated headers
  266. into an array of headers with no comma separated values.
  267. ## `function modify_request`
  268. `function modify_request(RequestInterface $request, array $changes)`
  269. Clone and modify a request with the given changes. This method is useful for
  270. reducing the number of clones needed to mutate a message.
  271. The changes can be one of:
  272. - method: (string) Changes the HTTP method.
  273. - set_headers: (array) Sets the given headers.
  274. - remove_headers: (array) Remove the given headers.
  275. - body: (mixed) Sets the given body.
  276. - uri: (UriInterface) Set the URI.
  277. - query: (string) Set the query string value of the URI.
  278. - version: (string) Set the protocol version.
  279. ## `function rewind_body`
  280. `function rewind_body(MessageInterface $message)`
  281. Attempts to rewind a message body and throws an exception on failure. The body
  282. of the message will only be rewound if a call to `tell()` returns a value other
  283. than `0`.
  284. ## `function try_fopen`
  285. `function try_fopen($filename, $mode)`
  286. Safely opens a PHP stream resource using a filename.
  287. When fopen fails, PHP normally raises a warning. This function adds an error
  288. handler that checks for errors and throws an exception instead.
  289. ## `function copy_to_string`
  290. `function copy_to_string(StreamInterface $stream, $maxLen = -1)`
  291. Copy the contents of a stream into a string until the given number of bytes
  292. have been read.
  293. ## `function copy_to_stream`
  294. `function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)`
  295. Copy the contents of a stream into another stream until the given number of
  296. bytes have been read.
  297. ## `function hash`
  298. `function hash(StreamInterface $stream, $algo, $rawOutput = false)`
  299. Calculate a hash of a Stream. This method reads the entire stream to calculate
  300. a rolling hash (based on PHP's hash_init functions).
  301. ## `function readline`
  302. `function readline(StreamInterface $stream, $maxLength = null)`
  303. Read a line from the stream up to the maximum allowed buffer length.
  304. ## `function parse_request`
  305. `function parse_request($message)`
  306. Parses a request message string into a request object.
  307. ## `function parse_response`
  308. `function parse_response($message)`
  309. Parses a response message string into a response object.
  310. ## `function parse_query`
  311. `function parse_query($str, $urlEncoding = true)`
  312. Parse a query string into an associative array.
  313. If multiple values are found for the same key, the value of that key value pair
  314. will become an array. This function does not parse nested PHP style arrays into
  315. an associative array (e.g., `foo[a]=1&foo[b]=2` will be parsed into
  316. `['foo[a]' => '1', 'foo[b]' => '2']`).
  317. ## `function build_query`
  318. `function build_query(array $params, $encoding = PHP_QUERY_RFC3986)`
  319. Build a query string from an array of key value pairs.
  320. This function can use the return value of parse_query() to build a query string.
  321. This function does not modify the provided keys when an array is encountered
  322. (like http_build_query would).
  323. ## `function mimetype_from_filename`
  324. `function mimetype_from_filename($filename)`
  325. Determines the mimetype of a file by looking at its extension.
  326. ## `function mimetype_from_extension`
  327. `function mimetype_from_extension($extension)`
  328. Maps a file extensions to a mimetype.
  329. # Additional URI Methods
  330. Aside from the standard `Psr\Http\Message\UriInterface` implementation in form of the `GuzzleHttp\Psr7\Uri` class,
  331. this library also provides additional functionality when working with URIs as static methods.
  332. ## URI Types
  333. An instance of `Psr\Http\Message\UriInterface` can either be an absolute URI or a relative reference.
  334. An absolute URI has a scheme. A relative reference is used to express a URI relative to another URI,
  335. the base URI. Relative references can be divided into several forms according to
  336. [RFC 3986 Section 4.2](https://tools.ietf.org/html/rfc3986#section-4.2):
  337. - network-path references, e.g. `//example.com/path`
  338. - absolute-path references, e.g. `/path`
  339. - relative-path references, e.g. `subpath`
  340. The following methods can be used to identify the type of the URI.
  341. ### `GuzzleHttp\Psr7\Uri::isAbsolute`
  342. `public static function isAbsolute(UriInterface $uri): bool`
  343. Whether the URI is absolute, i.e. it has a scheme.
  344. ### `GuzzleHttp\Psr7\Uri::isNetworkPathReference`
  345. `public static function isNetworkPathReference(UriInterface $uri): bool`
  346. Whether the URI is a network-path reference. A relative reference that begins with two slash characters is
  347. termed an network-path reference.
  348. ### `GuzzleHttp\Psr7\Uri::isAbsolutePathReference`
  349. `public static function isAbsolutePathReference(UriInterface $uri): bool`
  350. Whether the URI is a absolute-path reference. A relative reference that begins with a single slash character is
  351. termed an absolute-path reference.
  352. ### `GuzzleHttp\Psr7\Uri::isRelativePathReference`
  353. `public static function isRelativePathReference(UriInterface $uri): bool`
  354. Whether the URI is a relative-path reference. A relative reference that does not begin with a slash character is
  355. termed a relative-path reference.
  356. ### `GuzzleHttp\Psr7\Uri::isSameDocumentReference`
  357. `public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null): bool`
  358. Whether the URI is a same-document reference. A same-document reference refers to a URI that is, aside from its
  359. fragment component, identical to the base URI. When no base URI is given, only an empty URI reference
  360. (apart from its fragment) is considered a same-document reference.
  361. ## URI Components
  362. Additional methods to work with URI components.
  363. ### `GuzzleHttp\Psr7\Uri::isDefaultPort`
  364. `public static function isDefaultPort(UriInterface $uri): bool`
  365. Whether the URI has the default port of the current scheme. `Psr\Http\Message\UriInterface::getPort` may return null
  366. or the standard port. This method can be used independently of the implementation.
  367. ### `GuzzleHttp\Psr7\Uri::composeComponents`
  368. `public static function composeComponents($scheme, $authority, $path, $query, $fragment): string`
  369. Composes a URI reference string from its various components according to
  370. [RFC 3986 Section 5.3](https://tools.ietf.org/html/rfc3986#section-5.3). Usually this method does not need to be called
  371. manually but instead is used indirectly via `Psr\Http\Message\UriInterface::__toString`.
  372. ### `GuzzleHttp\Psr7\Uri::fromParts`
  373. `public static function fromParts(array $parts): UriInterface`
  374. Creates a URI from a hash of [`parse_url`](http://php.net/manual/en/function.parse-url.php) components.
  375. ### `GuzzleHttp\Psr7\Uri::withQueryValue`
  376. `public static function withQueryValue(UriInterface $uri, $key, $value): UriInterface`
  377. Creates a new URI with a specific query string value. Any existing query string values that exactly match the
  378. provided key are removed and replaced with the given key value pair. A value of null will set the query string
  379. key without a value, e.g. "key" instead of "key=value".
  380. ### `GuzzleHttp\Psr7\Uri::withQueryValues`
  381. `public static function withQueryValues(UriInterface $uri, array $keyValueArray): UriInterface`
  382. Creates a new URI with multiple query string values. It has the same behavior as `withQueryValue()` but for an
  383. associative array of key => value.
  384. ### `GuzzleHttp\Psr7\Uri::withoutQueryValue`
  385. `public static function withoutQueryValue(UriInterface $uri, $key): UriInterface`
  386. Creates a new URI with a specific query string value removed. Any existing query string values that exactly match the
  387. provided key are removed.
  388. ## Reference Resolution
  389. `GuzzleHttp\Psr7\UriResolver` provides methods to resolve a URI reference in the context of a base URI according
  390. to [RFC 3986 Section 5](https://tools.ietf.org/html/rfc3986#section-5). This is for example also what web browsers
  391. do when resolving a link in a website based on the current request URI.
  392. ### `GuzzleHttp\Psr7\UriResolver::resolve`
  393. `public static function resolve(UriInterface $base, UriInterface $rel): UriInterface`
  394. Converts the relative URI into a new URI that is resolved against the base URI.
  395. ### `GuzzleHttp\Psr7\UriResolver::removeDotSegments`
  396. `public static function removeDotSegments(string $path): string`
  397. Removes dot segments from a path and returns the new path according to
  398. [RFC 3986 Section 5.2.4](https://tools.ietf.org/html/rfc3986#section-5.2.4).
  399. ### `GuzzleHttp\Psr7\UriResolver::relativize`
  400. `public static function relativize(UriInterface $base, UriInterface $target): UriInterface`
  401. Returns the target URI as a relative reference from the base URI. This method is the counterpart to resolve():
  402. ```php
  403. (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
  404. ```
  405. One use-case is to use the current request URI as base URI and then generate relative links in your documents
  406. to reduce the document size or offer self-contained downloadable document archives.
  407. ```php
  408. $base = new Uri('http://example.com/a/b/');
  409. echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'.
  410. echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'.
  411. echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
  412. echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
  413. ```
  414. ## Normalization and Comparison
  415. `GuzzleHttp\Psr7\UriNormalizer` provides methods to normalize and compare URIs according to
  416. [RFC 3986 Section 6](https://tools.ietf.org/html/rfc3986#section-6).
  417. ### `GuzzleHttp\Psr7\UriNormalizer::normalize`
  418. `public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS): UriInterface`
  419. Returns a normalized URI. The scheme and host component are already normalized to lowercase per PSR-7 UriInterface.
  420. This methods adds additional normalizations that can be configured with the `$flags` parameter which is a bitmask
  421. of normalizations to apply. The following normalizations are available:
  422. - `UriNormalizer::PRESERVING_NORMALIZATIONS`
  423. Default normalizations which only include the ones that preserve semantics.
  424. - `UriNormalizer::CAPITALIZE_PERCENT_ENCODING`
  425. All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized.
  426. Example: `http://example.org/a%c2%b1b` → `http://example.org/a%C2%B1b`
  427. - `UriNormalizer::DECODE_UNRESERVED_CHARACTERS`
  428. Decodes percent-encoded octets of unreserved characters. For consistency, percent-encoded octets in the ranges of
  429. ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should
  430. not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved
  431. characters by URI normalizers.
  432. Example: `http://example.org/%7Eusern%61me/` → `http://example.org/~username/`
  433. - `UriNormalizer::CONVERT_EMPTY_PATH`
  434. Converts the empty path to "/" for http and https URIs.
  435. Example: `http://example.org` → `http://example.org/`
  436. - `UriNormalizer::REMOVE_DEFAULT_HOST`
  437. Removes the default host of the given URI scheme from the URI. Only the "file" scheme defines the default host
  438. "localhost". All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` are equivalent according to
  439. RFC 3986.
  440. Example: `file://localhost/myfile` → `file:///myfile`
  441. - `UriNormalizer::REMOVE_DEFAULT_PORT`
  442. Removes the default port of the given URI scheme from the URI.
  443. Example: `http://example.org:80/` → `http://example.org/`
  444. - `UriNormalizer::REMOVE_DOT_SEGMENTS`
  445. Removes unnecessary dot-segments. Dot-segments in relative-path references are not removed as it would
  446. change the semantics of the URI reference.
  447. Example: `http://example.org/../a/b/../c/./d.html` → `http://example.org/a/c/d.html`
  448. - `UriNormalizer::REMOVE_DUPLICATE_SLASHES`
  449. Paths which include two or more adjacent slashes are converted to one. Webservers usually ignore duplicate slashes
  450. and treat those URIs equivalent. But in theory those URIs do not need to be equivalent. So this normalization
  451. may change the semantics. Encoded slashes (%2F) are not removed.
  452. Example: `http://example.org//foo///bar.html` → `http://example.org/foo/bar.html`
  453. - `UriNormalizer::SORT_QUERY_PARAMETERS`
  454. Sort query parameters with their values in alphabetical order. However, the order of parameters in a URI may be
  455. significant (this is not defined by the standard). So this normalization is not safe and may change the semantics
  456. of the URI.
  457. Example: `?lang=en&article=fred` → `?article=fred&lang=en`
  458. ### `GuzzleHttp\Psr7\UriNormalizer::isEquivalent`
  459. `public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS): bool`
  460. Whether two URIs can be considered equivalent. Both URIs are normalized automatically before comparison with the given
  461. `$normalizations` bitmask. The method also accepts relative URI references and returns true when they are equivalent.
  462. This of course assumes they will be resolved against the same base URI. If this is not the case, determination of
  463. equivalence or difference of relative references does not mean anything.