Make WordPress Core


Ignore:
Timestamp:
05/11/2021 07:40:41 PM (6 years ago)
Author:
desrosj
Message:

External Libraries: Update the Requests library to version 1.8.0.

While some of the changes in the 1.8.0 release have already been copied to WordPress Core in earlier releases (see [38727], [46258], [47902] and [49382]), this release contains additional improvements, including:

  • A significant performance fix when using cURL.
  • Improved compliance with RFC2616.

The library has also been moved under the WordPress project’s GitHub organization and can now be found at ​https://github.com/WordPress/Requests.

Props jrf, dd32, rmccue, justinahinon, netweb, schlessera, TimothyBJacobs, soulseekah, ozh, skithund, carlalexander, travisnorthcutt, desrosj.
Fixes #53101.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-requests.php

    r47902 r50842  
    8989         * @var string
    9090         */
    91         const VERSION = '1.7-3470169';
     91        const VERSION = '1.7';
    9292
    9393        /**
    … …  
    144144                $file = str_replace('_', '/', $class);
    145145                if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
    146                         require_once(dirname(__FILE__) . '/' . $file . '.php');
     146                        require_once dirname(__FILE__) . '/' . $file . '.php';
    147147                }
    148148        }
    … …  
    343343         *    (string|boolean, default: library/Requests/Transport/cacert.pem)
    344344         * - `verifyname`: Should we verify the common name in the SSL certificate?
    345          *    (boolean: default, true)
     345         *    (boolean, default: true)
    346346         * - `data_format`: How should we send the `$data` parameter?
    347347         *    (string, one of 'query' or 'body', default: 'query' for
    … …  
    375375                }
    376376                else {
    377                         $need_ssl = (0 === stripos($url, 'https://'));
     377                        $need_ssl     = (stripos($url, 'https://') === 0);
    378378                        $capabilities = array('ssl' => $need_ssl);
    379                         $transport = self::get_transport($capabilities);
     379                        $transport    = self::get_transport($capabilities);
    380380                }
    381381                $response = $transport->request($url, $headers, $data, $options);
    … …  
    448448                        }
    449449                        if (!isset($request['options'])) {
    450                                 $request['options'] = $options;
     450                                $request['options']         = $options;
    451451                                $request['options']['type'] = $request['type'];
    452452                        }
    … …  
    504504        protected static function get_default_options($multirequest = false) {
    505505                $defaults = array(
    506                         'timeout' => 10,
    507                         'connect_timeout' => 10,
    508                         'useragent' => 'php-requests/' . self::VERSION,
     506                        'timeout'          => 10,
     507                        'connect_timeout'  => 10,
     508                        'useragent'        => 'php-requests/' . self::VERSION,
    509509                        'protocol_version' => 1.1,
    510                         'redirected' => 0,
    511                         'redirects' => 10,
     510                        'redirected'       => 0,
     511                        'redirects'        => 10,
    512512                        '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' => Requests::get_certificate_path(),
    524                         'verifyname' => 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,
    525525                );
    526526                if ($multirequest !== false) {
    … …  
    536536         */
    537537        public static function get_certificate_path() {
    538                 if ( ! empty( Requests::$certificate_path ) ) {
    539                         return Requests::$certificate_path;
     538                if (!empty(self::$certificate_path)) {
     539                        return self::$certificate_path;
    540540                }
    541541
    … …  
    548548         * @param string $path Certificate path, pointing to a PEM file.
    549549         */
    550         public static function set_certificate_path( $path ) {
    551                 Requests::$certificate_path = $path;
     550        public static function set_certificate_path($path) {
     551                self::$certificate_path = $path;
    552552        }
    553553
    … …  
    596596
    597597                if ($options['idn'] !== false) {
    598                         $iri = new Requests_IRI($url);
     598                        $iri       = new Requests_IRI($url);
    599599                        $iri->host = Requests_IDNAEncoder::encode($iri->ihost);
    600                         $url = $iri->uri;
     600                        $url       = $iri->uri;
    601601                }
    602602
    … …  
    605605
    606606                if (!isset($options['data_format'])) {
    607                         if (in_array($type, array(self::HEAD, self::GET, self::DELETE))) {
     607                        if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) {
    608608                                $options['data_format'] = 'query';
    609609                        }
    … …  
    634634                }
    635635
    636                 $return->raw = $headers;
    637                 $return->url = $url;
     636                $return->raw  = $headers;
     637                $return->url  = (string) $url;
     638                $return->body = '';
    638639
    639640                if (!$options['filename']) {
    640                         if (($pos = strpos($headers, "\r\n\r\n")) === false) {
     641                        $pos = strpos($headers, "\r\n\r\n");
     642                        if ($pos === false) {
    641643                                // Crap!
    642644                                throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');
    … …  
    644646
    645647                        $headers = substr($return->raw, 0, $pos);
    646                         $return->body = substr($return->raw, $pos + strlen("\n\r\n\r"));
    647                 }
    648                 else {
    649                         $return->body = '';
     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                        }
    650653                }
    651654                // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3)
    … …  
    659662                }
    660663                $return->protocol_version = (float) $matches[1];
    661                 $return->status_code = (int) $matches[2];
     664                $return->status_code      = (int) $matches[2];
    662665                if ($return->status_code >= 200 && $return->status_code < 300) {
    663666                        $return->success = true;
    … …  
    666669                foreach ($headers as $header) {
    667670                        list($key, $value) = explode(':', $header, 2);
    668                         $value = trim($value);
     671                        $value             = trim($value);
    669672                        preg_replace('#(\s+)#i', ' ', $value);
    670673                        $return->headers[$key] = $value;
    … …  
    703706                                        &$req_data,
    704707                                        &$options,
    705                                         $return
     708                                        $return,
    706709                                );
    707710                                $options['hooks']->dispatch('requests.before_redirect', $hook_args);
    708                                 $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options);
     711                                $redirected            = self::request($location, $req_headers, $req_data, $options['type'], $options);
    709712                                $redirected->history[] = $return;
    710713                                return $redirected;
    … …  
    733736        public static function parse_multiple(&$response, $request) {
    734737                try {
    735                         $url = $request['url'];
    736                         $headers = $request['headers'];
    737                         $data = $request['data'];
    738                         $options = $request['options'];
     738                        $url      = $request['url'];
     739                        $headers  = $request['headers'];
     740                        $data     = $request['data'];
     741                        $options  = $request['options'];
    739742                        $response = self::parse_response($response, $url, $headers, $data, $options);
    740743                }
    … …  
    755758                        return $data;
    756759                }
    757 
    758 
    759760
    760761                $decoded = '';
    … …  
    775776
    776777                        $chunk_length = strlen($matches[0]);
    777                         $decoded .= substr($encoded, $chunk_length, $length);
    778                         $encoded = substr($encoded, $chunk_length + $length + 2);
     778                        $decoded     .= substr($encoded, $chunk_length, $length);
     779                        $encoded      = substr($encoded, $chunk_length + $length + 2);
    779780
    780781                        if (trim($encoded) === '0' || empty($encoded)) {
    … …  
    792793         *
    793794         * @param array $array Dictionary of header values
    794          * @return string[] List of headers
     795         * @return array List of headers
    795796         */
    796797        public static function flatten($array) {
    … …  
    808809         * @deprecated Misspelling of {@see Requests::flatten}
    809810         * @param array $array Dictionary of header values
    810          * @return string[] List of headers
     811         * @return array List of headers
    811812         */
    812813        public static function flattern($array) {
    … …  
    829830                }
    830831
    831                 if (function_exists('gzdecode') && ($decoded = @gzdecode($data)) !== false) {
     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) {
    832849                        return $decoded;
    833850                }
    834                 elseif (function_exists('gzinflate') && ($decoded = @gzinflate($data)) !== false) {
    835                         return $decoded;
    836                 }
    837                 elseif (($decoded = self::compatible_gzinflate($data)) !== false) {
    838                         return $decoded;
    839                 }
    840                 elseif (function_exists('gzuncompress') && ($decoded = @gzuncompress($data)) !== false) {
    841                         return $decoded;
     851
     852                if (function_exists('gzuncompress')) {
     853                        $decoded = @gzuncompress($data);
     854                        if ($decoded !== false) {
     855                                return $decoded;
     856                        }
    842857                }
    843858
    … …  
    862877         * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
    863878         *
    864          * @param string $gzData String to decompress.
     879         * @param string $gz_data String to decompress.
    865880         * @return string|bool False on failure.
    866881         */
    867         public static function compatible_gzinflate($gzData) {
     882        public static function compatible_gzinflate($gz_data) {
    868883                // Compressed data might contain a full zlib header, if so strip it for
    869884                // gzinflate()
    870                 if (substr($gzData, 0, 3) == "\x1f\x8b\x08") {
    871                         $i = 10;
    872                         $flg = ord(substr($gzData, 3, 1));
     885                if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") {
     886                        $i   = 10;
     887                        $flg = ord(substr($gz_data, 3, 1));
    873888                        if ($flg > 0) {
    874889                                if ($flg & 4) {
    875                                         list($xlen) = unpack('v', substr($gzData, $i, 2));
    876                                         $i = $i + 2 + $xlen;
     890                                        list($xlen) = unpack('v', substr($gz_data, $i, 2));
     891                                        $i         += 2 + $xlen;
    877892                                }
    878893                                if ($flg & 8) {
    879                                         $i = strpos($gzData, "\0", $i) + 1;
     894                                        $i = strpos($gz_data, "\0", $i) + 1;
    880895                                }
    881896                                if ($flg & 16) {
    882                                         $i = strpos($gzData, "\0", $i) + 1;
     897                                        $i = strpos($gz_data, "\0", $i) + 1;
    883898                                }
    884899                                if ($flg & 2) {
    885                                         $i = $i + 2;
     900                                        $i += 2;
    886901                                }
    887902                        }
    888                         $decompressed = self::compatible_gzinflate(substr($gzData, $i));
    889                         if (false !== $decompressed) {
     903                        $decompressed = self::compatible_gzinflate(substr($gz_data, $i));
     904                        if ($decompressed !== false) {
    890905                                return $decompressed;
    891906                        }
    … …  
    903918
    904919                // low nibble of first byte should be 0x08
    905                 list(, $first_nibble)    = unpack('h', $gzData);
     920                list(, $first_nibble) = unpack('h', $gz_data);
    906921
    907922                // First 2 bytes should be divisible by 0x1F
    908                 list(, $first_two_bytes) = unpack('n', $gzData);
    909 
    910                 if (0x08 == $first_nibble && 0 == ($first_two_bytes % 0x1F)) {
     923                list(, $first_two_bytes) = unpack('n', $gz_data);
     924
     925                if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) {
    911926                        $huffman_encoded = true;
    912927                }
    913928
    914929                if ($huffman_encoded) {
    915                         if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
     930                        $decompressed = @gzinflate(substr($gz_data, 2));
     931                        if ($decompressed !== false) {
    916932                                return $decompressed;
    917933                        }
    918934                }
    919935
    920                 if ("\x50\x4b\x03\x04" == substr($gzData, 0, 4)) {
     936                if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") {
    921937                        // ZIP file format header
    922938                        // Offset 6: 2 bytes, General-purpose field
    … …  
    925941                        // Offset 30: Filename field, followed by optional field, followed
    926942                        // immediately by data
    927                         list(, $general_purpose_flag) = unpack('v', substr($gzData, 6, 2));
     943                        list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2));
    928944
    929945                        // If the file has been compressed on the fly, 0x08 bit is set of
    930946                        // the general purpose field. We can use this to differentiate
    931947                        // between a compressed document, and a ZIP file
    932                         $zip_compressed_on_the_fly = (0x08 == (0x08 & $general_purpose_flag));
     948                        $zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08);
    933949
    934950                        if (!$zip_compressed_on_the_fly) {
    935951                                // Don't attempt to decode a compressed zip file
    936                                 return $gzData;
     952                                return $gz_data;
    937953                        }
    938954
    939955                        // Determine the first byte of data, based on the above ZIP header
    940956                        // offsets:
    941                         $first_file_start = array_sum(unpack('v2', substr($gzData, 26, 4)));
    942                         if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) {
     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) {
    943960                                return $decompressed;
    944961                        }
    … …  
    947964
    948965                // Finally fall back to straight gzinflate
    949                 if (false !== ($decompressed = @gzinflate($gzData))) {
     966                $decompressed = @gzinflate($gz_data);
     967                if ($decompressed !== false) {
    950968                        return $decompressed;
    951969                }
    … …  
    953971                // Fallback for all above failing, not expected, but included for
    954972                // debugging and preventing regressions and to track stats
    955                 if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
     973                $decompressed = @gzinflate(substr($gz_data, 2));
     974                if ($decompressed !== false) {
    956975                        return $decompressed;
    957976                }
Note: See TracChangeset for help on using the changeset viewer.