3 * HTTP API: WP_Http_Encoding class
11 * Core class used to implement deflate and gzip transfer encoding support for HTTP requests.
13 * Includes RFC 1950, RFC 1951, and RFC 1952.
17 class WP_Http_Encoding {
20 * Compress raw string using the deflate format.
22 * Supports the RFC 1951 standard.
28 * @param string $raw String to compress.
29 * @param int $level Optional, default is 9. Compression level, 9 is highest.
30 * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
31 * @return string|false False on failure.
33 public static function compress( $raw, $level = 9, $supports = null ) {
34 return gzdeflate( $raw, $level );
38 * Decompression of deflated string.
40 * Will attempt to decompress using the RFC 1950 standard, and if that fails
41 * then the RFC 1951 standard deflate will be attempted. Finally, the RFC
42 * 1952 standard gzip decode will be attempted. If all fail, then the
43 * original compressed string will be returned.
49 * @param string $compressed String to decompress.
50 * @param int $length The optional length of the compressed data.
51 * @return string|bool False on failure.
53 public static function decompress( $compressed, $length = null ) {
55 if ( empty($compressed) )
58 if ( false !== ( $decompressed = @gzinflate( $compressed ) ) )
61 if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) )
64 if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) )
67 if ( function_exists('gzdecode') ) {
68 $decompressed = @gzdecode( $compressed );
70 if ( false !== $decompressed )
78 * Decompression of deflated string while staying compatible with the majority of servers.
80 * Certain Servers will return deflated data with headers which PHP's gzinflate()
81 * function cannot handle out of the box. The following function has been created from
82 * various snippets on the gzinflate() PHP documentation.
84 * Warning: Magic numbers within. Due to the potential different formats that the compressed
85 * data may be returned in, some "magic offsets" are needed to ensure proper decompression
86 * takes place. For a simple progmatic way to determine the magic offset in use, see:
87 * https://core.trac.wordpress.org/ticket/18273
90 * @link https://core.trac.wordpress.org/ticket/18273
91 * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
92 * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
96 * @param string $gzData String to decompress.
97 * @return string|bool False on failure.
99 public static function compatible_gzinflate($gzData) {
101 // Compressed data might contain a full header, if so strip it for gzinflate().
102 if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
104 $flg = ord( substr($gzData, 3, 1) );
107 list($xlen) = unpack('v', substr($gzData, $i, 2) );
111 $i = strpos($gzData, "\0", $i) + 1;
113 $i = strpos($gzData, "\0", $i) + 1;
117 $decompressed = @gzinflate( substr($gzData, $i, -8) );
118 if ( false !== $decompressed )
119 return $decompressed;
122 // Compressed data from java.util.zip.Deflater amongst others.
123 $decompressed = @gzinflate( substr($gzData, 2) );
124 if ( false !== $decompressed )
125 return $decompressed;
131 * What encoding types to accept and their priority values.
139 * @return string Types of encoding to accept.
141 public static function accept_encoding( $url, $args ) {
143 $compression_enabled = self::is_available();
145 if ( ! $args['decompress'] ) // Decompression specifically disabled.
146 $compression_enabled = false;
147 elseif ( $args['stream'] ) // Disable when streaming to file.
148 $compression_enabled = false;
149 elseif ( isset( $args['limit_response_size'] ) ) // If only partial content is being requested, we won't be able to decompress it.
150 $compression_enabled = false;
152 if ( $compression_enabled ) {
153 if ( function_exists( 'gzinflate' ) )
154 $type[] = 'deflate;q=1.0';
156 if ( function_exists( 'gzuncompress' ) )
157 $type[] = 'compress;q=0.5';
159 if ( function_exists( 'gzdecode' ) )
160 $type[] = 'gzip;q=0.5';
164 * Filters the allowed encoding types.
168 * @param array $type Encoding types allowed. Accepts 'gzinflate',
169 * 'gzuncompress', 'gzdecode'.
170 * @param string $url URL of the HTTP request.
171 * @param array $args HTTP request arguments.
173 $type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
175 return implode(', ', $type);
179 * What encoding the content used when it was compressed to send in the headers.
185 * @return string Content-Encoding string to send in the header.
187 public static function content_encoding() {
192 * Whether the content be decoded based on the headers.
198 * @param array|string $headers All of the available headers.
201 public static function should_decode($headers) {
202 if ( is_array( $headers ) ) {
203 if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
205 } elseif ( is_string( $headers ) ) {
206 return ( stripos($headers, 'content-encoding:') !== false );
213 * Whether decompression and compression are supported by the PHP version.
215 * Each function is tested instead of checking for the zlib extension, to
216 * ensure that the functions all exist in the PHP version and aren't
225 public static function is_available() {
226 return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );