Make WordPress Core


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

HTTP API: Revert changeset [52244].

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

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

Follow-up to [52327].

See #54562, #54504.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/Requests/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}
Note: See TracChangeset for help on using the changeset viewer.