Make WordPress Core


Ignore:
Timestamp:
12/06/2021 09:29:00 PM (3 years ago)
Author:
SergeyBiryukov
Message:

HTTP API: Revert changeset [52244].

Reverting Requests 2.0.0 changes and moving to WordPress 6.0 cycle. Why? The namespace and file case renaming revealed 2 issues in Core's upgrader process.

See https://core.trac.wordpress.org/ticket/54504#comment:22 for more information.

Follow-up to [52327].

See #54562, #54504.

File:
1 edited

Legend:

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

    r52244 r52328  
    33 * Cookie storage object
    44 *
    5  * @package Requests\Cookies
     5 * @package Requests
     6 * @subpackage Cookies
    67 */
    7 
    8 namespace WpOrg\Requests;
    9 
    10 use WpOrg\Requests\Exception\InvalidArgument;
    11 use WpOrg\Requests\Iri;
    12 use WpOrg\Requests\Response\Headers;
    13 use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
    14 use WpOrg\Requests\Utility\InputValidator;
    158
    169/**
    1710 * Cookie storage object
    1811 *
    19  * @package Requests\Cookies
     12 * @package Requests
     13 * @subpackage Cookies
    2014 */
    21 class Cookie {
     15class Requests_Cookie {
    2216    /**
    2317     * Cookie name.
     
    4034     * httponly.
    4135     *
    42      * @var \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array Array-like object
    43      */
    44     public $attributes = [];
     36     * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object
     37     */
     38    public $attributes = array();
    4539
    4640    /**
     
    5246     * @var array
    5347     */
    54     public $flags = [];
     48    public $flags = array();
    5549
    5650    /**
     
    6963     * @param string $name
    7064     * @param string $value
    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 
     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) {
    10268        $this->name       = $name;
    10369        $this->value      = $value;
    10470        $this->attributes = $attributes;
    105         $default_flags    = [
     71        $default_flags    = array(
    10672            'creation'    => time(),
    10773            'last-access' => time(),
    10874            'persistent'  => false,
    10975            'host-only'   => true,
    110         ];
     76        );
    11177        $this->flags      = array_merge($default_flags, $flags);
    11278
     
    11783
    11884        $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;
    12885    }
    12986
     
    157114     * Check if a cookie is valid for a given URI
    158115     *
    159      * @param \WpOrg\Requests\Iri $uri URI to check
     116     * @param Requests_IRI $uri URI to check
    160117     * @return boolean Whether the cookie is valid for the given URI
    161118     */
    162     public function uri_matches(Iri $uri) {
     119    public function uri_matches(Requests_IRI $uri) {
    163120        if (!$this->domain_matches($uri->host)) {
    164121            return false;
     
    175132     * Check if a cookie is valid for a given domain
    176133     *
    177      * @param string $domain Domain to check
     134     * @param string $string Domain to check
    178135     * @return boolean Whether the cookie is valid for the given domain
    179136     */
    180     public function domain_matches($domain) {
    181         if (is_string($domain) === false) {
    182             return false;
    183         }
    184 
     137    public function domain_matches($string) {
    185138        if (!isset($this->attributes['domain'])) {
    186139            // Cookies created manually; cookies created by Requests will set
     
    189142        }
    190143
    191         $cookie_domain = $this->attributes['domain'];
    192         if ($cookie_domain === $domain) {
    193             // The cookie domain and the passed domain are identical.
     144        $domain_string = $this->attributes['domain'];
     145        if ($domain_string === $string) {
     146            // The domain string and the string are identical.
    194147            return true;
    195148        }
     
    201154        }
    202155
    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
     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
    206159            return false;
    207160        }
    208161
    209         if (substr($domain, -1 * strlen($cookie_domain)) !== $cookie_domain) {
    210             // The cookie domain should be a suffix of the passed domain.
     162        if (substr($string, -1 * strlen($domain_string)) !== $domain_string) {
     163            // The domain string should be a suffix of the string.
    211164            return false;
    212165        }
    213166
    214         $prefix = substr($domain, 0, strlen($domain) - strlen($cookie_domain));
     167        $prefix = substr($string, 0, strlen($string) - strlen($domain_string));
    215168        if (substr($prefix, -1) !== '.') {
    216             // The last character of the passed domain that is not included in the
     169            // The last character of the string that is not included in the
    217170            // domain string should be a %x2E (".") character.
    218171            return false;
    219172        }
    220173
    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);
     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);
    223176    }
    224177
     
    241194            // the path to the requested path
    242195            return true;
    243         }
    244 
    245         if (is_scalar($request_path) === false) {
    246             return false;
    247196        }
    248197
     
    369318
    370319    /**
     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    /**
    371331     * Format a cookie for a Set-Cookie header
    372332     *
     
    379339        $header_value = $this->format_for_header();
    380340        if (!empty($this->attributes)) {
    381             $parts = [];
     341            $parts = array();
    382342            foreach ($this->attributes as $key => $value) {
    383343                // Ignore non-associative attributes
     
    396356
    397357    /**
     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    /**
    398378     * Parse a cookie string into a cookie object
    399379     *
     
    402382     * specifies some of this handling, but not in a thorough manner.
    403383     *
    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);
     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);
    422389        $kvparts = array_shift($parts);
    423390
    424391        if (!empty($name)) {
    425             $value = $cookie_header;
     392            $value = $string;
    426393        }
    427394        elseif (strpos($kvparts, '=') === false) {
     
    440407        $value = trim($value);
    441408
    442         // Attribute keys are handled case-insensitively
    443         $attributes = new CaseInsensitiveDictionary();
     409        // Attribute key are handled case-insensitively
     410        $attributes = new Requests_Utility_CaseInsensitiveDictionary();
    444411
    445412        if (!empty($parts)) {
     
    459426        }
    460427
    461         return new static($name, $value, $attributes, [], $reference_time);
     428        return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);
    462429    }
    463430
     
    465432     * Parse all Set-Cookie headers from request headers
    466433     *
    467      * @param \WpOrg\Requests\Response\Headers $headers Headers to parse from
    468      * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins
     434     * @param Requests_Response_Headers $headers Headers to parse from
     435     * @param Requests_IRI|null $origin URI for comparing cookie origins
    469436     * @param int|null $time Reference time for expiration calculation
    470437     * @return array
    471438     */
    472     public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) {
     439    public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) {
    473440        $cookie_headers = $headers->getValues('Set-Cookie');
    474441        if (empty($cookie_headers)) {
    475             return [];
    476         }
    477 
    478         $cookies = [];
     442            return array();
     443        }
     444
     445        $cookies = array();
    479446        foreach ($cookie_headers as $header) {
    480447            $parsed = self::parse($header, '', $time);
     
    525492        return $cookies;
    526493    }
     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    }
    527505}
Note: See TracChangeset for help on using the changeset viewer.