Skip to content

Commit 6b08735

Browse files
committed
Remove query sorting during normalization
1 parent 360d638 commit 6b08735

File tree

1 file changed

+2
-39
lines changed

1 file changed

+2
-39
lines changed

UriString.php

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use League\Uri\IPv6\Converter as IPv6Converter;
2121
use Stringable;
2222

23-
use function array_map;
2423
use function array_merge;
2524
use function array_pop;
2625
use function array_reduce;
@@ -30,19 +29,15 @@
3029
use function in_array;
3130
use function inet_pton;
3231
use function preg_match;
33-
use function preg_split;
3432
use function rawurldecode;
3533
use function sprintf;
36-
use function strcmp;
3734
use function strpos;
3835
use function strtolower;
3936
use function substr;
40-
use function uksort;
4137

4238
use const FILTER_FLAG_IPV4;
4339
use const FILTER_FLAG_IPV6;
4440
use const FILTER_VALIDATE_IP;
45-
use const PREG_SPLIT_NO_EMPTY;
4641

4742
/**
4843
* A class to parse a URI string according to RFC3986.
@@ -292,7 +287,7 @@ public static function normalize(Stringable|string $uri): string
292287
false === filter_var($components['host'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) &&
293288
!IPv6Converter::isIpv6($components['host'])
294289
) {
295-
$components['host'] = IdnaConverter::toUnicode($components['host'])->domain();
290+
$components['host'] = IdnaConverter::toAscii($components['host'])->domain();
296291
}
297292

298293
$path = $components['path'];
@@ -306,7 +301,7 @@ public static function normalize(Stringable|string $uri): string
306301
}
307302

308303
$components['path'] = $path;
309-
$components['query'] = Encoder::decodeQuery(self::sortQuery($components['query']));
304+
$components['query'] = Encoder::decodeQuery($components['query']);
310305
$components['fragment'] = Encoder::decodeFragment($components['fragment']);
311306
$components['user'] = Encoder::decodeUnreservedCharacters($components['user']);
312307
$components['pass'] = Encoder::decodeUnreservedCharacters($components['pass']);
@@ -708,36 +703,4 @@ private static function isIpHost(string $ipHost): bool
708703
return false !== filter_var($ipHost, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
709704
&& str_starts_with((string)inet_pton($ipHost), self::ZONE_ID_ADDRESS_BLOCK);
710705
}
711-
712-
private static function sortQuery(?string $query): ?string
713-
{
714-
if (null === $query || !str_contains($query, '&')) {
715-
return $query;
716-
}
717-
718-
$codepoints = fn (?string $str): string => in_array($str, ['', null], true) ? '' : implode('.', array_map(
719-
mb_ord(...), /* @phpstan-ignore-line */
720-
(array) preg_split(pattern:'//u', subject: $str, flags: PREG_SPLIT_NO_EMPTY)
721-
));
722-
$compare = fn (string $name1, string $name2): int => match (1) {
723-
preg_match('/[^\x20-\x7f]/', $name1.$name2) => strcmp($codepoints($name1), $codepoints($name2)),
724-
default => strcmp($name1, $name2),
725-
};
726-
727-
$parameters = array_reduce(QueryString::parse($query), function (array $carry, array $pair) {
728-
$carry[$pair[0]] ??= [];
729-
$carry[$pair[0]][] = $pair[1];
730-
731-
return $carry;
732-
}, []);
733-
734-
uksort($parameters, $compare);
735-
736-
$pairs = [];
737-
foreach ($parameters as $key => $values) {
738-
$pairs = [...$pairs, ...array_map(fn ($value) => [$key, $value], $values)];
739-
}
740-
741-
return QueryString::buildFromPairs($pairs);
742-
}
743706
}

0 commit comments

Comments
 (0)