forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UriResolver.php
362 lines (308 loc) · 10.7 KB
/
UriResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri;
use League\Uri\Contracts\UriInterface;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
use function array_pop;
use function array_reduce;
use function count;
use function end;
use function explode;
use function implode;
use function in_array;
use function str_repeat;
use function strpos;
use function substr;
final class UriResolver
{
/**
* @var array<string,int>
*/
const DOT_SEGMENTS = ['.' => 1, '..' => 1];
/**
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Input URI normalization to allow Stringable and string URI.
*/
private static function filterUri(Psr7UriInterface|UriInterface|Stringable|string $uri): Psr7UriInterface|UriInterface
{
return match (true) {
$uri instanceof Psr7UriInterface, $uri instanceof UriInterface => $uri,
default => Uri::createFromString($uri),
};
}
/**
* Resolves a URI against a base URI using RFC3986 rules.
*
* If the first argument is a UriInterface the method returns a UriInterface object
* If the first argument is a Psr7UriInterface the method returns a Psr7UriInterface object
*/
public static function resolve(
Psr7UriInterface|UriInterface|Stringable|string $uri,
Psr7UriInterface|UriInterface|Stringable|string $base_uri
): Psr7UriInterface|UriInterface {
$uri = self::filterUri($uri);
$base_uri = self::filterUri($base_uri);
$null = $uri instanceof Psr7UriInterface ? '' : null;
if ($null !== $uri->getScheme()) {
return $uri
->withPath(self::removeDotSegments($uri->getPath()));
}
if ($null !== $uri->getAuthority()) {
return $uri
->withScheme($base_uri->getScheme())
->withPath(self::removeDotSegments($uri->getPath()));
}
$user = $null;
$pass = null;
$userInfo = $base_uri->getUserInfo();
if (null !== $userInfo) {
[$user, $pass] = explode(':', $userInfo, 2) + [1 => null];
}
[$uri_path, $uri_query] = self::resolvePathAndQuery($uri, $base_uri);
return $uri
->withPath(self::removeDotSegments($uri_path))
->withQuery($uri_query)
->withHost($base_uri->getHost())
->withPort($base_uri->getPort())
->withUserInfo((string) $user, $pass)
->withScheme($base_uri->getScheme())
;
}
/**
* Remove dot segments from the URI path.
*/
private static function removeDotSegments(string $path): string
{
if (!str_contains($path, '.')) {
return $path;
}
$old_segments = explode('/', $path);
$new_path = implode('/', array_reduce($old_segments, UriResolver::reducer(...), []));
if (isset(self::DOT_SEGMENTS[end($old_segments)])) {
$new_path .= '/';
}
// @codeCoverageIgnoreStart
// added because some PSR-7 implementations do not respect RFC3986
if (str_starts_with($path, '/') && !str_starts_with($new_path, '/')) {
return '/'.$new_path;
}
// @codeCoverageIgnoreEnd
return $new_path;
}
/**
* Remove dot segments.
*
* @return array<int, string>
*/
private static function reducer(array $carry, string $segment): array
{
if ('..' === $segment) {
array_pop($carry);
return $carry;
}
if (!isset(self::DOT_SEGMENTS[$segment])) {
$carry[] = $segment;
}
return $carry;
}
/**
* Resolves an URI path and query component.
*
* @return array{0:string, 1:string|null}
*/
private static function resolvePathAndQuery(
Psr7UriInterface|UriInterface $uri,
Psr7UriInterface|UriInterface $base_uri
): array {
$target_path = $uri->getPath();
$target_query = $uri->getQuery();
$null = $uri instanceof Psr7UriInterface ? '' : null;
$baseNull = $base_uri instanceof Psr7UriInterface ? '' : null;
if (str_starts_with($target_path, '/')) {
return [$target_path, $target_query];
}
if ('' === $target_path) {
if ($null === $target_query) {
$target_query = $base_uri->getQuery();
}
$target_path = $base_uri->getPath();
//@codeCoverageIgnoreStart
//because some PSR-7 Uri implementations allow this RFC3986 forbidden construction
if ($baseNull !== $base_uri->getAuthority() && !str_starts_with($target_path, '/')) {
$target_path = '/'.$target_path;
}
//@codeCoverageIgnoreEnd
return [$target_path, $target_query];
}
$base_path = $base_uri->getPath();
if ($baseNull !== $base_uri->getAuthority() && '' === $base_path) {
$target_path = '/'.$target_path;
}
if ('' !== $base_path) {
$segments = explode('/', $base_path);
array_pop($segments);
if ([] !== $segments) {
$target_path = implode('/', $segments).'/'.$target_path;
}
}
return [$target_path, $target_query];
}
/**
* Relativizes an URI according to a base URI.
*
* This method MUST retain the state of the submitted URI instance, and return
* an URI instance of the same type that contains the applied modifications.
*
* This method MUST be transparent when dealing with error and exceptions.
* It MUST not alter of silence them apart from validating its own parameters.
*/
public static function relativize(
Psr7UriInterface|UriInterface|Stringable|string $uri,
Psr7UriInterface|UriInterface|Stringable|string $base_uri
): Psr7UriInterface|UriInterface {
$uri = self::filterUri($uri);
$base_uri = self::filterUri($base_uri);
$uri = self::formatHost($uri);
$base_uri = self::formatHost($base_uri);
if (!self::isRelativizable($uri, $base_uri)) {
return $uri;
}
$null = $uri instanceof Psr7UriInterface ? '' : null;
$uri = $uri->withScheme($null)->withPort(null)->withUserInfo($null)->withHost($null);
$target_path = $uri->getPath();
if ($target_path !== $base_uri->getPath()) {
return $uri->withPath(self::relativizePath($target_path, $base_uri->getPath()));
}
if (self::componentEquals('query', $uri, $base_uri)) {
return $uri->withPath('')->withQuery($null);
}
if ($null === $uri->getQuery()) {
return $uri->withPath(self::formatPathWithEmptyBaseQuery($target_path));
}
return $uri->withPath('');
}
/**
* Tells whether the component value from both URI object equals.
*/
private static function componentEquals(
string $property,
Psr7UriInterface|UriInterface $uri,
Psr7UriInterface|UriInterface $base_uri
): bool {
return self::getComponent($property, $uri) === self::getComponent($property, $base_uri);
}
/**
* Returns the component value from the submitted URI object.
*/
private static function getComponent(string $property, Psr7UriInterface|UriInterface $uri): ?string
{
$component = match ($property) {
'query' => $uri->getQuery(),
'authority' => $uri->getAuthority(),
default => $uri->getScheme(), //scheme
};
if ($uri instanceof Psr7UriInterface && '' === $component) {
return null;
}
return $component;
}
/**
* Filter the URI object.
*/
private static function formatHost(Psr7UriInterface|UriInterface $uri): Psr7UriInterface|UriInterface
{
if (!$uri instanceof Psr7UriInterface) {
return $uri;
}
$host = $uri->getHost();
if ('' === $host) {
return $uri;
}
return $uri->withHost((string) Uri::createFromComponents(['host' => $host])->getHost());
}
/**
* Tells whether the submitted URI object can be relativize.
*/
private static function isRelativizable(
Psr7UriInterface|UriInterface $uri,
Psr7UriInterface|UriInterface $base_uri
): bool {
return !UriInfo::isRelativePath($uri)
&& self::componentEquals('scheme', $uri, $base_uri)
&& self::componentEquals('authority', $uri, $base_uri);
}
/**
* Relatives the URI for an authority-less target URI.
*/
private static function relativizePath(string $path, string $basepath): string
{
$base_segments = self::getSegments($basepath);
$target_segments = self::getSegments($path);
$target_basename = array_pop($target_segments);
array_pop($base_segments);
foreach ($base_segments as $offset => $segment) {
if (!isset($target_segments[$offset]) || $segment !== $target_segments[$offset]) {
break;
}
unset($base_segments[$offset], $target_segments[$offset]);
}
$target_segments[] = $target_basename;
return self::formatPath(
str_repeat('../', count($base_segments)).implode('/', $target_segments),
$basepath
);
}
/**
* returns the path segments.
*
* @return string[]
*/
private static function getSegments(string $path): array
{
if ('' !== $path && '/' === $path[0]) {
$path = substr($path, 1);
}
return explode('/', $path);
}
/**
* Formatting the path to keep a valid URI.
*/
private static function formatPath(string $path, string $basepath): string
{
if ('' === $path) {
return in_array($basepath, ['', '/'], true) ? $basepath : './';
}
if (false === ($colon_pos = strpos($path, ':'))) {
return $path;
}
$slash_pos = strpos($path, '/');
if (false === $slash_pos || $colon_pos < $slash_pos) {
return "./$path";
}
return $path;
}
/**
* Formatting the path to keep a resolvable URI.
*/
private static function formatPathWithEmptyBaseQuery(string $path): string
{
$target_segments = self::getSegments($path);
/** @var string $basename */
$basename = end($target_segments);
return '' === $basename ? './' : $basename;
}
}