京东云
This commit is contained in:
55
vendor/guzzlehttp/psr7/src/UriNormalizer.php
vendored
55
vendor/guzzlehttp/psr7/src/UriNormalizer.php
vendored
@@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GuzzleHttp\Psr7;
|
||||
|
||||
use Psr\Http\Message\UriInterface;
|
||||
@@ -11,27 +9,24 @@ use Psr\Http\Message\UriInterface;
|
||||
*
|
||||
* @author Tobias Schultze
|
||||
*
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-6
|
||||
* @link https://tools.ietf.org/html/rfc3986#section-6
|
||||
*/
|
||||
final class UriNormalizer
|
||||
{
|
||||
/**
|
||||
* Default normalizations which only include the ones that preserve semantics.
|
||||
*
|
||||
* self::CAPITALIZE_PERCENT_ENCODING | self::DECODE_UNRESERVED_CHARACTERS | self::CONVERT_EMPTY_PATH |
|
||||
* self::REMOVE_DEFAULT_HOST | self::REMOVE_DEFAULT_PORT | self::REMOVE_DOT_SEGMENTS
|
||||
*/
|
||||
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;
|
||||
const PRESERVING_NORMALIZATIONS = 63;
|
||||
|
||||
/**
|
||||
* All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized.
|
||||
*
|
||||
* Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b
|
||||
*/
|
||||
public const CAPITALIZE_PERCENT_ENCODING = 1;
|
||||
const CAPITALIZE_PERCENT_ENCODING = 1;
|
||||
|
||||
/**
|
||||
* Decodes percent-encoded octets of unreserved characters.
|
||||
@@ -42,14 +37,14 @@ final class UriNormalizer
|
||||
*
|
||||
* Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/
|
||||
*/
|
||||
public const DECODE_UNRESERVED_CHARACTERS = 2;
|
||||
const DECODE_UNRESERVED_CHARACTERS = 2;
|
||||
|
||||
/**
|
||||
* Converts the empty path to "/" for http and https URIs.
|
||||
*
|
||||
* Example: http://example.org → http://example.org/
|
||||
*/
|
||||
public const CONVERT_EMPTY_PATH = 4;
|
||||
const CONVERT_EMPTY_PATH = 4;
|
||||
|
||||
/**
|
||||
* Removes the default host of the given URI scheme from the URI.
|
||||
@@ -62,14 +57,14 @@ final class UriNormalizer
|
||||
*
|
||||
* Example: file://localhost/myfile → file:///myfile
|
||||
*/
|
||||
public const REMOVE_DEFAULT_HOST = 8;
|
||||
const REMOVE_DEFAULT_HOST = 8;
|
||||
|
||||
/**
|
||||
* Removes the default port of the given URI scheme from the URI.
|
||||
*
|
||||
* Example: http://example.org:80/ → http://example.org/
|
||||
*/
|
||||
public const REMOVE_DEFAULT_PORT = 16;
|
||||
const REMOVE_DEFAULT_PORT = 16;
|
||||
|
||||
/**
|
||||
* Removes unnecessary dot-segments.
|
||||
@@ -79,7 +74,7 @@ final class UriNormalizer
|
||||
*
|
||||
* Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html
|
||||
*/
|
||||
public const REMOVE_DOT_SEGMENTS = 32;
|
||||
const REMOVE_DOT_SEGMENTS = 32;
|
||||
|
||||
/**
|
||||
* Paths which include two or more adjacent slashes are converted to one.
|
||||
@@ -90,7 +85,7 @@ final class UriNormalizer
|
||||
*
|
||||
* Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html
|
||||
*/
|
||||
public const REMOVE_DUPLICATE_SLASHES = 64;
|
||||
const REMOVE_DUPLICATE_SLASHES = 64;
|
||||
|
||||
/**
|
||||
* Sort query parameters with their values in alphabetical order.
|
||||
@@ -103,7 +98,7 @@ final class UriNormalizer
|
||||
* Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the
|
||||
* purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly.
|
||||
*/
|
||||
public const SORT_QUERY_PARAMETERS = 128;
|
||||
const SORT_QUERY_PARAMETERS = 128;
|
||||
|
||||
/**
|
||||
* Returns a normalized URI.
|
||||
@@ -119,9 +114,11 @@ final class UriNormalizer
|
||||
* @param UriInterface $uri The URI to normalize
|
||||
* @param int $flags A bitmask of normalizations to apply, see constants
|
||||
*
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2
|
||||
* @return UriInterface The normalized URI
|
||||
*
|
||||
* @link https://tools.ietf.org/html/rfc3986#section-6.2
|
||||
*/
|
||||
public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface
|
||||
public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS)
|
||||
{
|
||||
if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
|
||||
$uri = self::capitalizePercentEncoding($uri);
|
||||
@@ -131,8 +128,8 @@ final class UriNormalizer
|
||||
$uri = self::decodeUnreservedCharacters($uri);
|
||||
}
|
||||
|
||||
if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === ''
|
||||
&& ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')
|
||||
if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' &&
|
||||
($uri->getScheme() === 'http' || $uri->getScheme() === 'https')
|
||||
) {
|
||||
$uri = $uri->withPath('/');
|
||||
}
|
||||
@@ -174,18 +171,20 @@ final class UriNormalizer
|
||||
* @param UriInterface $uri2 An URI to compare
|
||||
* @param int $normalizations A bitmask of normalizations to apply, see constants
|
||||
*
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1
|
||||
* @return bool
|
||||
*
|
||||
* @link https://tools.ietf.org/html/rfc3986#section-6.1
|
||||
*/
|
||||
public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool
|
||||
public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS)
|
||||
{
|
||||
return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations);
|
||||
}
|
||||
|
||||
private static function capitalizePercentEncoding(UriInterface $uri): UriInterface
|
||||
private static function capitalizePercentEncoding(UriInterface $uri)
|
||||
{
|
||||
$regex = '/(?:%[A-Fa-f0-9]{2})++/';
|
||||
|
||||
$callback = function (array $match): string {
|
||||
$callback = function (array $match) {
|
||||
return strtoupper($match[0]);
|
||||
};
|
||||
|
||||
@@ -197,11 +196,11 @@ final class UriNormalizer
|
||||
);
|
||||
}
|
||||
|
||||
private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface
|
||||
private static function decodeUnreservedCharacters(UriInterface $uri)
|
||||
{
|
||||
$regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i';
|
||||
|
||||
$callback = function (array $match): string {
|
||||
$callback = function (array $match) {
|
||||
return rawurldecode($match[0]);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user