Make WordPress Core

Changeset 52328


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.

Location:
trunk
Files:
18 deleted
26 edited
11 copied

Legend:

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

    r52244 r52328  
    33 * Authentication provider interface
    44 *
    5  * @package Requests\Authentication
     5 * @package Requests
     6 * @subpackage Authentication
    67 */
    7 
    8 namespace WpOrg\Requests;
    9 
    10 use WpOrg\Requests\Hooks;
    118
    129/**
     
    1815 * makes it much easier for users to use your provider.
    1916 *
    20  * @see \WpOrg\Requests\Hooks
    21  *
    22  * @package Requests\Authentication
     17 * @see Requests_Hooks
     18 * @package Requests
     19 * @subpackage Authentication
    2320 */
    24 interface Auth {
     21interface Requests_Auth {
    2522    /**
    2623     * Register hooks as needed
    2724     *
    28      * This method is called in {@see \WpOrg\Requests\Requests::request()} when the user
    29      * has set an instance as the 'auth' option. Use this callback to register all the
     25     * This method is called in {@see Requests::request} when the user has set
     26     * an instance as the 'auth' option. Use this callback to register all the
    3027     * hooks you'll need.
    3128     *
    32      * @see \WpOrg\Requests\Hooks::register()
    33      * @param \WpOrg\Requests\Hooks $hooks Hook system
     29     * @see Requests_Hooks::register
     30     * @param Requests_Hooks $hooks Hook system
    3431     */
    35     public function register(Hooks $hooks);
     32    public function register(Requests_Hooks $hooks);
    3633}
  • trunk/src/wp-includes/Requests/Auth/Basic.php

    r52244 r52328  
    33 * Basic Authentication provider
    44 *
    5  * @package Requests\Authentication
     5 * @package Requests
     6 * @subpackage Authentication
    67 */
    7 
    8 namespace WpOrg\Requests\Auth;
    9 
    10 use WpOrg\Requests\Auth;
    11 use WpOrg\Requests\Exception\ArgumentCount;
    12 use WpOrg\Requests\Exception\InvalidArgument;
    13 use WpOrg\Requests\Hooks;
    148
    159/**
     
    1913 * header.
    2014 *
    21  * @package Requests\Authentication
     15 * @package Requests
     16 * @subpackage Authentication
    2217 */
    23 class Basic implements Auth {
     18class Requests_Auth_Basic implements Requests_Auth {
    2419    /**
    2520     * Username
     
    3934     * Constructor
    4035     *
    41      * @since 2.0 Throws an `InvalidArgument` exception.
    42      * @since 2.0 Throws an `ArgumentCount` exception instead of the Requests base `Exception.
    43      *
     36     * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`)
    4437     * @param array|null $args Array of user and password. Must have exactly two elements
    45      *
    46      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array or null.
    47      * @throws \WpOrg\Requests\Exception\ArgumentCount   On incorrect number of array elements (`authbasicbadargs`).
    4838     */
    4939    public function __construct($args = null) {
    5040        if (is_array($args)) {
    5141            if (count($args) !== 2) {
    52                 throw ArgumentCount::create('an array with exactly two elements', count($args), 'authbasicbadargs');
     42                throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs');
    5343            }
    5444
    5545            list($this->user, $this->pass) = $args;
    56             return;
    57         }
    58 
    59         if ($args !== null) {
    60             throw InvalidArgument::create(1, '$args', 'array|null', gettype($args));
    6146        }
    6247    }
     
    6550     * Register the necessary callbacks
    6651     *
    67      * @see \WpOrg\Requests\Auth\Basic::curl_before_send()
    68      * @see \WpOrg\Requests\Auth\Basic::fsockopen_header()
    69      * @param \WpOrg\Requests\Hooks $hooks Hook system
     52     * @see curl_before_send
     53     * @see fsockopen_header
     54     * @param Requests_Hooks $hooks Hook system
    7055     */
    71     public function register(Hooks $hooks) {
    72         $hooks->register('curl.before_send', [$this, 'curl_before_send']);
    73         $hooks->register('fsockopen.after_headers', [$this, 'fsockopen_header']);
     56    public function register(Requests_Hooks $hooks) {
     57        $hooks->register('curl.before_send', array($this, 'curl_before_send'));
     58        $hooks->register('fsockopen.after_headers', array($this, 'fsockopen_header'));
    7459    }
    7560
     
    7762     * Set cURL parameters before the data is sent
    7863     *
    79      * @param resource|\CurlHandle $handle cURL handle
     64     * @param resource $handle cURL resource
    8065     */
    8166    public function curl_before_send(&$handle) {
  • 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}
  • trunk/src/wp-includes/Requests/Cookie/Jar.php

    r52244 r52328  
    33 * Cookie holder object
    44 *
    5  * @package Requests\Cookies
     5 * @package Requests
     6 * @subpackage Cookies
    67 */
    7 
    8 namespace WpOrg\Requests\Cookie;
    9 
    10 use ArrayAccess;
    11 use ArrayIterator;
    12 use IteratorAggregate;
    13 use ReturnTypeWillChange;
    14 use WpOrg\Requests\Cookie;
    15 use WpOrg\Requests\Exception;
    16 use WpOrg\Requests\Exception\InvalidArgument;
    17 use WpOrg\Requests\HookManager;
    18 use WpOrg\Requests\Iri;
    19 use WpOrg\Requests\Response;
    208
    219/**
    2210 * Cookie holder object
    2311 *
    24  * @package Requests\Cookies
     12 * @package Requests
     13 * @subpackage Cookies
    2514 */
    26 class Jar implements ArrayAccess, IteratorAggregate {
     15class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate {
    2716    /**
    2817     * Actual item data
     
    3019     * @var array
    3120     */
    32     protected $cookies = [];
     21    protected $cookies = array();
    3322
    3423    /**
     
    3625     *
    3726     * @param array $cookies Existing cookie values
    38      *
    39      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array.
    4027     */
    41     public function __construct($cookies = []) {
    42         if (is_array($cookies) === false) {
    43             throw InvalidArgument::create(1, '$cookies', 'array', gettype($cookies));
    44         }
    45 
     28    public function __construct($cookies = array()) {
    4629        $this->cookies = $cookies;
    4730    }
    4831
    4932    /**
    50      * Normalise cookie data into a \WpOrg\Requests\Cookie
     33     * Normalise cookie data into a Requests_Cookie
    5134     *
    52      * @param string|\WpOrg\Requests\Cookie $cookie
    53      * @return \WpOrg\Requests\Cookie
     35     * @param string|Requests_Cookie $cookie
     36     * @return Requests_Cookie
    5437     */
    55     public function normalize_cookie($cookie, $key = '') {
    56         if ($cookie instanceof Cookie) {
     38    public function normalize_cookie($cookie, $key = null) {
     39        if ($cookie instanceof Requests_Cookie) {
    5740            return $cookie;
    5841        }
    5942
    60         return Cookie::parse($cookie, $key);
     43        return Requests_Cookie::parse($cookie, $key);
     44    }
     45
     46    /**
     47     * Normalise cookie data into a Requests_Cookie
     48     *
     49     * @codeCoverageIgnore
     50     * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie}
     51     * @return Requests_Cookie
     52     */
     53    public function normalizeCookie($cookie, $key = null) {
     54        return $this->normalize_cookie($cookie, $key);
    6155    }
    6256
     
    6458     * Check if the given item exists
    6559     *
    66      * @param string $offset Item key
     60     * @param string $key Item key
    6761     * @return boolean Does the item exist?
    6862     */
    69     #[ReturnTypeWillChange]
    70     public function offsetExists($offset) {
    71         return isset($this->cookies[$offset]);
     63    public function offsetExists($key) {
     64        return isset($this->cookies[$key]);
    7265    }
    7366
     
    7568     * Get the value for the item
    7669     *
    77      * @param string $offset Item key
     70     * @param string $key Item key
    7871     * @return string|null Item value (null if offsetExists is false)
    7972     */
    80     #[ReturnTypeWillChange]
    81     public function offsetGet($offset) {
    82         if (!isset($this->cookies[$offset])) {
     73    public function offsetGet($key) {
     74        if (!isset($this->cookies[$key])) {
    8375            return null;
    8476        }
    8577
    86         return $this->cookies[$offset];
     78        return $this->cookies[$key];
    8779    }
    8880
     
    9082     * Set the given item
    9183     *
    92      * @param string $offset Item name
     84     * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
     85     *
     86     * @param string $key Item name
    9387     * @param string $value Item value
    94      *
    95      * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
    9688     */
    97     #[ReturnTypeWillChange]
    98     public function offsetSet($offset, $value) {
    99         if ($offset === null) {
    100             throw new Exception('Object is a dictionary, not a list', 'invalidset');
     89    public function offsetSet($key, $value) {
     90        if ($key === null) {
     91            throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
    10192        }
    10293
    103         $this->cookies[$offset] = $value;
     94        $this->cookies[$key] = $value;
    10495    }
    10596
     
    10798     * Unset the given header
    10899     *
    109      * @param string $offset
     100     * @param string $key
    110101     */
    111     #[ReturnTypeWillChange]
    112     public function offsetUnset($offset) {
    113         unset($this->cookies[$offset]);
     102    public function offsetUnset($key) {
     103        unset($this->cookies[$key]);
    114104    }
    115105
     
    117107     * Get an iterator for the data
    118108     *
    119      * @return \ArrayIterator
     109     * @return ArrayIterator
    120110     */
    121     #[ReturnTypeWillChange]
    122111    public function getIterator() {
    123112        return new ArrayIterator($this->cookies);
     
    127116     * Register the cookie handler with the request's hooking system
    128117     *
    129      * @param \WpOrg\Requests\HookManager $hooks Hooking system
     118     * @param Requests_Hooker $hooks Hooking system
    130119     */
    131     public function register(HookManager $hooks) {
    132         $hooks->register('requests.before_request', [$this, 'before_request']);
    133         $hooks->register('requests.before_redirect_check', [$this, 'before_redirect_check']);
     120    public function register(Requests_Hooker $hooks) {
     121        $hooks->register('requests.before_request', array($this, 'before_request'));
     122        $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check'));
    134123    }
    135124
     
    146135     */
    147136    public function before_request($url, &$headers, &$data, &$type, &$options) {
    148         if (!$url instanceof Iri) {
    149             $url = new Iri($url);
     137        if (!$url instanceof Requests_IRI) {
     138            $url = new Requests_IRI($url);
    150139        }
    151140
    152141        if (!empty($this->cookies)) {
    153             $cookies = [];
     142            $cookies = array();
    154143            foreach ($this->cookies as $key => $cookie) {
    155144                $cookie = $this->normalize_cookie($cookie, $key);
     
    172161     * Parse all cookies from a response and attach them to the response
    173162     *
    174      * @param \WpOrg\Requests\Response $response
     163     * @var Requests_Response $response
    175164     */
    176     public function before_redirect_check(Response $response) {
    177         $url = $response->url;
    178         if (!$url instanceof Iri) {
    179             $url = new Iri($url);
     165    public function before_redirect_check(Requests_Response $return) {
     166        $url = $return->url;
     167        if (!$url instanceof Requests_IRI) {
     168            $url = new Requests_IRI($url);
    180169        }
    181170
    182         $cookies           = Cookie::parse_from_headers($response->headers, $url);
    183         $this->cookies     = array_merge($this->cookies, $cookies);
    184         $response->cookies = $this;
     171        $cookies         = Requests_Cookie::parse_from_headers($return->headers, $url);
     172        $this->cookies   = array_merge($this->cookies, $cookies);
     173        $return->cookies = $this;
    185174    }
    186175}
  • trunk/src/wp-includes/Requests/Exception.php

    r52244 r52328  
    33 * Exception for HTTP requests
    44 *
    5  * @package Requests\Exceptions
     5 * @package Requests
    66 */
    7 
    8 namespace WpOrg\Requests;
    9 
    10 use Exception as PHPException;
    117
    128/**
    139 * Exception for HTTP requests
    1410 *
    15  * @package Requests\Exceptions
     11 * @package Requests
    1612 */
    17 class Exception extends PHPException {
     13class Requests_Exception extends Exception {
    1814    /**
    1915     * Type of exception
     
    4642
    4743    /**
    48      * Like {@see \Exception::getCode()}, but a string code.
     44     * Like {@see getCode()}, but a string code.
    4945     *
    5046     * @codeCoverageIgnore
  • trunk/src/wp-includes/Requests/Exception/Transport.php

    r52244 r52328  
    11<?php
    2 /**
    3  * Transport Exception
    4  *
    5  * @package Requests\Exceptions
    6  */
    72
    8 namespace WpOrg\Requests\Exception;
     3class Requests_Exception_Transport extends Requests_Exception {
    94
    10 use WpOrg\Requests\Exception;
    11 
    12 /**
    13  * Transport Exception
    14  *
    15  * @package Requests\Exceptions
    16  */
    17 class Transport extends Exception {}
     5}
  • trunk/src/wp-includes/Requests/Hooks.php

    r52244 r52328  
    33 * Handles adding and dispatching events
    44 *
    5  * @package Requests\EventDispatcher
     5 * @package Requests
     6 * @subpackage Utilities
    67 */
    7 
    8 namespace WpOrg\Requests;
    9 
    10 use WpOrg\Requests\Exception\InvalidArgument;
    11 use WpOrg\Requests\HookManager;
    12 use WpOrg\Requests\Utility\InputValidator;
    138
    149/**
    1510 * Handles adding and dispatching events
    1611 *
    17  * @package Requests\EventDispatcher
     12 * @package Requests
     13 * @subpackage Utilities
    1814 */
    19 class Hooks implements HookManager {
     15class Requests_Hooks implements Requests_Hooker {
    2016    /**
    2117     * Registered callbacks for each hook
     
    2319     * @var array
    2420     */
    25     protected $hooks = [];
     21    protected $hooks = array();
     22
     23    /**
     24     * Constructor
     25     */
     26    public function __construct() {
     27        // pass
     28    }
    2629
    2730    /**
     
    2932     *
    3033     * @param string $hook Hook name
    31      * @param callable $callback Function/method to call on event
     34     * @param callback $callback Function/method to call on event
    3235     * @param int $priority Priority number. <0 is executed earlier, >0 is executed later
    33      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string.
    34      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $callback argument is not callable.
    35      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $priority argument is not an integer.
    3636     */
    3737    public function register($hook, $callback, $priority = 0) {
    38         if (is_string($hook) === false) {
    39             throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
     38        if (!isset($this->hooks[$hook])) {
     39            $this->hooks[$hook] = array();
    4040        }
    41 
    42         if (is_callable($callback) === false) {
    43             throw InvalidArgument::create(2, '$callback', 'callable', gettype($callback));
    44         }
    45 
    46         if (InputValidator::is_numeric_array_key($priority) === false) {
    47             throw InvalidArgument::create(3, '$priority', 'integer', gettype($priority));
    48         }
    49 
    50         if (!isset($this->hooks[$hook])) {
    51             $this->hooks[$hook] = [
    52                 $priority => [],
    53             ];
    54         } elseif (!isset($this->hooks[$hook][$priority])) {
    55             $this->hooks[$hook][$priority] = [];
     41        if (!isset($this->hooks[$hook][$priority])) {
     42            $this->hooks[$hook][$priority] = array();
    5643        }
    5744
     
    6552     * @param array $parameters Parameters to pass to callbacks
    6653     * @return boolean Successfulness
    67      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string.
    68      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $parameters argument is not an array.
    6954     */
    70     public function dispatch($hook, $parameters = []) {
    71         if (is_string($hook) === false) {
    72             throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
    73         }
    74 
    75         // Check strictly against array, as Array* objects don't work in combination with `call_user_func_array()`.
    76         if (is_array($parameters) === false) {
    77             throw InvalidArgument::create(2, '$parameters', 'array', gettype($parameters));
    78         }
    79 
     55    public function dispatch($hook, $parameters = array()) {
    8056        if (empty($this->hooks[$hook])) {
    8157            return false;
    8258        }
    8359
    84         if (!empty($parameters)) {
    85             // Strip potential keys from the array to prevent them being interpreted as parameter names in PHP 8.0.
    86             $parameters = array_values($parameters);
    87         }
    88 
    8960        foreach ($this->hooks[$hook] as $priority => $hooked) {
    9061            foreach ($hooked as $callback) {
    91                 $callback(...$parameters);
     62                call_user_func_array($callback, $parameters);
    9263            }
    9364        }
  • trunk/src/wp-includes/Requests/Proxy.php

    r52244 r52328  
    33 * Proxy connection interface
    44 *
    5  * @package Requests\Proxy
    6  * @since   1.6
     5 * @package Requests
     6 * @subpackage Proxy
     7 * @since 1.6
    78 */
    8 
    9 namespace WpOrg\Requests;
    10 
    11 use WpOrg\Requests\Hooks;
    129
    1310/**
     
    1916 * makes it much easier for users to use your provider.
    2017 *
    21  * @see \WpOrg\Requests\Hooks
    22  *
    23  * @package Requests\Proxy
    24  * @since   1.6
     18 * @see Requests_Hooks
     19 * @package Requests
     20 * @subpackage Proxy
     21 * @since 1.6
    2522 */
    26 interface Proxy {
     23interface Requests_Proxy {
    2724    /**
    2825     * Register hooks as needed
    2926     *
    30      * This method is called in {@see \WpOrg\Requests\Requests::request()} when the user
    31      * has set an instance as the 'auth' option. Use this callback to register all the
     27     * This method is called in {@see Requests::request} when the user has set
     28     * an instance as the 'auth' option. Use this callback to register all the
    3229     * hooks you'll need.
    3330     *
    34      * @see \WpOrg\Requests\Hooks::register()
    35      * @param \WpOrg\Requests\Hooks $hooks Hook system
     31     * @see Requests_Hooks::register
     32     * @param Requests_Hooks $hooks Hook system
    3633     */
    37     public function register(Hooks $hooks);
     34    public function register(Requests_Hooks $hooks);
    3835}
  • trunk/src/wp-includes/Requests/Response.php

    r52244 r52328  
    33 * HTTP response class
    44 *
    5  * Contains a response from \WpOrg\Requests\Requests::request()
    6  *
     5 * Contains a response from Requests::request()
    76 * @package Requests
    87 */
    9 
    10 namespace WpOrg\Requests;
    11 
    12 use WpOrg\Requests\Cookie\Jar;
    13 use WpOrg\Requests\Exception;
    14 use WpOrg\Requests\Exception\Http;
    15 use WpOrg\Requests\Response\Headers;
    168
    179/**
    1810 * HTTP response class
    1911 *
    20  * Contains a response from \WpOrg\Requests\Requests::request()
    21  *
     12 * Contains a response from Requests::request()
    2213 * @package Requests
    2314 */
    24 class Response {
     15class Requests_Response {
     16    /**
     17     * Constructor
     18     */
     19    public function __construct() {
     20        $this->headers = new Requests_Response_Headers();
     21        $this->cookies = new Requests_Cookie_Jar();
     22    }
    2523
    2624    /**
     
    4139     * Headers, as an associative array
    4240     *
    43      * @var \WpOrg\Requests\Response\Headers Array-like object representing headers
     41     * @var Requests_Response_Headers Array-like object representing headers
    4442     */
    45     public $headers = [];
     43    public $headers = array();
    4644
    4745    /**
     
    8381     * Previous requests (from redirects)
    8482     *
    85      * @var array Array of \WpOrg\Requests\Response objects
     83     * @var array Array of Requests_Response objects
    8684     */
    87     public $history = [];
     85    public $history = array();
    8886
    8987    /**
    9088     * Cookies from the request
    9189     *
    92      * @var \WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar
     90     * @var Requests_Cookie_Jar Array-like object representing a cookie jar
    9391     */
    94     public $cookies = [];
    95 
    96     /**
    97      * Constructor
    98      */
    99     public function __construct() {
    100         $this->headers = new Headers();
    101         $this->cookies = new Jar();
    102     }
     92    public $cookies = array();
    10393
    10494    /**
     
    10999    public function is_redirect() {
    110100        $code = $this->status_code;
    111         return in_array($code, [300, 301, 302, 303, 307], true) || $code > 307 && $code < 400;
     101        return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;
    112102    }
    113103
     
    115105     * Throws an exception if the request was not successful
    116106     *
     107     * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
     108     * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
    117109     * @param boolean $allow_redirects Set to false to throw on a 3xx as well
    118      *
    119      * @throws \WpOrg\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
    120      * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to "Status" + code (e.g. {@see \WpOrg\Requests\Exception\Http\Status404})
    121110     */
    122111    public function throw_for_status($allow_redirects = true) {
    123112        if ($this->is_redirect()) {
    124             if ($allow_redirects !== true) {
    125                 throw new Exception('Redirection not allowed', 'response.no_redirects', $this);
     113            if (!$allow_redirects) {
     114                throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
    126115            }
    127116        }
    128117        elseif (!$this->success) {
    129             $exception = Http::get_class($this->status_code);
     118            $exception = Requests_Exception_HTTP::get_class($this->status_code);
    130119            throw new $exception(null, $this);
    131120        }
    132121    }
    133 
    134     /**
    135      * JSON decode the response body.
    136      *
    137      * The method parameters are the same as those for the PHP native `json_decode()` function.
    138      *
    139      * @link https://php.net/json-decode
    140      *
    141      * @param ?bool $associative Optional. When `true`, JSON objects will be returned as associative arrays;
    142      *                           When `false`, JSON objects will be returned as objects.
    143      *                           When `null`, JSON objects will be returned as associative arrays
    144      *                           or objects depending on whether `JSON_OBJECT_AS_ARRAY` is set in the flags.
    145      *                           Defaults to `true` (in contrast to the PHP native default of `null`).
    146      * @param int   $depth       Optional. Maximum nesting depth of the structure being decoded.
    147      *                           Defaults to `512`.
    148      * @param int   $options     Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE,
    149      *                           JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.
    150      *                           Defaults to `0` (no options set).
    151      *
    152      * @return array
    153      *
    154      * @throws \WpOrg\Requests\Exception If `$this->body` is not valid json.
    155      */
    156     public function decode_body($associative = true, $depth = 512, $options = 0) {
    157         $data = json_decode($this->body, $associative, $depth, $options);
    158 
    159         if (json_last_error() !== JSON_ERROR_NONE) {
    160             $last_error = json_last_error_msg();
    161             throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this);
    162         }
    163 
    164         return $data;
    165     }
    166122}
  • trunk/src/wp-includes/Requests/Response/Headers.php

    r52244 r52328  
    66 */
    77
    8 namespace WpOrg\Requests\Response;
    9 
    10 use WpOrg\Requests\Exception;
    11 use WpOrg\Requests\Exception\InvalidArgument;
    12 use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
    13 use WpOrg\Requests\Utility\FilteredIterator;
    14 
    158/**
    169 * Case-insensitive dictionary, suitable for HTTP headers
     
    1811 * @package Requests
    1912 */
    20 class Headers extends CaseInsensitiveDictionary {
     13class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
    2114    /**
    2215     * Get the given header
    2316     *
    24      * Unlike {@see \WpOrg\Requests\Response\Headers::getValues()}, this returns a string. If there are
     17     * Unlike {@see self::getValues()}, this returns a string. If there are
    2518     * multiple values, it concatenates them with a comma as per RFC2616.
    2619     *
     
    2821     * Set-Cookie headers.
    2922     *
    30      * @param string $offset
     23     * @param string $key
    3124     * @return string|null Header value
    3225     */
    33     public function offsetGet($offset) {
    34         if (is_string($offset)) {
    35             $offset = strtolower($offset);
    36         }
    37 
    38         if (!isset($this->data[$offset])) {
     26    public function offsetGet($key) {
     27        $key = strtolower($key);
     28        if (!isset($this->data[$key])) {
    3929            return null;
    4030        }
    4131
    42         return $this->flatten($this->data[$offset]);
     32        return $this->flatten($this->data[$key]);
    4333    }
    4434
     
    4636     * Set the given item
    4737     *
    48      * @param string $offset Item name
     38     * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
     39     *
     40     * @param string $key Item name
    4941     * @param string $value Item value
    50      *
    51      * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
    5242     */
    53     public function offsetSet($offset, $value) {
    54         if ($offset === null) {
    55             throw new Exception('Object is a dictionary, not a list', 'invalidset');
     43    public function offsetSet($key, $value) {
     44        if ($key === null) {
     45            throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
    5646        }
    5747
    58         if (is_string($offset)) {
    59             $offset = strtolower($offset);
     48        $key = strtolower($key);
     49
     50        if (!isset($this->data[$key])) {
     51            $this->data[$key] = array();
    6052        }
    6153
    62         if (!isset($this->data[$offset])) {
    63             $this->data[$offset] = [];
    64         }
    65 
    66         $this->data[$offset][] = $value;
     54        $this->data[$key][] = $value;
    6755    }
    6856
     
    7058     * Get all values for a given header
    7159     *
    72      * @param string $offset
     60     * @param string $key
    7361     * @return array|null Header values
    74      *
    75      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not valid as an array key.
    7662     */
    77     public function getValues($offset) {
    78         if (!is_string($offset) && !is_int($offset)) {
    79             throw InvalidArgument::create(1, '$offset', 'string|int', gettype($offset));
    80         }
    81 
    82         $offset = strtolower($offset);
    83         if (!isset($this->data[$offset])) {
     63    public function getValues($key) {
     64        $key = strtolower($key);
     65        if (!isset($this->data[$key])) {
    8466            return null;
    8567        }
    8668
    87         return $this->data[$offset];
     69        return $this->data[$key];
    8870    }
    8971
     
    9678     * @param string|array $value Value to flatten
    9779     * @return string Flattened value
    98      *
    99      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or an array.
    10080     */
    10181    public function flatten($value) {
    102         if (is_string($value)) {
    103             return $value;
     82        if (is_array($value)) {
     83            $value = implode(',', $value);
    10484        }
    10585
    106         if (is_array($value)) {
    107             return implode(',', $value);
    108         }
    109 
    110         throw InvalidArgument::create(1, '$value', 'string|array', gettype($value));
     86        return $value;
    11187    }
    11288
     
    11490     * Get an iterator for the data
    11591     *
    116      * Converts the internally stored values to a comma-separated string if there is more
    117      * than one value for a key.
    118      *
    119      * @return \ArrayIterator
     92     * Converts the internal
     93     * @return ArrayIterator
    12094     */
    12195    public function getIterator() {
    122         return new FilteredIterator($this->data, [$this, 'flatten']);
     96        return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));
    12397    }
    12498}
  • trunk/src/wp-includes/Requests/Session.php

    r52244 r52328  
    33 * Session handler for persistent requests and default parameters
    44 *
    5  * @package Requests\SessionHandler
     5 * @package Requests
     6 * @subpackage Session Handler
    67 */
    7 
    8 namespace WpOrg\Requests;
    9 
    10 use WpOrg\Requests\Cookie\Jar;
    11 use WpOrg\Requests\Exception\InvalidArgument;
    12 use WpOrg\Requests\Iri;
    13 use WpOrg\Requests\Requests;
    14 use WpOrg\Requests\Utility\InputValidator;
    158
    169/**
     
    2215 * a shared cookie jar), then overridden for individual requests.
    2316 *
    24  * @package Requests\SessionHandler
     17 * @package Requests
     18 * @subpackage Session Handler
    2519 */
    26 class Session {
     20class Requests_Session {
    2721    /**
    2822     * Base URL for requests
     
    3933     * @var array
    4034     */
    41     public $headers = [];
     35    public $headers = array();
    4236
    4337    /**
     
    4943     * @var array
    5044     */
    51     public $data = [];
     45    public $data = array();
    5246
    5347    /**
     
    6256     * @var array
    6357     */
    64     public $options = [];
     58    public $options = array();
    6559
    6660    /**
    6761     * Create a new session
    6862     *
    69      * @param string|Stringable|null $url Base URL for requests
     63     * @param string|null $url Base URL for requests
    7064     * @param array $headers Default headers for requests
    7165     * @param array $data Default data for requests
    7266     * @param array $options Default options for requests
    73      *
    74      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string, Stringable or null.
    75      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $headers argument is not an array.
    76      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data argument is not an array.
    77      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
    78      */
    79     public function __construct($url = null, $headers = [], $data = [], $options = []) {
    80         if ($url !== null && InputValidator::is_string_or_stringable($url) === false) {
    81             throw InvalidArgument::create(1, '$url', 'string|Stringable|null', gettype($url));
    82         }
    83 
    84         if (is_array($headers) === false) {
    85             throw InvalidArgument::create(2, '$headers', 'array', gettype($headers));
    86         }
    87 
    88         if (is_array($data) === false) {
    89             throw InvalidArgument::create(3, '$data', 'array', gettype($data));
    90         }
    91 
    92         if (is_array($options) === false) {
    93             throw InvalidArgument::create(4, '$options', 'array', gettype($options));
    94         }
    95 
     67     */
     68    public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
    9669        $this->url     = $url;
    9770        $this->headers = $headers;
     
    10073
    10174        if (empty($this->options['cookies'])) {
    102             $this->options['cookies'] = new Jar();
     75            $this->options['cookies'] = new Requests_Cookie_Jar();
    10376        }
    10477    }
     
    10780     * Get a property's value
    10881     *
    109      * @param string $name Property name.
     82     * @param string $key Property key
    11083     * @return mixed|null Property value, null if none found
    11184     */
    112     public function __get($name) {
    113         if (isset($this->options[$name])) {
    114             return $this->options[$name];
     85    public function __get($key) {
     86        if (isset($this->options[$key])) {
     87            return $this->options[$key];
    11588        }
    11689
     
    12194     * Set a property's value
    12295     *
    123      * @param string $name Property name.
     96     * @param string $key Property key
    12497     * @param mixed $value Property value
    12598     */
    126     public function __set($name, $value) {
    127         $this->options[$name] = $value;
     99    public function __set($key, $value) {
     100        $this->options[$key] = $value;
    128101    }
    129102
     
    131104     * Remove a property's value
    132105     *
    133      * @param string $name Property name.
    134      */
    135     public function __isset($name) {
    136         return isset($this->options[$name]);
     106     * @param string $key Property key
     107     */
     108    public function __isset($key) {
     109        return isset($this->options[$key]);
    137110    }
    138111
     
    140113     * Remove a property's value
    141114     *
    142      * @param string $name Property name.
    143      */
    144     public function __unset($name) {
    145         unset($this->options[$name]);
     115     * @param string $key Property key
     116     */
     117    public function __unset($key) {
     118        if (isset($this->options[$key])) {
     119            unset($this->options[$key]);
     120        }
    146121    }
    147122
    148123    /**#@+
    149      * @see \WpOrg\Requests\Session::request()
     124     * @see request()
    150125     * @param string $url
    151126     * @param array $headers
    152127     * @param array $options
    153      * @return \WpOrg\Requests\Response
     128     * @return Requests_Response
    154129     */
    155130    /**
    156131     * Send a GET request
    157132     */
    158     public function get($url, $headers = [], $options = []) {
     133    public function get($url, $headers = array(), $options = array()) {
    159134        return $this->request($url, $headers, null, Requests::GET, $options);
    160135    }
     
    163138     * Send a HEAD request
    164139     */
    165     public function head($url, $headers = [], $options = []) {
     140    public function head($url, $headers = array(), $options = array()) {
    166141        return $this->request($url, $headers, null, Requests::HEAD, $options);
    167142    }
     
    170145     * Send a DELETE request
    171146     */
    172     public function delete($url, $headers = [], $options = []) {
     147    public function delete($url, $headers = array(), $options = array()) {
    173148        return $this->request($url, $headers, null, Requests::DELETE, $options);
    174149    }
     
    176151
    177152    /**#@+
    178      * @see \WpOrg\Requests\Session::request()
     153     * @see request()
    179154     * @param string $url
    180155     * @param array $headers
    181156     * @param array $data
    182157     * @param array $options
    183      * @return \WpOrg\Requests\Response
     158     * @return Requests_Response
    184159     */
    185160    /**
    186161     * Send a POST request
    187162     */
    188     public function post($url, $headers = [], $data = [], $options = []) {
     163    public function post($url, $headers = array(), $data = array(), $options = array()) {
    189164        return $this->request($url, $headers, $data, Requests::POST, $options);
    190165    }
     
    193168     * Send a PUT request
    194169     */
    195     public function put($url, $headers = [], $data = [], $options = []) {
     170    public function put($url, $headers = array(), $data = array(), $options = array()) {
    196171        return $this->request($url, $headers, $data, Requests::PUT, $options);
    197172    }
     
    200175     * Send a PATCH request
    201176     *
    202      * Note: Unlike {@see \WpOrg\Requests\Session::post()} and {@see \WpOrg\Requests\Session::put()},
    203      * `$headers` is required, as the specification recommends that should send an ETag
     177     * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
     178     * specification recommends that should send an ETag
    204179     *
    205180     * @link https://tools.ietf.org/html/rfc5789
    206181     */
    207     public function patch($url, $headers, $data = [], $options = []) {
     182    public function patch($url, $headers, $data = array(), $options = array()) {
    208183        return $this->request($url, $headers, $data, Requests::PATCH, $options);
    209184    }
     
    216191     * parsing.
    217192     *
    218      * @see \WpOrg\Requests\Requests::request()
     193     * @see Requests::request()
     194     *
     195     * @throws Requests_Exception On invalid URLs (`nonhttp`)
    219196     *
    220197     * @param string $url URL to request
    221198     * @param array $headers Extra headers to send with the request
    222199     * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
    223      * @param string $type HTTP request type (use \WpOrg\Requests\Requests constants)
    224      * @param array $options Options for the request (see {@see \WpOrg\Requests\Requests::request()})
    225      * @return \WpOrg\Requests\Response
    226      *
    227      * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`)
    228      */
    229     public function request($url, $headers = [], $data = [], $type = Requests::GET, $options = []) {
     200     * @param string $type HTTP request type (use Requests constants)
     201     * @param array $options Options for the request (see {@see Requests::request})
     202     * @return Requests_Response
     203     */
     204    public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) {
    230205        $request = $this->merge_request(compact('url', 'headers', 'data', 'options'));
    231206
     
    236211     * Send multiple HTTP requests simultaneously
    237212     *
    238      * @see \WpOrg\Requests\Requests::request_multiple()
    239      *
    240      * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple()})
    241      * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request()})
    242      * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object)
    243      *
    244      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access.
    245      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
    246      */
    247     public function request_multiple($requests, $options = []) {
    248         if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) {
    249             throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
    250         }
    251 
    252         if (is_array($options) === false) {
    253             throw InvalidArgument::create(2, '$options', 'array', gettype($options));
    254         }
    255 
     213     * @see Requests::request_multiple()
     214     *
     215     * @param array $requests Requests data (see {@see Requests::request_multiple})
     216     * @param array $options Global and default options (see {@see Requests::request})
     217     * @return array Responses (either Requests_Response or a Requests_Exception object)
     218     */
     219    public function request_multiple($requests, $options = array()) {
    256220        foreach ($requests as $key => $request) {
    257221            $requests[$key] = $this->merge_request($request, false);
     
    269233     * Merge a request's data with the default data
    270234     *
    271      * @param array $request Request data (same form as {@see \WpOrg\Requests\Session::request_multiple()})
     235     * @param array $request Request data (same form as {@see request_multiple})
    272236     * @param boolean $merge_options Should we merge options as well?
    273237     * @return array Request data
     
    275239    protected function merge_request($request, $merge_options = true) {
    276240        if ($this->url !== null) {
    277             $request['url'] = Iri::absolutize($this->url, $request['url']);
     241            $request['url'] = Requests_IRI::absolutize($this->url, $request['url']);
    278242            $request['url'] = $request['url']->uri;
    279243        }
    280244
    281245        if (empty($request['headers'])) {
    282             $request['headers'] = [];
     246            $request['headers'] = array();
    283247        }
    284248        $request['headers'] = array_merge($this->headers, $request['headers']);
     
    293257        }
    294258
    295         if ($merge_options === true) {
     259        if ($merge_options !== false) {
    296260            $request['options'] = array_merge($this->options, $request['options']);
    297261
  • trunk/src/wp-includes/Requests/Transport.php

    r52244 r52328  
    33 * Base HTTP transport
    44 *
    5  * @package Requests\Transport
     5 * @package Requests
     6 * @subpackage Transport
    67 */
    7 
    8 namespace WpOrg\Requests;
    98
    109/**
    1110 * Base HTTP transport
    1211 *
    13  * @package Requests\Transport
     12 * @package Requests
     13 * @subpackage Transport
    1414 */
    15 interface Transport {
     15interface Requests_Transport {
    1616    /**
    1717     * Perform a request
     
    2020     * @param array $headers Associative array of request headers
    2121     * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
    22      * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation
     22     * @param array $options Request options, see {@see Requests::response()} for documentation
    2323     * @return string Raw HTTP result
    2424     */
    25     public function request($url, $headers = [], $data = [], $options = []);
     25    public function request($url, $headers = array(), $data = array(), $options = array());
    2626
    2727    /**
    2828     * Send multiple requests simultaneously
    2929     *
    30      * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request()}
    31      * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation
    32      * @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well)
     30     * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see Requests_Transport::request}
     31     * @param array $options Global options, see {@see Requests::response()} for documentation
     32     * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
    3333     */
    3434    public function request_multiple($requests, $options);
    3535
    3636    /**
    37      * Self-test whether the transport can be used.
    38      *
    39      * The available capabilities to test for can be found in {@see \WpOrg\Requests\Capability}.
    40      *
    41      * @param array<string, bool> $capabilities Optional. Associative array of capabilities to test against, i.e. `['<capability>' => true]`.
    42      * @return bool Whether the transport can be used.
     37     * Self-test whether the transport can be used
     38     * @return bool
    4339     */
    44     public static function test($capabilities = []);
     40    public static function test();
    4541}
  • trunk/src/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php

    r52244 r52328  
    33 * Case-insensitive dictionary, suitable for HTTP headers
    44 *
    5  * @package Requests\Utilities
     5 * @package Requests
     6 * @subpackage Utilities
    67 */
    7 
    8 namespace WpOrg\Requests\Utility;
    9 
    10 use ArrayAccess;
    11 use ArrayIterator;
    12 use IteratorAggregate;
    13 use ReturnTypeWillChange;
    14 use WpOrg\Requests\Exception;
    158
    169/**
    1710 * Case-insensitive dictionary, suitable for HTTP headers
    1811 *
    19  * @package Requests\Utilities
     12 * @package Requests
     13 * @subpackage Utilities
    2014 */
    21 class CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
     15class Requests_Utility_CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
    2216    /**
    2317     * Actual item data
     
    2519     * @var array
    2620     */
    27     protected $data = [];
     21    protected $data = array();
    2822
    2923    /**
     
    3226     * @param array $data Dictionary/map to convert to case-insensitive
    3327     */
    34     public function __construct(array $data = []) {
    35         foreach ($data as $offset => $value) {
    36             $this->offsetSet($offset, $value);
     28    public function __construct(array $data = array()) {
     29        foreach ($data as $key => $value) {
     30            $this->offsetSet($key, $value);
    3731        }
    3832    }
     
    4135     * Check if the given item exists
    4236     *
    43      * @param string $offset Item key
     37     * @param string $key Item key
    4438     * @return boolean Does the item exist?
    4539     */
    46     #[ReturnTypeWillChange]
    47     public function offsetExists($offset) {
    48         if (is_string($offset)) {
    49             $offset = strtolower($offset);
    50         }
    51 
    52         return isset($this->data[$offset]);
     40    public function offsetExists($key) {
     41        $key = strtolower($key);
     42        return isset($this->data[$key]);
    5343    }
    5444
     
    5646     * Get the value for the item
    5747     *
    58      * @param string $offset Item key
    59      * @return string|null Item value (null if the item key doesn't exist)
     48     * @param string $key Item key
     49     * @return string|null Item value (null if offsetExists is false)
    6050     */
    61     #[ReturnTypeWillChange]
    62     public function offsetGet($offset) {
    63         if (is_string($offset)) {
    64             $offset = strtolower($offset);
    65         }
    66 
    67         if (!isset($this->data[$offset])) {
     51    public function offsetGet($key) {
     52        $key = strtolower($key);
     53        if (!isset($this->data[$key])) {
    6854            return null;
    6955        }
    7056
    71         return $this->data[$offset];
     57        return $this->data[$key];
    7258    }
    7359
     
    7561     * Set the given item
    7662     *
    77      * @param string $offset Item name
     63     * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
     64     *
     65     * @param string $key Item name
    7866     * @param string $value Item value
    79      *
    80      * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
    8167     */
    82     #[ReturnTypeWillChange]
    83     public function offsetSet($offset, $value) {
    84         if ($offset === null) {
    85             throw new Exception('Object is a dictionary, not a list', 'invalidset');
     68    public function offsetSet($key, $value) {
     69        if ($key === null) {
     70            throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
    8671        }
    8772
    88         if (is_string($offset)) {
    89             $offset = strtolower($offset);
    90         }
    91 
    92         $this->data[$offset] = $value;
     73        $key              = strtolower($key);
     74        $this->data[$key] = $value;
    9375    }
    9476
     
    9678     * Unset the given header
    9779     *
    98      * @param string $offset
     80     * @param string $key
    9981     */
    100     #[ReturnTypeWillChange]
    101     public function offsetUnset($offset) {
    102         if (is_string($offset)) {
    103             $offset = strtolower($offset);
    104         }
    105 
    106         unset($this->data[$offset]);
     82    public function offsetUnset($key) {
     83        unset($this->data[strtolower($key)]);
    10784    }
    10885
     
    11087     * Get an iterator for the data
    11188     *
    112      * @return \ArrayIterator
     89     * @return ArrayIterator
    11390     */
    114     #[ReturnTypeWillChange]
    11591    public function getIterator() {
    11692        return new ArrayIterator($this->data);
  • trunk/src/wp-includes/Requests/Utility/FilteredIterator.php

    r52244 r52328  
    33 * Iterator for arrays requiring filtered values
    44 *
    5  * @package Requests\Utilities
     5 * @package Requests
     6 * @subpackage Utilities
    67 */
    7 
    8 namespace WpOrg\Requests\Utility;
    9 
    10 use ArrayIterator;
    11 use ReturnTypeWillChange;
    12 use WpOrg\Requests\Exception\InvalidArgument;
    13 use WpOrg\Requests\Utility\InputValidator;
    148
    159/**
    1610 * Iterator for arrays requiring filtered values
    1711 *
    18  * @package Requests\Utilities
     12 * @package Requests
     13 * @subpackage Utilities
    1914 */
    20 final class FilteredIterator extends ArrayIterator {
     15class Requests_Utility_FilteredIterator extends ArrayIterator {
    2116    /**
    2217     * Callback to run as a filter
     
    2419     * @var callable
    2520     */
    26     private $callback;
     21    protected $callback;
    2722
    2823    /**
     
    3126     * @param array $data
    3227     * @param callable $callback Callback to be called on each value
    33      *
    34      * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data argument is not iterable.
    3528     */
    3629    public function __construct($data, $callback) {
    37         if (InputValidator::is_iterable($data) === false) {
    38             throw InvalidArgument::create(1, '$data', 'iterable', gettype($data));
    39         }
    40 
    4130        parent::__construct($data);
    4231
    43         if (is_callable($callback)) {
    44             $this->callback = $callback;
    45         }
    46     }
    47 
    48     /**
    49      * @inheritdoc
    50      *
    51      * @phpcs:disable PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    52      */
    53     #[ReturnTypeWillChange]
    54     public function __unserialize($data) {}
    55     // phpcs:enable
    56 
    57     public function __wakeup() {
    58         unset($this->callback);
     32        $this->callback = $callback;
    5933    }
    6034
     
    6438     * @return string
    6539     */
    66     #[ReturnTypeWillChange]
    6740    public function current() {
    6841        $value = parent::current();
     
    7851     * @inheritdoc
    7952     */
    80     #[ReturnTypeWillChange]
    81     public function unserialize($data) {}
     53    public function unserialize($serialized) {}
     54
     55    /**
     56     * @inheritdoc
     57     *
     58     * @phpcs:disable PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
     59     */
     60    public function __unserialize($serialized) {}
     61
     62    public function __wakeup() {
     63        unset($this->callback);
     64    }
    8265}
  • trunk/src/wp-includes/class-requests.php

    r52244 r52328  
    88 *
    99 * @package Requests
    10  *
    11  * @deprecated 5.9.0
    1210 */
    13 
    14 /*
    15  * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations
    16  * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`.
    17  * The constant needs to be defined before this class is required.
    18  */
    19 if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) {
    20     // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
    21     trigger_error(
    22         'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
    23         . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
    24         E_USER_DEPRECATED
    25     );
    26 
    27     // Prevent the deprecation notice from being thrown twice.
    28     if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
    29         define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
    30     }
    31 }
    32 
    33 require_once __DIR__ . '/Requests/Requests.php';
    3411
    3512/**
     
    4118 *
    4219 * @package Requests
    43  *
    44  * @deprecated 5.9.0 Use `WpOrg\Requests\Requests` instead for the actual functionality and
    45  *                   use `WpOrg\Requests\Autoload` for the autoloading.
    4620 */
    47 class Requests extends WpOrg\Requests\Requests {
    48 
    49     /**
    50      * Deprecated autoloader for Requests.
    51      *
    52      * @deprecated 5.9.0 Use the `WpOrg\Requests\Autoload::load()` method instead.
     21class Requests {
     22    /**
     23     * POST method
     24     *
     25     * @var string
     26     */
     27    const POST = 'POST';
     28
     29    /**
     30     * PUT method
     31     *
     32     * @var string
     33     */
     34    const PUT = 'PUT';
     35
     36    /**
     37     * GET method
     38     *
     39     * @var string
     40     */
     41    const GET = 'GET';
     42
     43    /**
     44     * HEAD method
     45     *
     46     * @var string
     47     */
     48    const HEAD = 'HEAD';
     49
     50    /**
     51     * DELETE method
     52     *
     53     * @var string
     54     */
     55    const DELETE = 'DELETE';
     56
     57    /**
     58     * OPTIONS method
     59     *
     60     * @var string
     61     */
     62    const OPTIONS = 'OPTIONS';
     63
     64    /**
     65     * TRACE method
     66     *
     67     * @var string
     68     */
     69    const TRACE = 'TRACE';
     70
     71    /**
     72     * PATCH method
     73     *
     74     * @link https://tools.ietf.org/html/rfc5789
     75     * @var string
     76     */
     77    const PATCH = 'PATCH';
     78
     79    /**
     80     * Default size of buffer size to read streams
     81     *
     82     * @var integer
     83     */
     84    const BUFFER_SIZE = 1160;
     85
     86    /**
     87     * Current version of Requests
     88     *
     89     * @var string
     90     */
     91    const VERSION = '1.8.1';
     92
     93    /**
     94     * Registered transport classes
     95     *
     96     * @var array
     97     */
     98    protected static $transports = array();
     99
     100    /**
     101     * Selected transport name
     102     *
     103     * Use {@see get_transport()} instead
     104     *
     105     * @var array
     106     */
     107    public static $transport = array();
     108
     109    /**
     110     * Default certificate path.
     111     *
     112     * @see Requests::get_certificate_path()
     113     * @see Requests::set_certificate_path()
     114     *
     115     * @var string
     116     */
     117    protected static $certificate_path;
     118
     119    /**
     120     * This is a static class, do not instantiate it
    53121     *
    54122     * @codeCoverageIgnore
     123     */
     124    private function __construct() {}
     125
     126    /**
     127     * Autoloader for Requests
     128     *
     129     * Register this with {@see register_autoloader()} if you'd like to avoid
     130     * having to create your own.
     131     *
     132     * (You can also use `spl_autoload_register` directly if you'd prefer.)
     133     *
     134     * @codeCoverageIgnore
    55135     *
    56136     * @param string $class Class name to load
    57137     */
    58138    public static function autoloader($class) {
    59         if (class_exists('WpOrg\Requests\Autoload') === false) {
    60             require_once __DIR__ . '/Requests/Autoload.php';
    61         }
    62 
    63         return WpOrg\Requests\Autoload::load($class);
     139        // Check that the class starts with "Requests"
     140        if (strpos($class, 'Requests') !== 0) {
     141            return;
     142        }
     143
     144        $file = str_replace('_', '/', $class);
     145        if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
     146            require_once dirname(__FILE__) . '/' . $file . '.php';
     147        }
    64148    }
    65149
     
    67151     * Register the built-in autoloader
    68152     *
    69      * @deprecated 5.9.0 Include the `WpOrg\Requests\Autoload` class and
    70      *                   call `WpOrg\Requests\Autoload::register()` instead.
    71      *
    72153     * @codeCoverageIgnore
    73154     */
    74155    public static function register_autoloader() {
    75         require_once __DIR__ . '/Requests/Autoload.php';
    76         WpOrg\Requests\Autoload::register();
     156        spl_autoload_register(array('Requests', 'autoloader'));
     157    }
     158
     159    /**
     160     * Register a transport
     161     *
     162     * @param string $transport Transport class to add, must support the Requests_Transport interface
     163     */
     164    public static function add_transport($transport) {
     165        if (empty(self::$transports)) {
     166            self::$transports = array(
     167                'Requests_Transport_cURL',
     168                'Requests_Transport_fsockopen',
     169            );
     170        }
     171
     172        self::$transports = array_merge(self::$transports, array($transport));
     173    }
     174
     175    /**
     176     * Get a working transport
     177     *
     178     * @throws Requests_Exception If no valid transport is found (`notransport`)
     179     * @return Requests_Transport
     180     */
     181    protected static function get_transport($capabilities = array()) {
     182        // Caching code, don't bother testing coverage
     183        // @codeCoverageIgnoreStart
     184        // array of capabilities as a string to be used as an array key
     185        ksort($capabilities);
     186        $cap_string = serialize($capabilities);
     187
     188        // Don't search for a transport if it's already been done for these $capabilities
     189        if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) {
     190            $class = self::$transport[$cap_string];
     191            return new $class();
     192        }
     193        // @codeCoverageIgnoreEnd
     194
     195        if (empty(self::$transports)) {
     196            self::$transports = array(
     197                'Requests_Transport_cURL',
     198                'Requests_Transport_fsockopen',
     199            );
     200        }
     201
     202        // Find us a working transport
     203        foreach (self::$transports as $class) {
     204            if (!class_exists($class)) {
     205                continue;
     206            }
     207
     208            $result = call_user_func(array($class, 'test'), $capabilities);
     209            if ($result) {
     210                self::$transport[$cap_string] = $class;
     211                break;
     212            }
     213        }
     214        if (self::$transport[$cap_string] === null) {
     215            throw new Requests_Exception('No working transports found', 'notransport', self::$transports);
     216        }
     217
     218        $class = self::$transport[$cap_string];
     219        return new $class();
     220    }
     221
     222    /**#@+
     223     * @see request()
     224     * @param string $url
     225     * @param array $headers
     226     * @param array $options
     227     * @return Requests_Response
     228     */
     229    /**
     230     * Send a GET request
     231     */
     232    public static function get($url, $headers = array(), $options = array()) {
     233        return self::request($url, $headers, null, self::GET, $options);
     234    }
     235
     236    /**
     237     * Send a HEAD request
     238     */
     239    public static function head($url, $headers = array(), $options = array()) {
     240        return self::request($url, $headers, null, self::HEAD, $options);
     241    }
     242
     243    /**
     244     * Send a DELETE request
     245     */
     246    public static function delete($url, $headers = array(), $options = array()) {
     247        return self::request($url, $headers, null, self::DELETE, $options);
     248    }
     249
     250    /**
     251     * Send a TRACE request
     252     */
     253    public static function trace($url, $headers = array(), $options = array()) {
     254        return self::request($url, $headers, null, self::TRACE, $options);
     255    }
     256    /**#@-*/
     257
     258    /**#@+
     259     * @see request()
     260     * @param string $url
     261     * @param array $headers
     262     * @param array $data
     263     * @param array $options
     264     * @return Requests_Response
     265     */
     266    /**
     267     * Send a POST request
     268     */
     269    public static function post($url, $headers = array(), $data = array(), $options = array()) {
     270        return self::request($url, $headers, $data, self::POST, $options);
     271    }
     272    /**
     273     * Send a PUT request
     274     */
     275    public static function put($url, $headers = array(), $data = array(), $options = array()) {
     276        return self::request($url, $headers, $data, self::PUT, $options);
     277    }
     278
     279    /**
     280     * Send an OPTIONS request
     281     */
     282    public static function options($url, $headers = array(), $data = array(), $options = array()) {
     283        return self::request($url, $headers, $data, self::OPTIONS, $options);
     284    }
     285
     286    /**
     287     * Send a PATCH request
     288     *
     289     * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
     290     * specification recommends that should send an ETag
     291     *
     292     * @link https://tools.ietf.org/html/rfc5789
     293     */
     294    public static function patch($url, $headers, $data = array(), $options = array()) {
     295        return self::request($url, $headers, $data, self::PATCH, $options);
     296    }
     297    /**#@-*/
     298
     299    /**
     300     * Main interface for HTTP requests
     301     *
     302     * This method initiates a request and sends it via a transport before
     303     * parsing.
     304     *
     305     * The `$options` parameter takes an associative array with the following
     306     * options:
     307     *
     308     * - `timeout`: How long should we wait for a response?
     309     *    Note: for cURL, a minimum of 1 second applies, as DNS resolution
     310     *    operates at second-resolution only.
     311     *    (float, seconds with a millisecond precision, default: 10, example: 0.01)
     312     * - `connect_timeout`: How long should we wait while trying to connect?
     313     *    (float, seconds with a millisecond precision, default: 10, example: 0.01)
     314     * - `useragent`: Useragent to send to the server
     315     *    (string, default: php-requests/$version)
     316     * - `follow_redirects`: Should we follow 3xx redirects?
     317     *    (boolean, default: true)
     318     * - `redirects`: How many times should we redirect before erroring?
     319     *    (integer, default: 10)
     320     * - `blocking`: Should we block processing on this request?
     321     *    (boolean, default: true)
     322     * - `filename`: File to stream the body to instead.
     323     *    (string|boolean, default: false)
     324     * - `auth`: Authentication handler or array of user/password details to use
     325     *    for Basic authentication
     326     *    (Requests_Auth|array|boolean, default: false)
     327     * - `proxy`: Proxy details to use for proxy by-passing and authentication
     328     *    (Requests_Proxy|array|string|boolean, default: false)
     329     * - `max_bytes`: Limit for the response body size.
     330     *    (integer|boolean, default: false)
     331     * - `idn`: Enable IDN parsing
     332     *    (boolean, default: true)
     333     * - `transport`: Custom transport. Either a class name, or a
     334     *    transport object. Defaults to the first working transport from
     335     *    {@see getTransport()}
     336     *    (string|Requests_Transport, default: {@see getTransport()})
     337     * - `hooks`: Hooks handler.
     338     *    (Requests_Hooker, default: new Requests_Hooks())
     339     * - `verify`: Should we verify SSL certificates? Allows passing in a custom
     340     *    certificate file as a string. (Using true uses the system-wide root
     341     *    certificate store instead, but this may have different behaviour
     342     *    across transports.)
     343     *    (string|boolean, default: library/Requests/Transport/cacert.pem)
     344     * - `verifyname`: Should we verify the common name in the SSL certificate?
     345     *    (boolean, default: true)
     346     * - `data_format`: How should we send the `$data` parameter?
     347     *    (string, one of 'query' or 'body', default: 'query' for
     348     *    HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH)
     349     *
     350     * @throws Requests_Exception On invalid URLs (`nonhttp`)
     351     *
     352     * @param string $url URL to request
     353     * @param array $headers Extra headers to send with the request
     354     * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
     355     * @param string $type HTTP request type (use Requests constants)
     356     * @param array $options Options for the request (see description for more information)
     357     * @return Requests_Response
     358     */
     359    public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) {
     360        if (empty($options['type'])) {
     361            $options['type'] = $type;
     362        }
     363        $options = array_merge(self::get_default_options(), $options);
     364
     365        self::set_defaults($url, $headers, $data, $type, $options);
     366
     367        $options['hooks']->dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options));
     368
     369        if (!empty($options['transport'])) {
     370            $transport = $options['transport'];
     371
     372            if (is_string($options['transport'])) {
     373                $transport = new $transport();
     374            }
     375        }
     376        else {
     377            $need_ssl     = (stripos($url, 'https://') === 0);
     378            $capabilities = array('ssl' => $need_ssl);
     379            $transport    = self::get_transport($capabilities);
     380        }
     381        $response = $transport->request($url, $headers, $data, $options);
     382
     383        $options['hooks']->dispatch('requests.before_parse', array(&$response, $url, $headers, $data, $type, $options));
     384
     385        return self::parse_response($response, $url, $headers, $data, $options);
     386    }
     387
     388    /**
     389     * Send multiple HTTP requests simultaneously
     390     *
     391     * The `$requests` parameter takes an associative or indexed array of
     392     * request fields. The key of each request can be used to match up the
     393     * request with the returned data, or with the request passed into your
     394     * `multiple.request.complete` callback.
     395     *
     396     * The request fields value is an associative array with the following keys:
     397     *
     398     * - `url`: Request URL Same as the `$url` parameter to
     399     *    {@see Requests::request}
     400     *    (string, required)
     401     * - `headers`: Associative array of header fields. Same as the `$headers`
     402     *    parameter to {@see Requests::request}
     403     *    (array, default: `array()`)
     404     * - `data`: Associative array of data fields or a string. Same as the
     405     *    `$data` parameter to {@see Requests::request}
     406     *    (array|string, default: `array()`)
     407     * - `type`: HTTP request type (use Requests constants). Same as the `$type`
     408     *    parameter to {@see Requests::request}
     409     *    (string, default: `Requests::GET`)
     410     * - `cookies`: Associative array of cookie name to value, or cookie jar.
     411     *    (array|Requests_Cookie_Jar)
     412     *
     413     * If the `$options` parameter is specified, individual requests will
     414     * inherit options from it. This can be used to use a single hooking system,
     415     * or set all the types to `Requests::POST`, for example.
     416     *
     417     * In addition, the `$options` parameter takes the following global options:
     418     *
     419     * - `complete`: A callback for when a request is complete. Takes two
     420     *    parameters, a Requests_Response/Requests_Exception reference, and the
     421     *    ID from the request array (Note: this can also be overridden on a
     422     *    per-request basis, although that's a little silly)
     423     *    (callback)
     424     *
     425     * @param array $requests Requests data (see description for more information)
     426     * @param array $options Global and default options (see {@see Requests::request})
     427     * @return array Responses (either Requests_Response or a Requests_Exception object)
     428     */
     429    public static function request_multiple($requests, $options = array()) {
     430        $options = array_merge(self::get_default_options(true), $options);
     431
     432        if (!empty($options['hooks'])) {
     433            $options['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
     434            if (!empty($options['complete'])) {
     435                $options['hooks']->register('multiple.request.complete', $options['complete']);
     436            }
     437        }
     438
     439        foreach ($requests as $id => &$request) {
     440            if (!isset($request['headers'])) {
     441                $request['headers'] = array();
     442            }
     443            if (!isset($request['data'])) {
     444                $request['data'] = array();
     445            }
     446            if (!isset($request['type'])) {
     447                $request['type'] = self::GET;
     448            }
     449            if (!isset($request['options'])) {
     450                $request['options']         = $options;
     451                $request['options']['type'] = $request['type'];
     452            }
     453            else {
     454                if (empty($request['options']['type'])) {
     455                    $request['options']['type'] = $request['type'];
     456                }
     457                $request['options'] = array_merge($options, $request['options']);
     458            }
     459
     460            self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']);
     461
     462            // Ensure we only hook in once
     463            if ($request['options']['hooks'] !== $options['hooks']) {
     464                $request['options']['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
     465                if (!empty($request['options']['complete'])) {
     466                    $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']);
     467                }
     468            }
     469        }
     470        unset($request);
     471
     472        if (!empty($options['transport'])) {
     473            $transport = $options['transport'];
     474
     475            if (is_string($options['transport'])) {
     476                $transport = new $transport();
     477            }
     478        }
     479        else {
     480            $transport = self::get_transport();
     481        }
     482        $responses = $transport->request_multiple($requests, $options);
     483
     484        foreach ($responses as $id => &$response) {
     485            // If our hook got messed with somehow, ensure we end up with the
     486            // correct response
     487            if (is_string($response)) {
     488                $request = $requests[$id];
     489                self::parse_multiple($response, $request);
     490                $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id));
     491            }
     492        }
     493
     494        return $responses;
     495    }
     496
     497    /**
     498     * Get the default options
     499     *
     500     * @see Requests::request() for values returned by this method
     501     * @param boolean $multirequest Is this a multirequest?
     502     * @return array Default option values
     503     */
     504    protected static function get_default_options($multirequest = false) {
     505        $defaults = array(
     506            'timeout'          => 10,
     507            'connect_timeout'  => 10,
     508            'useragent'        => 'php-requests/' . self::VERSION,
     509            'protocol_version' => 1.1,
     510            'redirected'       => 0,
     511            'redirects'        => 10,
     512            'follow_redirects' => true,
     513            'blocking'         => true,
     514            'type'             => self::GET,
     515            'filename'         => false,
     516            'auth'             => false,
     517            'proxy'            => false,
     518            'cookies'          => false,
     519            'max_bytes'        => false,
     520            'idn'              => true,
     521            'hooks'            => null,
     522            'transport'        => null,
     523            'verify'           => self::get_certificate_path(),
     524            'verifyname'       => true,
     525        );
     526        if ($multirequest !== false) {
     527            $defaults['complete'] = null;
     528        }
     529        return $defaults;
     530    }
     531
     532    /**
     533     * Get default certificate path.
     534     *
     535     * @return string Default certificate path.
     536     */
     537    public static function get_certificate_path() {
     538        if (!empty(self::$certificate_path)) {
     539            return self::$certificate_path;
     540        }
     541
     542        return dirname(__FILE__) . '/Requests/Transport/cacert.pem';
     543    }
     544
     545    /**
     546     * Set default certificate path.
     547     *
     548     * @param string $path Certificate path, pointing to a PEM file.
     549     */
     550    public static function set_certificate_path($path) {
     551        self::$certificate_path = $path;
     552    }
     553
     554    /**
     555     * Set the default values
     556     *
     557     * @param string $url URL to request
     558     * @param array $headers Extra headers to send with the request
     559     * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
     560     * @param string $type HTTP request type
     561     * @param array $options Options for the request
     562     * @return array $options
     563     */
     564    protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) {
     565        if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) {
     566            throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url);
     567        }
     568
     569        if (empty($options['hooks'])) {
     570            $options['hooks'] = new Requests_Hooks();
     571        }
     572
     573        if (is_array($options['auth'])) {
     574            $options['auth'] = new Requests_Auth_Basic($options['auth']);
     575        }
     576        if ($options['auth'] !== false) {
     577            $options['auth']->register($options['hooks']);
     578        }
     579
     580        if (is_string($options['proxy']) || is_array($options['proxy'])) {
     581            $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']);
     582        }
     583        if ($options['proxy'] !== false) {
     584            $options['proxy']->register($options['hooks']);
     585        }
     586
     587        if (is_array($options['cookies'])) {
     588            $options['cookies'] = new Requests_Cookie_Jar($options['cookies']);
     589        }
     590        elseif (empty($options['cookies'])) {
     591            $options['cookies'] = new Requests_Cookie_Jar();
     592        }
     593        if ($options['cookies'] !== false) {
     594            $options['cookies']->register($options['hooks']);
     595        }
     596
     597        if ($options['idn'] !== false) {
     598            $iri       = new Requests_IRI($url);
     599            $iri->host = Requests_IDNAEncoder::encode($iri->ihost);
     600            $url       = $iri->uri;
     601        }
     602
     603        // Massage the type to ensure we support it.
     604        $type = strtoupper($type);
     605
     606        if (!isset($options['data_format'])) {
     607            if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) {
     608                $options['data_format'] = 'query';
     609            }
     610            else {
     611                $options['data_format'] = 'body';
     612            }
     613        }
     614    }
     615
     616    /**
     617     * HTTP response parser
     618     *
     619     * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`)
     620     * @throws Requests_Exception On missing head/body separator (`noversion`)
     621     * @throws Requests_Exception On missing head/body separator (`toomanyredirects`)
     622     *
     623     * @param string $headers Full response text including headers and body
     624     * @param string $url Original request URL
     625     * @param array $req_headers Original $headers array passed to {@link request()}, in case we need to follow redirects
     626     * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects
     627     * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects
     628     * @return Requests_Response
     629     */
     630    protected static function parse_response($headers, $url, $req_headers, $req_data, $options) {
     631        $return = new Requests_Response();
     632        if (!$options['blocking']) {
     633            return $return;
     634        }
     635
     636        $return->raw  = $headers;
     637        $return->url  = (string) $url;
     638        $return->body = '';
     639
     640        if (!$options['filename']) {
     641            $pos = strpos($headers, "\r\n\r\n");
     642            if ($pos === false) {
     643                // Crap!
     644                throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');
     645            }
     646
     647            $headers = substr($return->raw, 0, $pos);
     648            // Headers will always be separated from the body by two new lines - `\n\r\n\r`.
     649            $body = substr($return->raw, $pos + 4);
     650            if (!empty($body)) {
     651                $return->body = $body;
     652            }
     653        }
     654        // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3)
     655        $headers = str_replace("\r\n", "\n", $headers);
     656        // Unfold headers (replace [CRLF] 1*( SP | HT ) with SP) as per RFC 2616 (section 2.2)
     657        $headers = preg_replace('/\n[ \t]/', ' ', $headers);
     658        $headers = explode("\n", $headers);
     659        preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches);
     660        if (empty($matches)) {
     661            throw new Requests_Exception('Response could not be parsed', 'noversion', $headers);
     662        }
     663        $return->protocol_version = (float) $matches[1];
     664        $return->status_code      = (int) $matches[2];
     665        if ($return->status_code >= 200 && $return->status_code < 300) {
     666            $return->success = true;
     667        }
     668
     669        foreach ($headers as $header) {
     670            list($key, $value) = explode(':', $header, 2);
     671            $value             = trim($value);
     672            preg_replace('#(\s+)#i', ' ', $value);
     673            $return->headers[$key] = $value;
     674        }
     675        if (isset($return->headers['transfer-encoding'])) {
     676            $return->body = self::decode_chunked($return->body);
     677            unset($return->headers['transfer-encoding']);
     678        }
     679        if (isset($return->headers['content-encoding'])) {
     680            $return->body = self::decompress($return->body);
     681        }
     682
     683        //fsockopen and cURL compatibility
     684        if (isset($return->headers['connection'])) {
     685            unset($return->headers['connection']);
     686        }
     687
     688        $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options));
     689
     690        if ($return->is_redirect() && $options['follow_redirects'] === true) {
     691            if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) {
     692                if ($return->status_code === 303) {
     693                    $options['type'] = self::GET;
     694                }
     695                $options['redirected']++;
     696                $location = $return->headers['location'];
     697                if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) {
     698                    // relative redirect, for compatibility make it absolute
     699                    $location = Requests_IRI::absolutize($url, $location);
     700                    $location = $location->uri;
     701                }
     702
     703                $hook_args = array(
     704                    &$location,
     705                    &$req_headers,
     706                    &$req_data,
     707                    &$options,
     708                    $return,
     709                );
     710                $options['hooks']->dispatch('requests.before_redirect', $hook_args);
     711                $redirected            = self::request($location, $req_headers, $req_data, $options['type'], $options);
     712                $redirected->history[] = $return;
     713                return $redirected;
     714            }
     715            elseif ($options['redirected'] >= $options['redirects']) {
     716                throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return);
     717            }
     718        }
     719
     720        $return->redirects = $options['redirected'];
     721
     722        $options['hooks']->dispatch('requests.after_request', array(&$return, $req_headers, $req_data, $options));
     723        return $return;
     724    }
     725
     726    /**
     727     * Callback for `transport.internal.parse_response`
     728     *
     729     * Internal use only. Converts a raw HTTP response to a Requests_Response
     730     * while still executing a multiple request.
     731     *
     732     * @param string $response Full response text including headers and body (will be overwritten with Response instance)
     733     * @param array $request Request data as passed into {@see Requests::request_multiple()}
     734     * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object
     735     */
     736    public static function parse_multiple(&$response, $request) {
     737        try {
     738            $url      = $request['url'];
     739            $headers  = $request['headers'];
     740            $data     = $request['data'];
     741            $options  = $request['options'];
     742            $response = self::parse_response($response, $url, $headers, $data, $options);
     743        }
     744        catch (Requests_Exception $e) {
     745            $response = $e;
     746        }
     747    }
     748
     749    /**
     750     * Decoded a chunked body as per RFC 2616
     751     *
     752     * @see https://tools.ietf.org/html/rfc2616#section-3.6.1
     753     * @param string $data Chunked body
     754     * @return string Decoded body
     755     */
     756    protected static function decode_chunked($data) {
     757        if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) {
     758            return $data;
     759        }
     760
     761        $decoded = '';
     762        $encoded = $data;
     763
     764        while (true) {
     765            $is_chunked = (bool) preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', $encoded, $matches);
     766            if (!$is_chunked) {
     767                // Looks like it's not chunked after all
     768                return $data;
     769            }
     770
     771            $length = hexdec(trim($matches[1]));
     772            if ($length === 0) {
     773                // Ignore trailer headers
     774                return $decoded;
     775            }
     776
     777            $chunk_length = strlen($matches[0]);
     778            $decoded     .= substr($encoded, $chunk_length, $length);
     779            $encoded      = substr($encoded, $chunk_length + $length + 2);
     780
     781            if (trim($encoded) === '0' || empty($encoded)) {
     782                return $decoded;
     783            }
     784        }
     785
     786        // We'll never actually get down here
     787        // @codeCoverageIgnoreStart
     788    }
     789    // @codeCoverageIgnoreEnd
     790
     791    /**
     792     * Convert a key => value array to a 'key: value' array for headers
     793     *
     794     * @param array $array Dictionary of header values
     795     * @return array List of headers
     796     */
     797    public static function flatten($array) {
     798        $return = array();
     799        foreach ($array as $key => $value) {
     800            $return[] = sprintf('%s: %s', $key, $value);
     801        }
     802        return $return;
     803    }
     804
     805    /**
     806     * Convert a key => value array to a 'key: value' array for headers
     807     *
     808     * @codeCoverageIgnore
     809     * @deprecated Misspelling of {@see Requests::flatten}
     810     * @param array $array Dictionary of header values
     811     * @return array List of headers
     812     */
     813    public static function flattern($array) {
     814        return self::flatten($array);
     815    }
     816
     817    /**
     818     * Decompress an encoded body
     819     *
     820     * Implements gzip, compress and deflate. Guesses which it is by attempting
     821     * to decode.
     822     *
     823     * @param string $data Compressed data in one of the above formats
     824     * @return string Decompressed string
     825     */
     826    public static function decompress($data) {
     827        if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") {
     828            // Not actually compressed. Probably cURL ruining this for us.
     829            return $data;
     830        }
     831
     832        if (function_exists('gzdecode')) {
     833            // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.gzdecodeFound -- Wrapped in function_exists() for PHP 5.2.
     834            $decoded = @gzdecode($data);
     835            if ($decoded !== false) {
     836                return $decoded;
     837            }
     838        }
     839
     840        if (function_exists('gzinflate')) {
     841            $decoded = @gzinflate($data);
     842            if ($decoded !== false) {
     843                return $decoded;
     844            }
     845        }
     846
     847        $decoded = self::compatible_gzinflate($data);
     848        if ($decoded !== false) {
     849            return $decoded;
     850        }
     851
     852        if (function_exists('gzuncompress')) {
     853            $decoded = @gzuncompress($data);
     854            if ($decoded !== false) {
     855                return $decoded;
     856            }
     857        }
     858
     859        return $data;
     860    }
     861
     862    /**
     863     * Decompression of deflated string while staying compatible with the majority of servers.
     864     *
     865     * Certain Servers will return deflated data with headers which PHP's gzinflate()
     866     * function cannot handle out of the box. The following function has been created from
     867     * various snippets on the gzinflate() PHP documentation.
     868     *
     869     * Warning: Magic numbers within. Due to the potential different formats that the compressed
     870     * data may be returned in, some "magic offsets" are needed to ensure proper decompression
     871     * takes place. For a simple progmatic way to determine the magic offset in use, see:
     872     * https://core.trac.wordpress.org/ticket/18273
     873     *
     874     * @since 2.8.1
     875     * @link https://core.trac.wordpress.org/ticket/18273
     876     * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
     877     * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
     878     *
     879     * @param string $gz_data String to decompress.
     880     * @return string|bool False on failure.
     881     */
     882    public static function compatible_gzinflate($gz_data) {
     883        // Compressed data might contain a full zlib header, if so strip it for
     884        // gzinflate()
     885        if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") {
     886            $i   = 10;
     887            $flg = ord(substr($gz_data, 3, 1));
     888            if ($flg > 0) {
     889                if ($flg & 4) {
     890                    list($xlen) = unpack('v', substr($gz_data, $i, 2));
     891                    $i         += 2 + $xlen;
     892                }
     893                if ($flg & 8) {
     894                    $i = strpos($gz_data, "\0", $i) + 1;
     895                }
     896                if ($flg & 16) {
     897                    $i = strpos($gz_data, "\0", $i) + 1;
     898                }
     899                if ($flg & 2) {
     900                    $i += 2;
     901                }
     902            }
     903            $decompressed = self::compatible_gzinflate(substr($gz_data, $i));
     904            if ($decompressed !== false) {
     905                return $decompressed;
     906            }
     907        }
     908
     909        // If the data is Huffman Encoded, we must first strip the leading 2
     910        // byte Huffman marker for gzinflate()
     911        // The response is Huffman coded by many compressors such as
     912        // java.util.zip.Deflater, Ruby’s Zlib::Deflate, and .NET's
     913        // System.IO.Compression.DeflateStream.
     914        //
     915        // See https://decompres.blogspot.com/ for a quick explanation of this
     916        // data type
     917        $huffman_encoded = false;
     918
     919        // low nibble of first byte should be 0x08
     920        list(, $first_nibble) = unpack('h', $gz_data);
     921
     922        // First 2 bytes should be divisible by 0x1F
     923        list(, $first_two_bytes) = unpack('n', $gz_data);
     924
     925        if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) {
     926            $huffman_encoded = true;
     927        }
     928
     929        if ($huffman_encoded) {
     930            $decompressed = @gzinflate(substr($gz_data, 2));
     931            if ($decompressed !== false) {
     932                return $decompressed;
     933            }
     934        }
     935
     936        if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") {
     937            // ZIP file format header
     938            // Offset 6: 2 bytes, General-purpose field
     939            // Offset 26: 2 bytes, filename length
     940            // Offset 28: 2 bytes, optional field length
     941            // Offset 30: Filename field, followed by optional field, followed
     942            // immediately by data
     943            list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2));
     944
     945            // If the file has been compressed on the fly, 0x08 bit is set of
     946            // the general purpose field. We can use this to differentiate
     947            // between a compressed document, and a ZIP file
     948            $zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08);
     949
     950            if (!$zip_compressed_on_the_fly) {
     951                // Don't attempt to decode a compressed zip file
     952                return $gz_data;
     953            }
     954
     955            // Determine the first byte of data, based on the above ZIP header
     956            // offsets:
     957            $first_file_start = array_sum(unpack('v2', substr($gz_data, 26, 4)));
     958            $decompressed     = @gzinflate(substr($gz_data, 30 + $first_file_start));
     959            if ($decompressed !== false) {
     960                return $decompressed;
     961            }
     962            return false;
     963        }
     964
     965        // Finally fall back to straight gzinflate
     966        $decompressed = @gzinflate($gz_data);
     967        if ($decompressed !== false) {
     968            return $decompressed;
     969        }
     970
     971        // Fallback for all above failing, not expected, but included for
     972        // debugging and preventing regressions and to track stats
     973        $decompressed = @gzinflate(substr($gz_data, 2));
     974        if ($decompressed !== false) {
     975            return $decompressed;
     976        }
     977
     978        return false;
     979    }
     980
     981    public static function match_domain($host, $reference) {
     982        // Check for a direct match
     983        if ($host === $reference) {
     984            return true;
     985        }
     986
     987        // Calculate the valid wildcard match if the host is not an IP address
     988        // Also validates that the host has 3 parts or more, as per Firefox's
     989        // ruleset.
     990        $parts = explode('.', $host);
     991        if (ip2long($host) === false && count($parts) >= 3) {
     992            $parts[0] = '*';
     993            $wildcard = implode('.', $parts);
     994            if ($wildcard === $reference) {
     995                return true;
     996            }
     997        }
     998
     999        return false;
    771000    }
    781001}
  • trunk/src/wp-includes/class-wp-http-requests-hooks.php

    r52244 r52328  
    1313 * @since 4.7.0
    1414 *
    15  * @see WpOrg\Requests\Hooks
     15 * @see Requests_Hooks
    1616 */
    17 class WP_HTTP_Requests_Hooks extends WpOrg\Requests\Hooks {
     17class WP_HTTP_Requests_Hooks extends Requests_Hooks {
    1818    /**
    1919     * Requested URL.
  • trunk/src/wp-includes/class-wp-http-requests-response.php

    r52244 r52328  
    99
    1010/**
    11  * Core wrapper object for a WpOrg\Requests\Response for standardisation.
     11 * Core wrapper object for a Requests_Response for standardisation.
    1212 *
    1313 * @since 4.6.0
     
    2020     *
    2121     * @since 4.6.0
    22      * @var \WpOrg\Requests\Response
     22     * @var Requests_Response
    2323     */
    2424    protected $response;
     
    3737     * @since 4.6.0
    3838     *
    39      * @param \WpOrg\Requests\Response $response HTTP response.
    40      * @param string                   $filename Optional. File name. Default empty.
     39     * @param Requests_Response $response HTTP response.
     40     * @param string            $filename Optional. File name. Default empty.
    4141     */
    42     public function __construct( WpOrg\Requests\Response $response, $filename = '' ) {
     42    public function __construct( Requests_Response $response, $filename = '' ) {
    4343        $this->response = $response;
    4444        $this->filename = $filename;
     
    5050     * @since 4.6.0
    5151     *
    52      * @return WpOrg\Requests\Response HTTP response.
     52     * @return Requests_Response HTTP response.
    5353     */
    5454    public function get_response_object() {
     
    6161     * @since 4.6.0
    6262     *
    63      * @return \WpOrg\Requests\Utility\CaseInsensitiveDictionary Map of header name to header value.
     63     * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
    6464     */
    6565    public function get_headers() {
    6666        // Ensure headers remain case-insensitive.
    67         $converted = new WpOrg\Requests\Utility\CaseInsensitiveDictionary();
     67        $converted = new Requests_Utility_CaseInsensitiveDictionary();
    6868
    6969        foreach ( $this->response->headers->getAll() as $key => $value ) {
     
    8686     */
    8787    public function set_headers( $headers ) {
    88         $this->response->headers = new WpOrg\Requests\Response\Headers( $headers );
     88        $this->response->headers = new Requests_Response_Headers( $headers );
    8989    }
    9090
  • trunk/src/wp-includes/class-wp-http.php

    r52327 r52328  
    88 */
    99
    10 if ( ! class_exists( 'WpOrg\Requests\Autoload' ) ) {
    11     require ABSPATH . WPINC . '/Requests/Autoload.php';
    12 
    13     WpOrg\Requests\Autoload::register();
    14     WpOrg\Requests\Requests::set_certificate_path( ABSPATH . WPINC . '/certificates/ca-bundle.crt' );
     10if ( ! class_exists( 'Requests' ) ) {
     11    require ABSPATH . WPINC . '/class-requests.php';
     12
     13    Requests::register_autoloader();
     14    Requests::set_certificate_path( ABSPATH . WPINC . '/certificates/ca-bundle.crt' );
    1515}
    1616
     
    275275            $response = new WP_Error( 'http_request_failed', __( 'A valid URL was not provided.' ) );
    276276            /** This action is documented in wp-includes/class-wp-http.php */
    277             do_action( 'http_api_debug', $response, 'response', 'WpOrg\Requests\Requests', $parsed_args, $url );
     277            do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
    278278            return $response;
    279279        }
     
    282282            $response = new WP_Error( 'http_request_not_executed', __( 'User has blocked requests through HTTP.' ) );
    283283            /** This action is documented in wp-includes/class-wp-http.php */
    284             do_action( 'http_api_debug', $response, 'response', 'WpOrg\Requests\Requests', $parsed_args, $url );
     284            do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
    285285            return $response;
    286286        }
     
    299299                $response = new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
    300300                /** This action is documented in wp-includes/class-wp-http.php */
    301                 do_action( 'http_api_debug', $response, 'response', 'WpOrg\Requests\Requests', $parsed_args, $url );
     301                do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
    302302                return $response;
    303303            }
     
    347347        }
    348348
    349         // If we've got cookies, use and convert them to WpOrg\Requests\Cookie.
     349        // If we've got cookies, use and convert them to Requests_Cookie.
    350350        if ( ! empty( $parsed_args['cookies'] ) ) {
    351351            $options['cookies'] = WP_Http::normalize_cookies( $parsed_args['cookies'] );
     
    379379        $proxy = new WP_HTTP_Proxy();
    380380        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
    381             $options['proxy'] = new WpOrg\Requests\Proxy\HTTP( $proxy->host() . ':' . $proxy->port() );
     381            $options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' . $proxy->port() );
    382382
    383383            if ( $proxy->use_authentication() ) {
     
    392392
    393393        try {
    394             $requests_response = WpOrg\Requests\Requests::request( $url, $headers, $data, $type, $options );
     394            $requests_response = Requests::request( $url, $headers, $data, $type, $options );
    395395
    396396            // Convert the response into an array.
     
    400400            // Add the original object to the array.
    401401            $response['http_response'] = $http_response;
    402         } catch ( WpOrg\Requests\Exception $e ) {
     402        } catch ( Requests_Exception $e ) {
    403403            $response = new WP_Error( 'http_request_failed', $e->getMessage() );
    404404        }
     
    417417         * @param string         $url         The request URL.
    418418         */
    419         do_action( 'http_api_debug', $response, 'response', 'WpOrg\Requests\Requests', $parsed_args, $url );
     419        do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
    420420        if ( is_wp_error( $response ) ) {
    421421            return $response;
     
    453453     *
    454454     * @param array $cookies Array of cookies to send with the request.
    455      * @return WpOrg\Requests\Cookie\Jar Cookie holder object.
     455     * @return Requests_Cookie_Jar Cookie holder object.
    456456     */
    457457    public static function normalize_cookies( $cookies ) {
    458         $cookie_jar = new WpOrg\Requests\Cookie\Jar();
     458        $cookie_jar = new Requests_Cookie_Jar();
    459459
    460460        foreach ( $cookies as $name => $value ) {
     
    466466                    }
    467467                );
    468                 $cookie_jar[ $value->name ] = new WpOrg\Requests\Cookie( $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) );
     468                $cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) );
    469469            } elseif ( is_scalar( $value ) ) {
    470                 $cookie_jar[ $name ] = new WpOrg\Requests\Cookie( $name, (string) $value );
     470                $cookie_jar[ $name ] = new Requests_Cookie( $name, $value );
    471471            }
    472472        }
     
    484484     * @since 4.6.0
    485485     *
    486      * @param string                  $location URL to redirect to.
    487      * @param array                   $headers  Headers for the redirect.
    488      * @param string|array            $data     Body to send with the request.
    489      * @param array                   $options  Redirect request options.
    490      * @param WpOrg\Requests\Response $original Response object.
     486     * @param string            $location URL to redirect to.
     487     * @param array             $headers  Headers for the redirect.
     488     * @param string|array      $data     Body to send with the request.
     489     * @param array             $options  Redirect request options.
     490     * @param Requests_Response $original Response object.
    491491     */
    492492    public static function browser_redirect_compatibility( $location, $headers, $data, &$options, $original ) {
    493493        // Browser compatibility.
    494494        if ( 302 === $original->status_code ) {
    495             $options['type'] = WpOrg\Requests\Requests::GET;
     495            $options['type'] = Requests::GET;
    496496        }
    497497    }
     
    502502     * @since 4.7.5
    503503     *
    504      * @throws WpOrg\Requests\Exception On unsuccessful URL validation.
     504     * @throws Requests_Exception On unsuccessful URL validation.
    505505     * @param string $location URL to redirect to.
    506506     */
    507507    public static function validate_redirects( $location ) {
    508508        if ( ! wp_http_validate_url( $location ) ) {
    509             throw new WpOrg\Requests\Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' );
     509            throw new Requests_Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' );
    510510        }
    511511    }
  • trunk/src/wp-includes/http.php

    r52244 r52328  
    201201 *
    202202 * @since 2.7.0
    203  * @since 4.6.0 Return value changed from an array to an WpOrg\Requests\Utility\CaseInsensitiveDictionary instance.
    204  *
    205  * @see \WpOrg\Requests\Utility\CaseInsensitiveDictionary
     203 * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance.
     204 *
     205 * @see \Requests_Utility_CaseInsensitiveDictionary
    206206 *
    207207 * @param array|WP_Error $response HTTP response.
    208  * @return array|\WpOrg\Requests\Utility\CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
     208 * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
    209209 */
    210210function wp_remote_retrieve_headers( $response ) {
  • trunk/src/wp-includes/rest-api.php

    r52286 r52328  
    13911391    $ipv4_pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
    13921392
    1393     if ( ! preg_match( $ipv4_pattern, $ip ) && ! WpOrg\Requests\Ipv6::check_ipv6( $ip ) ) {
     1393    if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests_IPv6::check_ipv6( $ip ) ) {
    13941394        return false;
    13951395    }
  • trunk/tests/phpunit/tests/feed/wpSimplePieFile.php

    r52244 r52328  
    8686
    8787        return array(
    88             'headers'  => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
     88            'headers'  => new Requests_Utility_CaseInsensitiveDictionary( $single_value_headers ),
    8989            'body'     => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
    9090            'response' => array(
     
    115115        );
    116116
    117         $response['headers'] = new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $multiple_value_headers );
     117        $response['headers'] = new Requests_Utility_CaseInsensitiveDictionary( $multiple_value_headers );
    118118
    119119        return $response;
  • trunk/tests/phpunit/tests/functions.php

    r52244 r52328  
    365365    public function test_deserialize_request_utility_filtered_iterator_objects( $value ) {
    366366        $serialized = maybe_serialize( $value );
    367         if ( get_class( $value ) === 'WpOrg\Requests\Utility\FilteredIterator' ) {
     367        if ( get_class( $value ) === 'Requests_Utility_FilteredIterator' ) {
    368368            $new_value = unserialize( $serialized );
    369             $property  = ( new ReflectionClass( 'WpOrg\Requests\Utility\FilteredIterator' ) )->getProperty( 'callback' );
     369            $property  = ( new ReflectionClass( 'Requests_Utility_FilteredIterator' ) )->getProperty( 'callback' );
    370370            $property->setAccessible( true );
    371371            $callback_value = $property->getValue( $new_value );
     
    378378    public function data_serialize_deserialize_objects() {
    379379        return array(
    380             array( new WpOrg\Requests\Utility\FilteredIterator( array( 1 ), 'md5' ) ),
    381             array( new WpOrg\Requests\Utility\FilteredIterator( array( 1, 2 ), 'sha1' ) ),
     380            array( new Requests_Utility_FilteredIterator( array( 1 ), 'md5' ) ),
     381            array( new Requests_Utility_FilteredIterator( array( 1, 2 ), 'sha1' ) ),
    382382            array( new ArrayIterator( array( 1, 2, 3 ) ) ),
    383383        );
  • trunk/tests/phpunit/tests/http/functions.php

    r52244 r52328  
    217217    public function test_get_cookie_host_only() {
    218218        // Emulate WP_Http::request() internals.
    219         $requests_response = new WpOrg\Requests\Response();
    220 
    221         $requests_response->cookies['test'] = WpOrg\Requests\Cookie::parse( 'test=foo; domain=.wordpress.org' );
     219        $requests_response = new Requests_Response();
     220
     221        $requests_response->cookies['test'] = Requests_Cookie::parse( 'test=foo; domain=.wordpress.org' );
    222222
    223223        $requests_response->cookies['test']->flags['host-only'] = false; // https://github.com/WordPress/Requests/issues/306
     
    232232        $this->assertFalse( $cookie->host_only, 'host-only flag not set' );
    233233
    234         // Regurgitate (WpOrg\Requests\Cookie -> WP_Http_Cookie -> WpOrg\Requests\Cookie).
     234        // Regurgitate (Requests_Cookie -> WP_Http_Cookie -> Requests_Cookie).
    235235        $cookies = WP_Http::normalize_cookies( wp_remote_retrieve_cookies( $response ) );
    236236        $this->assertFalse( $cookies['test']->flags['host-only'], 'host-only flag data lost' );
  • trunk/tests/phpunit/tests/http/http.php

    r52244 r52328  
    310310        );
    311311
    312         $this->assertInstanceOf( 'WpOrg\Requests\Cookie\Jar', $cookie_jar );
     312        $this->assertInstanceOf( 'Requests_Cookie_Jar', $cookie_jar );
    313313
    314314        foreach ( array_keys( $cookies ) as $cookie ) {
     
    316316                $this->assertArrayNotHasKey( $cookie, $cookie_jar );
    317317            } else {
    318                 $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar[ $cookie ] );
     318                $this->assertInstanceOf( 'Requests_Cookie', $cookie_jar[ $cookie ] );
    319319            }
    320320        }
  • trunk/tests/phpunit/tests/rest-api/rest-widgets-controller.php

    r52244 r52328  
    417417
    418418        return array(
    419             'headers'  => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
     419            'headers'  => new Requests_Utility_CaseInsensitiveDictionary( $single_value_headers ),
    420420            'body'     => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
    421421            'response' => array(
  • trunk/tests/phpunit/tests/widgets/wpWidgetRss.php

    r52244 r52328  
    107107
    108108        return array(
    109             'headers'  => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
     109            'headers'  => new Requests_Utility_CaseInsensitiveDictionary( $single_value_headers ),
    110110            'body'     => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
    111111            'response' => array(
Note: See TracChangeset for help on using the changeset viewer.