WP_Http_Encoding

Core class used to implement deflate and gzip transfer encoding support for HTTP requests.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-http-encoding.php  
  1. class WP_Http_Encoding { 
  2.  
  3. /** 
  4. * Compress raw string using the deflate format. 
  5. * Supports the RFC 1951 standard. 
  6. * @since 2.8.0 
  7. * @static 
  8. * @param string $raw String to compress. 
  9. * @param int $level Optional, default is 9. Compression level, 9 is highest. 
  10. * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports. 
  11. * @return string|false False on failure. 
  12. */ 
  13. public static function compress( $raw, $level = 9, $supports = null ) { 
  14. return gzdeflate( $raw, $level ); 
  15.  
  16. /** 
  17. * Decompression of deflated string. 
  18. * Will attempt to decompress using the RFC 1950 standard, and if that fails 
  19. * then the RFC 1951 standard deflate will be attempted. Finally, the RFC 
  20. * 1952 standard gzip decode will be attempted. If all fail, then the 
  21. * original compressed string will be returned. 
  22. * @since 2.8.0 
  23. * @static 
  24. * @param string $compressed String to decompress. 
  25. * @param int $length The optional length of the compressed data. 
  26. * @return string|bool False on failure. 
  27. */ 
  28. public static function decompress( $compressed, $length = null ) { 
  29.  
  30. if ( empty($compressed) ) 
  31. return $compressed; 
  32.  
  33. if ( false !== ( $decompressed = @gzinflate( $compressed ) ) ) 
  34. return $decompressed; 
  35.  
  36. if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) ) 
  37. return $decompressed; 
  38.  
  39. if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) ) 
  40. return $decompressed; 
  41.  
  42. if ( function_exists('gzdecode') ) { 
  43. $decompressed = @gzdecode( $compressed ); 
  44.  
  45. if ( false !== $decompressed ) 
  46. return $decompressed; 
  47.  
  48. return $compressed; 
  49.  
  50. /** 
  51. * Decompression of deflated string while staying compatible with the majority of servers. 
  52. * Certain Servers will return deflated data with headers which PHP's gzinflate() 
  53. * function cannot handle out of the box. The following function has been created from 
  54. * various snippets on the gzinflate() PHP documentation. 
  55. * Warning: Magic numbers within. Due to the potential different formats that the compressed 
  56. * data may be returned in, some "magic offsets" are needed to ensure proper decompression 
  57. * takes place. For a simple progmatic way to determine the magic offset in use, see: 
  58. * https://core.trac.wordpress.org/ticket/18273 
  59. * @since 2.8.1 
  60. * @link https://core.trac.wordpress.org/ticket/18273 
  61. * @link https://secure.php.net/manual/en/function.gzinflate.php#70875 
  62. * @link https://secure.php.net/manual/en/function.gzinflate.php#77336 
  63. * @static 
  64. * @param string $gzData String to decompress. 
  65. * @return string|bool False on failure. 
  66. */ 
  67. public static function compatible_gzinflate($gzData) { 
  68.  
  69. // Compressed data might contain a full header, if so strip it for gzinflate(). 
  70. if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) { 
  71. $i = 10; 
  72. $flg = ord( substr($gzData, 3, 1) ); 
  73. if ( $flg > 0 ) { 
  74. if ( $flg & 4 ) { 
  75. list($xlen) = unpack('v', substr($gzData, $i, 2) ); 
  76. $i = $i + 2 + $xlen; 
  77. if ( $flg & 8 ) 
  78. $i = strpos($gzData, "\0", $i) + 1; 
  79. if ( $flg & 16 ) 
  80. $i = strpos($gzData, "\0", $i) + 1; 
  81. if ( $flg & 2 ) 
  82. $i = $i + 2; 
  83. $decompressed = @gzinflate( substr($gzData, $i, -8) ); 
  84. if ( false !== $decompressed ) 
  85. return $decompressed; 
  86.  
  87. // Compressed data from java.util.zip.Deflater amongst others. 
  88. $decompressed = @gzinflate( substr($gzData, 2) ); 
  89. if ( false !== $decompressed ) 
  90. return $decompressed; 
  91.  
  92. return false; 
  93.  
  94. /** 
  95. * What encoding types to accept and their priority values. 
  96. * @since 2.8.0 
  97. * @static 
  98. * @param string $url 
  99. * @param array $args 
  100. * @return string Types of encoding to accept. 
  101. */ 
  102. public static function accept_encoding( $url, $args ) { 
  103. $type = array(); 
  104. $compression_enabled = self::is_available(); 
  105.  
  106. if ( ! $args['decompress'] ) // Decompression specifically disabled. 
  107. $compression_enabled = false; 
  108. elseif ( $args['stream'] ) // Disable when streaming to file. 
  109. $compression_enabled = false; 
  110. elseif ( isset( $args['limit_response_size'] ) ) // If only partial content is being requested, we won't be able to decompress it. 
  111. $compression_enabled = false; 
  112.  
  113. if ( $compression_enabled ) { 
  114. if ( function_exists( 'gzinflate' ) ) 
  115. $type[] = 'deflate;q=1.0'; 
  116.  
  117. if ( function_exists( 'gzuncompress' ) ) 
  118. $type[] = 'compress;q=0.5'; 
  119.  
  120. if ( function_exists( 'gzdecode' ) ) 
  121. $type[] = 'gzip;q=0.5'; 
  122.  
  123. /** 
  124. * Filters the allowed encoding types. 
  125. * @since 3.6.0 
  126. * @param array $type Encoding types allowed. Accepts 'gzinflate',  
  127. * 'gzuncompress', 'gzdecode'. 
  128. * @param string $url URL of the HTTP request. 
  129. * @param array $args HTTP request arguments. 
  130. */ 
  131. $type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args ); 
  132.  
  133. return implode(', ', $type); 
  134.  
  135. /** 
  136. * What encoding the content used when it was compressed to send in the headers. 
  137. * @since 2.8.0 
  138. * @static 
  139. * @return string Content-Encoding string to send in the header. 
  140. */ 
  141. public static function content_encoding() { 
  142. return 'deflate'; 
  143.  
  144. /** 
  145. * Whether the content be decoded based on the headers. 
  146. * @since 2.8.0 
  147. * @static 
  148. * @param array|string $headers All of the available headers. 
  149. * @return bool 
  150. */ 
  151. public static function should_decode($headers) { 
  152. if ( is_array( $headers ) ) { 
  153. if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) ) 
  154. return true; 
  155. } elseif ( is_string( $headers ) ) { 
  156. return ( stripos($headers, 'content-encoding:') !== false ); 
  157.  
  158. return false; 
  159.  
  160. /** 
  161. * Whether decompression and compression are supported by the PHP version. 
  162. * Each function is tested instead of checking for the zlib extension, to 
  163. * ensure that the functions all exist in the PHP version and aren't 
  164. * disabled. 
  165. * @since 2.8.0 
  166. * @static 
  167. * @return bool 
  168. */ 
  169. public static function is_available() { 
  170. return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );