PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
← All changes | vendor_prefixed/guzzlehttp/psr7/src/UriNormalizer.php +0 -190 28.2 → trunk View file →
@@ -1,190 +1,0 @@
1 -<?php
2 -
3 -declare (strict_types=1);
4 -namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
5 -
6 -use YoastSEO_Vendor\Psr\Http\Message\UriInterface;
7 -/**
8 - * Provides methods to normalize and compare URIs.
9 - *
10 - * @author Tobias Schultze
11 - *
12 - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6
13 - */
14 -final class UriNormalizer
15 -{
16 - /**
17 - * Default normalizations which only include the ones that preserve semantics.
18 - */
19 - public const PRESERVING_NORMALIZATIONS = self::CAPITALIZE_PERCENT_ENCODING | self::DECODE_UNRESERVED_CHARACTERS | self::CONVERT_EMPTY_PATH | self::REMOVE_DEFAULT_HOST | self::REMOVE_DEFAULT_PORT | self::REMOVE_DOT_SEGMENTS;
20 - /**
21 - * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized.
22 - *
23 - * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b
24 - */
25 - public const CAPITALIZE_PERCENT_ENCODING = 1;
26 - /**
27 - * Decodes percent-encoded octets of unreserved characters.
28 - *
29 - * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39),
30 - * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and,
31 - * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers.
32 - *
33 - * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/
34 - */
35 - public const DECODE_UNRESERVED_CHARACTERS = 2;
36 - /**
37 - * Converts the empty path to "/" for http and https URIs.
38 - *
39 - * Example: http://example.org → http://example.org/
40 - */
41 - public const CONVERT_EMPTY_PATH = 4;
42 - /**
43 - * Removes the default host of the given URI scheme from the URI.
44 - *
45 - * Only the "file" scheme defines the default host "localhost".
46 - * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile`
47 - * are equivalent according to RFC 3986. The first format is not accepted
48 - * by PHPs stream functions and thus already normalized implicitly to the
49 - * second format in the Uri class. See `GuzzleHttp\Psr7\Uri::composeComponents`.
50 - *
51 - * Example: file://localhost/myfile → file:///myfile
52 - */
53 - public const REMOVE_DEFAULT_HOST = 8;
54 - /**
55 - * Removes the default port of the given URI scheme from the URI.
56 - *
57 - * Example: http://example.org:80/ → http://example.org/
58 - */
59 - public const REMOVE_DEFAULT_PORT = 16;
60 - /**
61 - * Removes unnecessary dot-segments.
62 - *
63 - * Dot-segments in relative-path references are not removed as it would
64 - * change the semantics of the URI reference.
65 - *
66 - * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html
67 - */
68 - public const REMOVE_DOT_SEGMENTS = 32;
69 - /**
70 - * Paths which include two or more adjacent slashes are converted to one.
71 - *
72 - * Webservers usually ignore duplicate slashes and treat those URIs equivalent.
73 - * But in theory those URIs do not need to be equivalent. So this normalization
74 - * may change the semantics. Encoded slashes (%2F) are not removed.
75 - *
76 - * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html
77 - */
78 - public const REMOVE_DUPLICATE_SLASHES = 64;
79 - /**
80 - * Sort query parameters with their values in alphabetical order.
81 - *
82 - * However, the order of parameters in a URI may be significant (this is not defined by the standard).
83 - * So this normalization is not safe and may change the semantics of the URI.
84 - *
85 - * Example: ?lang=en&article=fred → ?article=fred&lang=en
86 - *
87 - * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the
88 - * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly.
89 - */
90 - public const SORT_QUERY_PARAMETERS = 128;
91 - /**
92 - * Returns a normalized URI.
93 - *
94 - * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface.
95 - * This methods adds additional normalizations that can be configured with the $flags parameter.
96 - *
97 - * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as
98 - * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are
99 - * treated equivalent which is not necessarily true according to RFC 3986. But that difference
100 - * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.
101 - *
102 - * @param UriInterface $uri The URI to normalize
103 - * @param int $flags A bitmask of normalizations to apply, see constants
104 - *
105 - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2
106 - */
107 - public static function normalize(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS) : \YoastSEO_Vendor\Psr\Http\Message\UriInterface
108 - {
109 - if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
110 - $uri = self::capitalizePercentEncoding($uri);
111 - }
112 - if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
113 - $uri = self::decodeUnreservedCharacters($uri);
114 - }
115 - if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')) {
116 - $uri = $uri->withPath('/');
117 - }
118 - if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') {
119 - $uri = $uri->withHost('');
120 - }
121 - if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && \YoastSEO_Vendor\GuzzleHttp\Psr7\Uri::isDefaultPort($uri)) {
122 - $uri = $uri->withPort(null);
123 - }
124 - if ($flags & self::REMOVE_DOT_SEGMENTS && !\YoastSEO_Vendor\GuzzleHttp\Psr7\Uri::isRelativePathReference($uri)) {
125 - $uri = $uri->withPath(\YoastSEO_Vendor\GuzzleHttp\Psr7\UriResolver::removeDotSegments($uri->getPath()));
126 - }
127 - if ($flags & self::REMOVE_DUPLICATE_SLASHES) {
128 - $path = \preg_replace('#//++#', '/', $uri->getPath());
129 - if ($path === null) {
130 - throw new \RuntimeException('Unable to remove duplicate slashes from URI path: ' . \preg_last_error_msg());
131 - }
132 - $uri = $uri->withPath($path);
133 - }
134 - if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') {
135 - $queryKeyValues = \explode('&', $uri->getQuery());
136 - \sort($queryKeyValues);
137 - $uri = $uri->withQuery(\implode('&', $queryKeyValues));
138 - }
139 - return $uri;
140 - }
141 - /**
142 - * Whether two URIs can be considered equivalent.
143 - *
144 - * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also
145 - * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be
146 - * resolved against the same base URI. If this is not the case, determination of equivalence or difference of
147 - * relative references does not mean anything.
148 - *
149 - * @param UriInterface $uri1 An URI to compare
150 - * @param UriInterface $uri2 An URI to compare
151 - * @param int $normalizations A bitmask of normalizations to apply, see constants
152 - *
153 - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1
154 - */
155 - public static function isEquivalent(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri1, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS) : bool
156 - {
157 - return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations);
158 - }
159 - private static function capitalizePercentEncoding(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri) : \YoastSEO_Vendor\Psr\Http\Message\UriInterface
160 - {
161 - $regex = '/(?:%[A-Fa-f0-9]{2})++/';
162 - $callback = function (array $match) : string {
163 - return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToUpper($match[0]);
164 - };
165 - return $uri->withPath(self::normalizePercentEncodingInComponent($uri->getPath(), $regex, $callback))->withQuery(self::normalizePercentEncodingInComponent($uri->getQuery(), $regex, $callback))->withFragment(self::normalizePercentEncodingInComponent($uri->getFragment(), $regex, $callback));
166 - }
167 - private static function decodeUnreservedCharacters(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri) : \YoastSEO_Vendor\Psr\Http\Message\UriInterface
168 - {
169 - $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i';
170 - $callback = function (array $match) : string {
171 - return \rawurldecode($match[0]);
172 - };
173 - return $uri->withPath(self::normalizePercentEncodingInComponent($uri->getPath(), $regex, $callback))->withQuery(self::normalizePercentEncodingInComponent($uri->getQuery(), $regex, $callback))->withFragment(self::normalizePercentEncodingInComponent($uri->getFragment(), $regex, $callback));
174 - }
175 - /**
176 - * @param callable(array): string $callback
177 - */
178 - private static function normalizePercentEncodingInComponent(string $component, string $regex, callable $callback) : string
179 - {
180 - $normalized = \preg_replace_callback($regex, $callback, $component);
181 - if ($normalized === null) {
182 - throw new \RuntimeException('Unable to normalize URI component percent-encoding: ' . \preg_last_error_msg());
183 - }
184 - return $normalized;
185 - }
186 - private function __construct()
187 - {
188 - // cannot be instantiated
189 - }
190 -}