Changeset 50842 for trunk/src/wp-includes/class-requests.php
- Timestamp:
- 05/11/2021 07:40:41 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-requests.php
r47902 r50842 89 89 * @var string 90 90 */ 91 const VERSION = '1.7 -3470169';91 const VERSION = '1.7'; 92 92 93 93 /** … … 144 144 $file = str_replace('_', '/', $class); 145 145 if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) { 146 require_once (dirname(__FILE__) . '/' . $file . '.php');146 require_once dirname(__FILE__) . '/' . $file . '.php'; 147 147 } 148 148 } … … 343 343 * (string|boolean, default: library/Requests/Transport/cacert.pem) 344 344 * - `verifyname`: Should we verify the common name in the SSL certificate? 345 * (boolean : default,true)345 * (boolean, default: true) 346 346 * - `data_format`: How should we send the `$data` parameter? 347 347 * (string, one of 'query' or 'body', default: 'query' for … … 375 375 } 376 376 else { 377 $need_ssl = (0 === stripos($url, 'https://'));377 $need_ssl = (stripos($url, 'https://') === 0); 378 378 $capabilities = array('ssl' => $need_ssl); 379 $transport = self::get_transport($capabilities);379 $transport = self::get_transport($capabilities); 380 380 } 381 381 $response = $transport->request($url, $headers, $data, $options); … … 448 448 } 449 449 if (!isset($request['options'])) { 450 $request['options'] = $options;450 $request['options'] = $options; 451 451 $request['options']['type'] = $request['type']; 452 452 } … … 504 504 protected static function get_default_options($multirequest = false) { 505 505 $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, 509 509 'protocol_version' => 1.1, 510 'redirected' => 0,511 'redirects' => 10,510 'redirected' => 0, 511 'redirects' => 10, 512 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' => 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, 525 525 ); 526 526 if ($multirequest !== false) { … … 536 536 */ 537 537 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; 540 540 } 541 541 … … 548 548 * @param string $path Certificate path, pointing to a PEM file. 549 549 */ 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; 552 552 } 553 553 … … 596 596 597 597 if ($options['idn'] !== false) { 598 $iri = new Requests_IRI($url);598 $iri = new Requests_IRI($url); 599 599 $iri->host = Requests_IDNAEncoder::encode($iri->ihost); 600 $url = $iri->uri;600 $url = $iri->uri; 601 601 } 602 602 … … 605 605 606 606 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)) { 608 608 $options['data_format'] = 'query'; 609 609 } … … 634 634 } 635 635 636 $return->raw = $headers; 637 $return->url = $url; 636 $return->raw = $headers; 637 $return->url = (string) $url; 638 $return->body = ''; 638 639 639 640 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) { 641 643 // Crap! 642 644 throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator'); … … 644 646 645 647 $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 } 650 653 } 651 654 // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3) … … 659 662 } 660 663 $return->protocol_version = (float) $matches[1]; 661 $return->status_code = (int) $matches[2];664 $return->status_code = (int) $matches[2]; 662 665 if ($return->status_code >= 200 && $return->status_code < 300) { 663 666 $return->success = true; … … 666 669 foreach ($headers as $header) { 667 670 list($key, $value) = explode(':', $header, 2); 668 $value = trim($value);671 $value = trim($value); 669 672 preg_replace('#(\s+)#i', ' ', $value); 670 673 $return->headers[$key] = $value; … … 703 706 &$req_data, 704 707 &$options, 705 $return 708 $return, 706 709 ); 707 710 $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); 709 712 $redirected->history[] = $return; 710 713 return $redirected; … … 733 736 public static function parse_multiple(&$response, $request) { 734 737 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']; 739 742 $response = self::parse_response($response, $url, $headers, $data, $options); 740 743 } … … 755 758 return $data; 756 759 } 757 758 759 760 760 761 $decoded = ''; … … 775 776 776 777 $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); 779 780 780 781 if (trim($encoded) === '0' || empty($encoded)) { … … 792 793 * 793 794 * @param array $array Dictionary of header values 794 * @return string[]List of headers795 * @return array List of headers 795 796 */ 796 797 public static function flatten($array) { … … 808 809 * @deprecated Misspelling of {@see Requests::flatten} 809 810 * @param array $array Dictionary of header values 810 * @return string[]List of headers811 * @return array List of headers 811 812 */ 812 813 public static function flattern($array) { … … 829 830 } 830 831 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) { 832 849 return $decoded; 833 850 } 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 } 842 857 } 843 858 … … 862 877 * @link https://secure.php.net/manual/en/function.gzinflate.php#77336 863 878 * 864 * @param string $gz Data String to decompress.879 * @param string $gz_data String to decompress. 865 880 * @return string|bool False on failure. 866 881 */ 867 public static function compatible_gzinflate($gz Data) {882 public static function compatible_gzinflate($gz_data) { 868 883 // Compressed data might contain a full zlib header, if so strip it for 869 884 // gzinflate() 870 if (substr($gz Data, 0, 3)== "\x1f\x8b\x08") {871 $i = 10;872 $flg = ord(substr($gz Data, 3, 1));885 if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") { 886 $i = 10; 887 $flg = ord(substr($gz_data, 3, 1)); 873 888 if ($flg > 0) { 874 889 if ($flg & 4) { 875 list($xlen) = unpack('v', substr($gz Data, $i, 2));876 $i = $i +2 + $xlen;890 list($xlen) = unpack('v', substr($gz_data, $i, 2)); 891 $i += 2 + $xlen; 877 892 } 878 893 if ($flg & 8) { 879 $i = strpos($gz Data, "\0", $i) + 1;894 $i = strpos($gz_data, "\0", $i) + 1; 880 895 } 881 896 if ($flg & 16) { 882 $i = strpos($gz Data, "\0", $i) + 1;897 $i = strpos($gz_data, "\0", $i) + 1; 883 898 } 884 899 if ($flg & 2) { 885 $i = $i +2;900 $i += 2; 886 901 } 887 902 } 888 $decompressed = self::compatible_gzinflate(substr($gz Data, $i));889 if ( false !== $decompressed) {903 $decompressed = self::compatible_gzinflate(substr($gz_data, $i)); 904 if ($decompressed !== false) { 890 905 return $decompressed; 891 906 } … … 903 918 904 919 // low nibble of first byte should be 0x08 905 list(, $first_nibble) = unpack('h', $gzData);920 list(, $first_nibble) = unpack('h', $gz_data); 906 921 907 922 // First 2 bytes should be divisible by 0x1F 908 list(, $first_two_bytes) = unpack('n', $gz Data);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) { 911 926 $huffman_encoded = true; 912 927 } 913 928 914 929 if ($huffman_encoded) { 915 if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) { 930 $decompressed = @gzinflate(substr($gz_data, 2)); 931 if ($decompressed !== false) { 916 932 return $decompressed; 917 933 } 918 934 } 919 935 920 if ( "\x50\x4b\x03\x04" == substr($gzData, 0, 4)) {936 if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") { 921 937 // ZIP file format header 922 938 // Offset 6: 2 bytes, General-purpose field … … 925 941 // Offset 30: Filename field, followed by optional field, followed 926 942 // immediately by data 927 list(, $general_purpose_flag) = unpack('v', substr($gz Data, 6, 2));943 list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2)); 928 944 929 945 // If the file has been compressed on the fly, 0x08 bit is set of 930 946 // the general purpose field. We can use this to differentiate 931 947 // 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); 933 949 934 950 if (!$zip_compressed_on_the_fly) { 935 951 // Don't attempt to decode a compressed zip file 936 return $gz Data;952 return $gz_data; 937 953 } 938 954 939 955 // Determine the first byte of data, based on the above ZIP header 940 956 // 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) { 943 960 return $decompressed; 944 961 } … … 947 964 948 965 // Finally fall back to straight gzinflate 949 if (false !== ($decompressed = @gzinflate($gzData))) { 966 $decompressed = @gzinflate($gz_data); 967 if ($decompressed !== false) { 950 968 return $decompressed; 951 969 } … … 953 971 // Fallback for all above failing, not expected, but included for 954 972 // 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) { 956 975 return $decompressed; 957 976 }
Note: See TracChangeset
for help on using the changeset viewer.