Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 9... Línea 9...
9
/**
9
/**
10
 * Provides methods to normalize and compare URIs.
10
 * Provides methods to normalize and compare URIs.
11
 *
11
 *
12
 * @author Tobias Schultze
12
 * @author Tobias Schultze
13
 *
13
 *
14
 * @link https://tools.ietf.org/html/rfc3986#section-6
14
 * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6
15
 */
15
 */
16
final class UriNormalizer
16
final class UriNormalizer
17
{
17
{
18
    /**
18
    /**
19
     * Default normalizations which only include the ones that preserve semantics.
19
     * Default normalizations which only include the ones that preserve semantics.
Línea 117... Línea 117...
117
     * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.
117
     * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.
118
     *
118
     *
119
     * @param UriInterface $uri   The URI to normalize
119
     * @param UriInterface $uri   The URI to normalize
120
     * @param int          $flags A bitmask of normalizations to apply, see constants
120
     * @param int          $flags A bitmask of normalizations to apply, see constants
121
     *
121
     *
122
     * @link https://tools.ietf.org/html/rfc3986#section-6.2
122
     * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2
123
     */
123
     */
124
    public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface
124
    public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface
125
    {
125
    {
126
        if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
126
        if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
127
            $uri = self::capitalizePercentEncoding($uri);
127
            $uri = self::capitalizePercentEncoding($uri);
Línea 129... Línea 129...
129
 
129
 
130
        if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
130
        if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
131
            $uri = self::decodeUnreservedCharacters($uri);
131
            $uri = self::decodeUnreservedCharacters($uri);
Línea 132... Línea 132...
132
        }
132
        }
133
 
133
 
134
        if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' &&
134
        if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === ''
135
            ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')
135
            && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')
136
        ) {
136
        ) {
Línea 137... Línea 137...
137
            $uri = $uri->withPath('/');
137
            $uri = $uri->withPath('/');
Línea 172... Línea 172...
172
     *
172
     *
173
     * @param UriInterface $uri1           An URI to compare
173
     * @param UriInterface $uri1           An URI to compare
174
     * @param UriInterface $uri2           An URI to compare
174
     * @param UriInterface $uri2           An URI to compare
175
     * @param int          $normalizations A bitmask of normalizations to apply, see constants
175
     * @param int          $normalizations A bitmask of normalizations to apply, see constants
176
     *
176
     *
177
     * @link https://tools.ietf.org/html/rfc3986#section-6.1
177
     * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1
178
     */
178
     */
179
    public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool
179
    public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool
180
    {
180
    {
181
        return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations);
181
        return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations);
182
    }
182
    }
Línea 183... Línea 183...
183
 
183
 
184
    private static function capitalizePercentEncoding(UriInterface $uri): UriInterface
184
    private static function capitalizePercentEncoding(UriInterface $uri): UriInterface
185
    {
185
    {
Línea 186... Línea 186...
186
        $regex = '/(?:%[A-Fa-f0-9]{2})++/';
186
        $regex = '/(?:%[A-Fa-f0-9]{2})++/';
187
 
187
 
188
        $callback = function (array $match) {
188
        $callback = function (array $match): string {
Línea 189... Línea 189...
189
            return strtoupper($match[0]);
189
            return strtoupper($match[0]);
190
        };
190
        };
Línea 199... Línea 199...
199
 
199
 
200
    private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface
200
    private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface
201
    {
201
    {
Línea 202... Línea 202...
202
        $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i';
202
        $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i';
203
 
203
 
204
        $callback = function (array $match) {
204
        $callback = function (array $match): string {
Línea 205... Línea 205...
205
            return rawurldecode($match[0]);
205
            return rawurldecode($match[0]);
206
        };
206
        };