Make WordPress Core


Ignore:
Timestamp:
11/25/2021 01:10:30 AM (5 years ago)
Author:
SergeyBiryukov
Message:

External Libraries: Update the Requests library to version 2.0.0.

This is a major release and contains breaking changes.

Most important changes to be aware of for this release:

  • All code is now namespaced. Though there is a full backward compatibility layer available and the old class names are still supported, using them will generate a deprecation notice (which can be silenced by plugins if they'd need to support multiple WP versions). See the ​upgrade guide for more details.
  • A lot of classes have been marked final. This should generally not affect userland code as care has been taken to not apply the final keyword to classes which are known to be extended in userland code.
  • Extensive input validation has been added to Requests. When Requests is used as documented though, this will be unnoticable.
  • A new WpOrg\Requests\Requests::has_capabilities() method has been introduced which can be used to address #37708.
  • A new WpOrg\Requests\Response::decode_body() method has been introduced which may be usable to simplify some of the WP native wrapper code.
  • Remaining PHP 8.0 compatibility fixed (support for named parameters).
  • PHP 8.1 compatibility.

Release notes: ​https://github.com/WordPress/Requests/releases/tag/v2.0.0

For a full list of changes in this update, see the Requests GitHub:
​https://github.com/WordPress/Requests/compare/v1.8.1...v2.0.0

Follow-up to [50842], [51078].

Props jrf, schlessera, datagutten, wojsmol, dd32, dustinrue, soulseekah, costdev, szepeviktor.
Fixes #54504.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/Requests/IdnaEncoder.php

    r52243 r52244  
    11<?php
     2
     3namespace WpOrg\Requests;
     4
     5use WpOrg\Requests\Exception;
     6use WpOrg\Requests\Exception\InvalidArgument;
     7use WpOrg\Requests\Utility\InputValidator;
    28
    39/**
    … …  
    612 * Note: Not fully compliant, as nameprep does nothing yet.
    713 *
    8  * @package Requests
    9  * @subpackage Utilities
    10  * @see https://tools.ietf.org/html/rfc3490 IDNA specification
    11  * @see https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification
     14 * @package Requests\Utilities
     15 *
     16 * @link https://tools.ietf.org/html/rfc3490 IDNA specification
     17 * @link https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification
    1218 */
    13 class Requests_IDNAEncoder {
     19class IdnaEncoder {
    1420        /**
    1521         * ACE prefix used for IDNA
    1622         *
    17          * @see https://tools.ietf.org/html/rfc3490#section-5
     23         * @link https://tools.ietf.org/html/rfc3490#section-5
    1824         * @var string
    1925         */
    2026        const ACE_PREFIX = 'xn--';
     27
     28        /**
     29         * Maximum length of a IDNA URL in ASCII.
     30         *
     31         * @see \WpOrg\Requests\IdnaEncoder::to_ascii()
     32         *
     33         * @since 2.0.0
     34         *
     35         * @var int
     36         */
     37        const MAX_LENGTH = 64;
    2138
    2239        /**#@+
    2340         * Bootstrap constant for Punycode
    2441         *
    25          * @see https://tools.ietf.org/html/rfc3492#section-5
     42         * @link https://tools.ietf.org/html/rfc3492#section-5
    2643         * @var int
    2744         */
    … …  
    3855         * Encode a hostname using Punycode
    3956         *
    40          * @param string $string Hostname
     57         * @param string|Stringable $hostname Hostname
    4158         * @return string Punycode-encoded hostname
    42          */
    43         public static function encode($string) {
    44                 $parts = explode('.', $string);
     59         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or a stringable object.
     60         */
     61        public static function encode($hostname) {
     62                if (InputValidator::is_string_or_stringable($hostname) === false) {
     63                        throw InvalidArgument::create(1, '$hostname', 'string|Stringable', gettype($hostname));
     64                }
     65
     66                $parts = explode('.', $hostname);
    4567                foreach ($parts as &$part) {
    4668                        $part = self::to_ascii($part);
    … …  
    5072
    5173        /**
    52          * Convert a UTF-8 string to an ASCII string using Punycode
    53          *
    54          * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`)
    55          * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`)
    56          * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`)
    57          * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`)
    58          *
    59          * @param string $string ASCII or UTF-8 string (max length 64 characters)
     74         * Convert a UTF-8 text string to an ASCII string using Punycode
     75         *
     76         * @param string $text ASCII or UTF-8 string (max length 64 characters)
    6077         * @return string ASCII string
    61          */
    62         public static function to_ascii($string) {
    63                 // Step 1: Check if the string is already ASCII
    64                 if (self::is_ascii($string)) {
     78         *
     79         * @throws \WpOrg\Requests\Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`)
     80         * @throws \WpOrg\Requests\Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`)
     81         * @throws \WpOrg\Requests\Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`)
     82         * @throws \WpOrg\Requests\Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`)
     83         */
     84        public static function to_ascii($text) {
     85                // Step 1: Check if the text is already ASCII
     86                if (self::is_ascii($text)) {
    6587                        // Skip to step 7
    66                         if (strlen($string) < 64) {
    67                                 return $string;
    68                         }
    69 
    70                         throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string);
     88                        if (strlen($text) < self::MAX_LENGTH) {
     89                                return $text;
     90                        }
     91
     92                        throw new Exception('Provided string is too long', 'idna.provided_too_long', $text);
    7193                }
    7294
    7395                // Step 2: nameprep
    74                 $string = self::nameprep($string);
     96                $text = self::nameprep($text);
    7597
    7698                // Step 3: UseSTD3ASCIIRules is false, continue
    7799                // Step 4: Check if it's ASCII now
    78                 if (self::is_ascii($string)) {
     100                if (self::is_ascii($text)) {
    79101                        // Skip to step 7
    80                         if (strlen($string) < 64) {
    81                                 return $string;
    82                         }
    83 
    84                         throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string);
     102                        /*
     103                         * As the `nameprep()` method returns the original string, this code will never be reached until
     104                         * that method is properly implemented.
     105                         */
     106                        // @codeCoverageIgnoreStart
     107                        if (strlen($text) < self::MAX_LENGTH) {
     108                                return $text;
     109                        }
     110
     111                        throw new Exception('Prepared string is too long', 'idna.prepared_too_long', $text);
     112                        // @codeCoverageIgnoreEnd
    85113                }
    86114
    87115                // Step 5: Check ACE prefix
    88                 if (strpos($string, self::ACE_PREFIX) === 0) {
    89                         throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string);
     116                if (strpos($text, self::ACE_PREFIX) === 0) {
     117                        throw new Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $text);
    90118                }
    91119
    92120                // Step 6: Encode with Punycode
    93                 $string = self::punycode_encode($string);
     121                $text = self::punycode_encode($text);
    94122
    95123                // Step 7: Prepend ACE prefix
    96                 $string = self::ACE_PREFIX . $string;
     124                $text = self::ACE_PREFIX . $text;
    97125
    98126                // Step 8: Check size
    99                 if (strlen($string) < 64) {
    100                         return $string;
    101                 }
    102 
    103                 throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string);
    104         }
    105 
    106         /**
    107          * Check whether a given string contains only ASCII characters
     127                if (strlen($text) < self::MAX_LENGTH) {
     128                        return $text;
     129                }
     130
     131                throw new Exception('Encoded string is too long', 'idna.encoded_too_long', $text);
     132        }
     133
     134        /**
     135         * Check whether a given text string contains only ASCII characters
    108136         *
    109137         * @internal (Testing found regex was the fastest implementation)
    110138         *
    111          * @param string $string
    112          * @return bool Is the string ASCII-only?
    113          */
    114         protected static function is_ascii($string) {
    115                 return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1);
    116         }
    117 
    118         /**
    119          * Prepare a string for use as an IDNA name
     139         * @param string $text
     140         * @return bool Is the text string ASCII-only?
     141         */
     142        protected static function is_ascii($text) {
     143                return (preg_match('/(?:[^\x00-\x7F])/', $text) !== 1);
     144        }
     145
     146        /**
     147         * Prepare a text string for use as an IDNA name
    120148         *
    121149         * @todo Implement this based on RFC 3491 and the newer 5891
    122          * @param string $string
     150         * @param string $text
    123151         * @return string Prepared string
    124152         */
    125         protected static function nameprep($string) {
    126                 return $string;
     153        protected static function nameprep($text) {
     154                return $text;
    127155        }
    128156
    … …  
    130158         * Convert a UTF-8 string to a UCS-4 codepoint array
    131159         *
    132          * Based on Requests_IRI::replace_invalid_with_pct_encoding()
    133          *
    134          * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`)
     160         * Based on \WpOrg\Requests\Iri::replace_invalid_with_pct_encoding()
     161         *
    135162         * @param string $input
    136163         * @return array Unicode code points
     164         *
     165         * @throws \WpOrg\Requests\Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`)
    137166         */
    138167        protected static function utf8_to_codepoints($input) {
    139                 $codepoints = array();
     168                $codepoints = [];
    140169
    141170                // Get number of bytes
    … …  
    172201                        // Invalid byte:
    173202                        else {
    174                                 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);
     203                                throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);
    175204                        }
    176205
    177206                        if ($remaining > 0) {
    178207                                if ($position + $length > $strlen) {
    179                                         throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
     208                                        throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
    180209                                }
    181210                                for ($position++; $remaining > 0; $position++) {
    … …  
    184213                                        // If it is invalid, count the sequence as invalid and reprocess the current byte:
    185214                                        if (($value & 0xC0) !== 0x80) {
    186                                                 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
     215                                                throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
    187216                                        }
    188217
    … …  
    209238                                )
    210239                        ) {
    211                                 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
     240                                throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
    212241                        }
    213242
    … …  
    222251         *
    223252         * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code
    224          * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`)
    225253         *
    226254         * @param string $input UTF-8 encoded string to encode
    227255         * @return string Punycode-encoded string
     256         *
     257         * @throws \WpOrg\Requests\Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`)
    228258         */
    229259        public static function punycode_encode($input) {
    … …  
    240270                // copy them to the output in order
    241271                $codepoints = self::utf8_to_codepoints($input);
    242                 $extended   = array();
     272                $extended   = [];
    243273
    244274                foreach ($codepoints as $char) {
    … …  
    253283                        // @codeCoverageIgnoreStart
    254284                        elseif ($char < $n) {
    255                                 throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char);
     285                                throw new Exception('Invalid character', 'idna.character_outside_domain', $char);
    256286                        }
    257287                        // @codeCoverageIgnoreEnd
    … …  
    333363         * Convert a digit to its respective character
    334364         *
    335          * @see https://tools.ietf.org/html/rfc3492#section-5
    336          * @throws Requests_Exception On invalid digit (`idna.invalid_digit`)
     365         * @link https://tools.ietf.org/html/rfc3492#section-5
    337366         *
    338367         * @param int $digit Digit in the range 0-35
    339368         * @return string Single character corresponding to digit
     369         *
     370         * @throws \WpOrg\Requests\Exception On invalid digit (`idna.invalid_digit`)
    340371         */
    341372        protected static function digit_to_char($digit) {
    … …  
    343374                // As far as I know, this never happens, but still good to be sure.
    344375                if ($digit < 0 || $digit > 35) {
    345                         throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);
     376                        throw new Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);
    346377                }
    347378                // @codeCoverageIgnoreEnd
    … …  
    353384         * Adapt the bias
    354385         *
    355          * @see https://tools.ietf.org/html/rfc3492#section-6.1
     386         * @link https://tools.ietf.org/html/rfc3492#section-6.1
    356387         * @param int $delta
    357388         * @param int $numpoints
Note: See TracChangeset for help on using the changeset viewer.