Make WordPress Core


Ignore:
Timestamp:
11/25/2021 01:10:30 AM (3 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 edited

Legend:

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

    r50842 r52244  
    33 * Cookie storage object
    44 *
    5  * @package Requests
    6  * @subpackage Cookies
     5 * @package Requests\Cookies
    76 */
     7
     8namespace WpOrg\Requests;
     9
     10use WpOrg\Requests\Exception\InvalidArgument;
     11use WpOrg\Requests\Iri;
     12use WpOrg\Requests\Response\Headers;
     13use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
     14use WpOrg\Requests\Utility\InputValidator;
    815
    916/**
    1017 * Cookie storage object
    1118 *
    12  * @package Requests
    13  * @subpackage Cookies
     19 * @package Requests\Cookies
    1420 */
    15 class Requests_Cookie {
     21class Cookie {
    1622    /**
    1723     * Cookie name.
     
    3440     * httponly.
    3541     *
    36      * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object
    37      */
    38     public $attributes = array();
     42     * @var \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array Array-like object
     43     */
     44    public $attributes = [];
    3945
    4046    /**
     
    4652     * @var array
    4753     */
    48     public $flags = array();
     54    public $flags = [];
    4955
    5056    /**
     
    6369     * @param string $name
    6470     * @param string $value
    65      * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data
    66      */
    67     public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) {
     71     * @param array|\WpOrg\Requests\Utility\CaseInsensitiveDictionary $attributes Associative array of attribute data
     72     * @param array $flags
     73     * @param int|null $reference_time
     74     *
     75     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string.
     76     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $value argument is not a string.
     77     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $attributes argument is not an array or iterable object with array access.
     78     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $flags argument is not an array.
     79     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $reference_time argument is not an integer or null.
     80     */
     81    public function __construct($name, $value, $attributes = [], $flags = [], $reference_time = null) {
     82        if (is_string($name) === false) {
     83            throw InvalidArgument::create(1, '$name', 'string', gettype($name));
     84        }
     85
     86        if (is_string($value) === false) {
     87            throw InvalidArgument::create(2, '$value', 'string', gettype($value));
     88        }
     89
     90        if (InputValidator::has_array_access($attributes) === false || InputValidator::is_iterable($attributes) === false) {
     91            throw InvalidArgument::create(3, '$attributes', 'array|ArrayAccess&Traversable', gettype($attributes));
     92        }
     93
     94        if (is_array($flags) === false) {
     95            throw InvalidArgument::create(4, '$flags', 'array', gettype($flags));
     96        }
     97
     98        if ($reference_time !== null && is_int($reference_time) === false) {
     99            throw InvalidArgument::create(5, '$reference_time', 'integer|null', gettype($reference_time));
     100        }
     101
    68102        $this->name       = $name;
    69103        $this->value      = $value;
    70104        $this->attributes = $attributes;
    71         $default_flags    = array(
     105        $default_flags    = [
    72106            'creation'    => time(),
    73107            'last-access' => time(),
    74108            'persistent'  => false,
    75109            'host-only'   => true,
    76         );
     110        ];
    77111        $this->flags      = array_merge($default_flags, $flags);
    78112
     
    83117
    84118        $this->normalize();
     119    }
     120
     121    /**
     122     * Get the cookie value
     123     *
     124     * Attributes and other data can be accessed via methods.
     125     */
     126    public function __toString() {
     127        return $this->value;
    85128    }
    86129
     
    114157     * Check if a cookie is valid for a given URI
    115158     *
    116      * @param Requests_IRI $uri URI to check
     159     * @param \WpOrg\Requests\Iri $uri URI to check
    117160     * @return boolean Whether the cookie is valid for the given URI
    118161     */
    119     public function uri_matches(Requests_IRI $uri) {
     162    public function uri_matches(Iri $uri) {
    120163        if (!$this->domain_matches($uri->host)) {
    121164            return false;
     
    132175     * Check if a cookie is valid for a given domain
    133176     *
    134      * @param string $string Domain to check
     177     * @param string $domain Domain to check
    135178     * @return boolean Whether the cookie is valid for the given domain
    136179     */
    137     public function domain_matches($string) {
     180    public function domain_matches($domain) {
     181        if (is_string($domain) === false) {
     182            return false;
     183        }
     184
    138185        if (!isset($this->attributes['domain'])) {
    139186            // Cookies created manually; cookies created by Requests will set
     
    142189        }
    143190
    144         $domain_string = $this->attributes['domain'];
    145         if ($domain_string === $string) {
    146             // The domain string and the string are identical.
     191        $cookie_domain = $this->attributes['domain'];
     192        if ($cookie_domain === $domain) {
     193            // The cookie domain and the passed domain are identical.
    147194            return true;
    148195        }
     
    154201        }
    155202
    156         if (strlen($string) <= strlen($domain_string)) {
    157             // For obvious reasons, the string cannot be a suffix if the domain
    158             // is shorter than the domain string
    159             return false;
    160         }
    161 
    162         if (substr($string, -1 * strlen($domain_string)) !== $domain_string) {
    163             // The domain string should be a suffix of the string.
    164             return false;
    165         }
    166 
    167         $prefix = substr($string, 0, strlen($string) - strlen($domain_string));
     203        if (strlen($domain) <= strlen($cookie_domain)) {
     204            // For obvious reasons, the cookie domain cannot be a suffix if the passed domain
     205            // is shorter than the cookie domain
     206            return false;
     207        }
     208
     209        if (substr($domain, -1 * strlen($cookie_domain)) !== $cookie_domain) {
     210            // The cookie domain should be a suffix of the passed domain.
     211            return false;
     212        }
     213
     214        $prefix = substr($domain, 0, strlen($domain) - strlen($cookie_domain));
    168215        if (substr($prefix, -1) !== '.') {
    169             // The last character of the string that is not included in the
     216            // The last character of the passed domain that is not included in the
    170217            // domain string should be a %x2E (".") character.
    171218            return false;
    172219        }
    173220
    174         // The string should be a host name (i.e., not an IP address).
    175         return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $string);
     221        // The passed domain should be a host name (i.e., not an IP address).
     222        return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $domain);
    176223    }
    177224
     
    194241            // the path to the requested path
    195242            return true;
     243        }
     244
     245        if (is_scalar($request_path) === false) {
     246            return false;
    196247        }
    197248
     
    318369
    319370    /**
    320      * Format a cookie for a Cookie header
    321      *
    322      * @codeCoverageIgnore
    323      * @deprecated Use {@see Requests_Cookie::format_for_header}
    324      * @return string
    325      */
    326     public function formatForHeader() {
    327         return $this->format_for_header();
    328     }
    329 
    330     /**
    331371     * Format a cookie for a Set-Cookie header
    332372     *
     
    339379        $header_value = $this->format_for_header();
    340380        if (!empty($this->attributes)) {
    341             $parts = array();
     381            $parts = [];
    342382            foreach ($this->attributes as $key => $value) {
    343383                // Ignore non-associative attributes
     
    356396
    357397    /**
    358      * Format a cookie for a Set-Cookie header
    359      *
    360      * @codeCoverageIgnore
    361      * @deprecated Use {@see Requests_Cookie::format_for_set_cookie}
    362      * @return string
    363      */
    364     public function formatForSetCookie() {
    365         return $this->format_for_set_cookie();
    366     }
    367 
    368     /**
    369      * Get the cookie value
    370      *
    371      * Attributes and other data can be accessed via methods.
    372      */
    373     public function __toString() {
    374         return $this->value;
    375     }
    376 
    377     /**
    378398     * Parse a cookie string into a cookie object
    379399     *
     
    382402     * specifies some of this handling, but not in a thorough manner.
    383403     *
    384      * @param string Cookie header value (from a Set-Cookie header)
    385      * @return Requests_Cookie Parsed cookie object
    386      */
    387     public static function parse($string, $name = '', $reference_time = null) {
    388         $parts   = explode(';', $string);
     404     * @param string $cookie_header Cookie header value (from a Set-Cookie header)
     405     * @param string $name
     406     * @param int|null $reference_time
     407     * @return \WpOrg\Requests\Cookie Parsed cookie object
     408     *
     409     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cookie_header argument is not a string.
     410     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string.
     411     */
     412    public static function parse($cookie_header, $name = '', $reference_time = null) {
     413        if (is_string($cookie_header) === false) {
     414            throw InvalidArgument::create(1, '$cookie_header', 'string', gettype($cookie_header));
     415        }
     416
     417        if (is_string($name) === false) {
     418            throw InvalidArgument::create(2, '$name', 'string', gettype($name));
     419        }
     420
     421        $parts   = explode(';', $cookie_header);
    389422        $kvparts = array_shift($parts);
    390423
    391424        if (!empty($name)) {
    392             $value = $string;
     425            $value = $cookie_header;
    393426        }
    394427        elseif (strpos($kvparts, '=') === false) {
     
    407440        $value = trim($value);
    408441
    409         // Attribute key are handled case-insensitively
    410         $attributes = new Requests_Utility_CaseInsensitiveDictionary();
     442        // Attribute keys are handled case-insensitively
     443        $attributes = new CaseInsensitiveDictionary();
    411444
    412445        if (!empty($parts)) {
     
    426459        }
    427460
    428         return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);
     461        return new static($name, $value, $attributes, [], $reference_time);
    429462    }
    430463
     
    432465     * Parse all Set-Cookie headers from request headers
    433466     *
    434      * @param Requests_Response_Headers $headers Headers to parse from
    435      * @param Requests_IRI|null $origin URI for comparing cookie origins
     467     * @param \WpOrg\Requests\Response\Headers $headers Headers to parse from
     468     * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins
    436469     * @param int|null $time Reference time for expiration calculation
    437470     * @return array
    438471     */
    439     public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) {
     472    public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) {
    440473        $cookie_headers = $headers->getValues('Set-Cookie');
    441474        if (empty($cookie_headers)) {
    442             return array();
    443         }
    444 
    445         $cookies = array();
     475            return [];
     476        }
     477
     478        $cookies = [];
    446479        foreach ($cookie_headers as $header) {
    447480            $parsed = self::parse($header, '', $time);
     
    492525        return $cookies;
    493526    }
    494 
    495     /**
    496      * Parse all Set-Cookie headers from request headers
    497      *
    498      * @codeCoverageIgnore
    499      * @deprecated Use {@see Requests_Cookie::parse_from_headers}
    500      * @return array
    501      */
    502     public static function parseFromHeaders(Requests_Response_Headers $headers) {
    503         return self::parse_from_headers($headers);
    504     }
    505527}
Note: See TracChangeset for help on using the changeset viewer.