Ticket #8674: 8674.2.diff
File 8674.2.diff, 7.1 KB (added by , 15 years ago) |
---|
-
http.php
13 13 */ 14 14 15 15 /** 16 * Implementation for deflate and gzip transfer encodings. 17 * 18 * Includes RFC 1950, RFC 1951, and RFC 1952. 19 * 20 * @since unknown 21 * @package WordPress 22 * @subpackage HTTP 23 */ 24 class WP_Http_Encoding { 25 26 /** 27 * Compress raw string using the deflate format. 28 * 29 * Supports the RFC 1951 standard. 30 * 31 * @since unknown 32 * 33 * @param string $raw String to compress. 34 * @param int $level Optional, default is 9. Compression level, 9 is highest. 35 * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports. 36 * @return string|bool False on failure. 37 */ 38 function compress( $raw, $level = 9, $supports = null ) { 39 return gzdeflate( $raw, $level ); 40 } 41 42 /** 43 * Decompression of deflated string. 44 * 45 * Will attempt to decompress using the RFC 1950 standard, and if that fails 46 * then the RFC 1951 standard deflate will be attempted. Finally, the RFC 47 * 1952 standard gzip decode will be attempted. If all fail, then the 48 * original compressed string will be returned. 49 * 50 * @since unknown 51 * 52 * @param string $compressed String to decompress. 53 * @param int $length The optional length of the compressed data. 54 * @return string|bool False on failure. 55 */ 56 function decompress( $compressed, $length = null ) { 57 $decompressed = gzinflate( $compressed ); 58 59 if( false !== $decompressed ) 60 return $decompressed; 61 62 $decompressed = gzuncompress( $compressed ); 63 64 if( false !== $decompressed ) 65 return $decompressed; 66 67 $decompressed = gzdecode( $compressed ); 68 69 if( false !== $decompressed ) 70 return $decompressed; 71 72 return $compressed; 73 } 74 75 /** 76 * What encoding types to accept and their priority values. 77 * 78 * @since unknown 79 * 80 * @return string Types of encoding to accept. 81 */ 82 function accept_encoding() { 83 $type = array; 84 if( function_exists( 'gzinflate' ) ) 85 $type[] = 'deflate;q=1.0'; 86 87 if( function_exists( 'gzuncompress' ) ) 88 $type[] = 'compress;q=0.5'; 89 90 if( function_exists( 'gzdecode' ) ) 91 $type[] = 'gzip;q=0.5'; 92 93 return implode(', ', $type); 94 } 95 96 /** 97 * What enconding the content used when it was compressed to send in the headers. 98 * 99 * @since unknown 100 * 101 * @return string Content-Encoding string to send in the header. 102 */ 103 function content_encoding() { 104 return 'deflate'; 105 } 106 107 /** 108 * Whether the content be decoded based on the headers. 109 * 110 * @since unknown 111 * 112 * @param array|string $headers All of the available headers. 113 * @return bool 114 */ 115 function should_decode($headers) { 116 if( is_array( $headers ) ) { 117 if( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) ) 118 return true; 119 } else if( is_string( $headers ) ) { 120 return ( stripos($headers, 'content-encoding:') !== false ); 121 } 122 123 return false; 124 } 125 126 /** 127 * Whether decompression and compression are supported by the PHP version. 128 * 129 * Each function is tested instead of checking for the zlib extension, to 130 * ensure that the functions all exist in the PHP version and aren't 131 * disabled. 132 * 133 * @since unknown 134 * 135 * @return bool 136 */ 137 function is_available() { 138 return ( function_exists('gzuncompress') || function_exists('gzdeflate') || 139 function_exists('gzinflate') ); 140 } 141 } 142 143 /** 16 144 * WordPress HTTP Class for managing HTTP Transports and making HTTP requests. 17 145 * 18 146 * This class is called for the functionality of making HTTP requests and should … … 217 345 'httpversion' => apply_filters( 'http_request_version', '1.0'), 218 346 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ), 219 347 'blocking' => true, 220 'headers' => array(), 'body' => null 348 'headers' => array(), 349 'body' => null, 350 'compress' => false, 351 'decompress' = true 221 352 ); 222 353 223 354 $r = wp_parse_args( $args, $defaults ); … … 241 372 unset($r['headers']['user-agent']); 242 373 } 243 374 375 if( WP_Http_Encoding::is_available() ) 376 $r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding(); 377 244 378 if ( is_null($r['body']) ) { 245 379 $r['headers']['Content-Length'] = 0; 246 380 $transports = WP_Http::_getTransport($r); … … 578 712 if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] ) 579 713 $process['body'] = WP_Http::chunkTransferDecode($process['body']); 580 714 715 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders) ) 716 $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); 717 581 718 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response']); 582 719 } 583 720 … … 688 825 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] ) 689 826 $strResponse = WP_Http::chunkTransferDecode($strResponse); 690 827 828 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders) ) 829 $strResponse = WP_Http_Encoding::decompress( $strResponse ); 830 691 831 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']); 692 832 } 693 833 … … 799 939 $strResponse = stream_get_contents($handle); 800 940 $meta = stream_get_meta_data($handle); 801 941 942 fclose($handle); 943 802 944 $processedHeaders = array(); 803 945 if( isset( $meta['wrapper_data']['headers'] ) ) 804 946 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']['headers']); … … 808 950 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] ) 809 951 $strResponse = WP_Http::chunkTransferDecode($strResponse); 810 952 811 fclose($handle); 953 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders) ) 954 $strResponse = WP_Http_Encoding::decompress( $strResponse ); 812 955 813 956 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']); 814 957 } … … 922 1065 $theBody = http_chunked_decode($theBody); 923 1066 } 924 1067 1068 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders) ) 1069 $theBody = http_inflate( $theBody ); 1070 925 1071 $theResponse = array(); 926 1072 $theResponse['code'] = $info['response_code']; 927 1073 $theResponse['message'] = get_status_header_desc($info['response_code']); … … 1045 1191 $theHeaders = array( 'headers' => array() ); 1046 1192 $theBody = ''; 1047 1193 } 1194 1048 1195 $response = array(); 1049 1196 $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE ); 1050 1197 $response['message'] = get_status_header_desc($response['code']); 1051 1198 1052 1199 curl_close( $handle ); 1053 1200 1201 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders) ) 1202 $theBody = WP_Http_Encoding::decompress( $theBody ); 1203 1054 1204 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response); 1055 1205 } 1056 1206