Make WordPress Core

Ticket #8674: 8674.diff

File 8674.diff, 5.7 KB (added by jacobsantos, 15 years ago)

Add Gzip support to the HTTP API.

  • http.php

     
    1313 */
    1414
    1515/**
     16 * Interface for the implementations of compression and decompression in PHP.
     17 *
     18 * This class will be checked for when adding classes that can be implemented.
     19 *
     20 * @abstract
     21 * @since unknown
     22 */
     23class WP_Http_Deflate_Interface {
     24
     25        /**
     26         * Child classes should compress strings in this method.
     27         *
     28         * @since unknown
     29         *
     30         * @param string $raw Raw string to compress.
     31         * @param int $level Optional, default is 9. Level of compression, usually 0 (none) through 9 (most).
     32         * @return string
     33         */
     34        function compress( $raw, $level = 9 ) {
     35                trigger_error('Not implemented!', E_USER_FATAL);
     36        }
     37
     38        /**
     39         * Decompress string that was compressed by the compress method.
     40         *
     41         * @since unknown
     42         *
     43         * @param string $compressed
     44         * @return string
     45         */
     46        function decompress( $compressed ) {
     47                trigger_error('Not implemented!', E_USER_FATAL);
     48        }
     49
     50        /**
     51         * Child classes should return the transfer encoding types that are supported.
     52         *
     53         * @since unknown
     54         * @return array
     55         */
     56        function accept() {
     57                trigger_error('Not implemented!', E_USER_FATAL);
     58        }
     59
     60        /**
     61         * Child classes should return the transfer encoding type if used.
     62         *
     63         * @since unknown
     64         * @return string
     65         */
     66        function type() {
     67                trigger_error('Not implemented!', E_USER_FATAL);
     68        }
     69
     70        /**
     71         * Child classes should return whether the class can be used.
     72         *
     73         * @since unknown
     74         * @return bool
     75         */
     76        function is_available() {
     77                trigger_error('Not implemented!', E_USER_FATAL);
     78        }
     79
     80}
     81
     82/**
     83 * Decompression implementation for deflate transfer encodings.
     84 *
     85 * Includes both RFC 1950 and RFC 1951.
     86 *
     87 * @since unknown
     88 * @uses WP_Http_Deflate_Interface Extends class.
     89 */
     90class WP_Http_Deflate extends WP_Http_Deflate_Interface {
     91
     92        /**
     93         * Compress raw string using the deflate format.
     94         *
     95         * Supports the RFC 1950 standard.
     96         *
     97         * @since unknown
     98         *
     99         * @param string $raw String to compress.
     100         * @param int $level Optional, default is 9. Compression level, 9 is highest.
     101         * @return string|bool False on failure.
     102         */
     103        function compress( $raw, $level = 9 ) {
     104                return gzcompress( $raw, $level );
     105        }
     106
     107        /**
     108         * Decompression of deflated string.
     109         *
     110         * Will attempt to decompress using the RFC 1950 standard, and if that fails
     111         * then the RFC 1951 standard deflate will be attempted.
     112         *
     113         * @since unknown
     114         *
     115         * @param string $compressed String to decompress.
     116         * @return string|bool False on failure.
     117         */
     118        function decompress( $compressed ) {
     119                $decompressed = gzuncompress( $text );
     120
     121                if( false === $decompressed )
     122                        return gzinflate( $text );
     123
     124                return $decompressed;
     125        }
     126
     127        /**
     128         * Supported transfer encoding types.
     129         *
     130         * @since unknown
     131         *
     132         * @return array
     133         */
     134        function accept() {
     135                return array( 'compress', 'x-compress', 'deflate' )
     136        }
     137
     138        /**
     139         * The accept encoding type, if supported.
     140         *
     141         * @since unknown
     142         *
     143         * @return string
     144         */
     145        function type() {
     146                return 'deflate';
     147        }
     148
     149        /**
     150         * Whether decompression and compression are supported by the PHP version.
     151         *
     152         * Each function is tested instead of checking for the zlib extension, to
     153         * ensure that the functions all exist in the PHP version and aren't
     154         * disabled.
     155         *
     156         * @since unknown
     157         *
     158         * @return bool
     159         */
     160        function is_available() {
     161                return ( function_exists('gzuncompress') && function_exists('gzcompress') &&
     162                                 function_exists('gzinflate') );
     163        }
     164
     165}
     166
     167/**
     168 * Decompression implementation for Gzip transfer encodings.
     169 *
     170 * For RFC 1952.
     171 *
     172 * @since unknown
     173 * @uses WP_Http_Deflate_Interface Extends class.
     174 */
     175class WP_Http_Gzip extends WP_Http_Deflate_Interface {
     176
     177        /**
     178         * Compress raw string using the gzip format.
     179         *
     180         * Supports the RFC 1952 standard.
     181         *
     182         * @since unknown
     183         *
     184         * @param string $raw String to compress.
     185         * @param int $level Optional, default is 9. Compression level, 9 is highest.
     186         * @return string|bool False on failure.
     187         */
     188        function compress( $raw, $level = 9 ) {
     189                return gzencode( $raw, $level );
     190        }
     191
     192        /**
     193         * Decompression of gzip string.
     194         *
     195         * Supports the RFC 1952 standard.
     196         *
     197         * @since unknown
     198         *
     199         * @param string $compressed String to decompress.
     200         * @return string|bool False on failure.
     201         */
     202        function decompress( $compressed ) {
     203                return gzdecode( $text );
     204        }
     205
     206        /**
     207         * Supported transfer encoding types.
     208         *
     209         * @since unknown
     210         *
     211         * @return array
     212         */
     213        function accept() {
     214                return array( 'gzip', 'x-gzip' )
     215        }
     216
     217        /**
     218         * The accept encoding type, if supported.
     219         *
     220         * @since unknown
     221         *
     222         * @return string
     223         */
     224        function type() {
     225                return 'gzip';
     226        }
     227
     228        /**
     229         * Whether decompression and compression are supported by the PHP version.
     230         *
     231         * Each function is tested instead of checking for the zlib extension, to
     232         * ensure that the functions all exist in the PHP version and aren't
     233         * disabled.
     234         *
     235         * @since unknown
     236         *
     237         * @return bool
     238         */
     239        function is_available() {
     240                return ( function_exists('gzencode') && function_exists('gzdecode') );
     241        }
     242
     243}
     244
     245/**
    16246 * WordPress HTTP Class for managing HTTP Transports and making HTTP requests.
    17247 *
    18248 * This class is called for the functionality of making HTTP requests and should
     
    217447                        'httpversion' => apply_filters( 'http_request_version', '1.0'),
    218448                        'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ),
    219449                        'blocking' => true,
    220                         'headers' => array(), 'body' => null
     450                        'headers' => array(),
     451                        'body' => null,
     452                        'compress' => false
    221453                );
    222454
    223455                $r = wp_parse_args( $args, $defaults );