WordPress.org

Make WordPress Core

Changeset 10692


Ignore:
Timestamp:
03/04/2009 07:28:39 AM (11 years ago)
Author:
westi
Message:

First pass of HTTP Proxy support. See #4011 props jacobsantos.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/http.php

    r10642 r10692  
    1414
    1515/**
    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 /**
    14416 * WordPress HTTP Class for managing HTTP Transports and making HTTP requests.
    14517 *
    146  * This class is called for the functionality of making HTTP requests and should
    147  * replace Snoopy functionality, eventually. There is no available functionality
    148  * to add HTTP transport implementations, since most of the HTTP transports are
    149  * added and available for use.
    150  *
    151  * The exception is that cURL is not available as a transport and lacking an
    152  * implementation. It will be added later and should be a patch on the WordPress
    153  * Trac.
    154  *
    155  * There are no properties, because none are needed and for performance reasons.
    156  * Some of the functions are static and while they do have some overhead over
    157  * functions in PHP4, the purpose is maintainability. When PHP5 is finally the
    158  * requirement, it will be easy to add the static keyword to the code. It is not
    159  * as easy to convert a function to a method after enough code uses the old way.
    160  *
    161  * Debugging includes several actions, which pass different variables for
    162  * debugging the HTTP API.
    163  *
    164  * <strong>http_transport_get_debug</strong> - gives working, nonblocking, and
    165  * blocking transports.
    166  *
    167  * <strong>http_transport_post_debug</strong> - gives working, nonblocking, and
    168  * blocking transports.
     18 * This class is called for the functionality of making HTTP requests and should replace Snoopy
     19 * functionality, eventually. There is no available functionality to add HTTP transport
     20 * implementations, since most of the HTTP transports are added and available for use.
     21 *
     22 * The exception is that cURL is not available as a transport and lacking an implementation. It will
     23 * be added later and should be a patch on the WordPress Trac.
     24 *
     25 * There are no properties, because none are needed and for performance reasons. Some of the
     26 * functions are static and while they do have some overhead over functions in PHP4, the purpose is
     27 * maintainability. When PHP5 is finally the requirement, it will be easy to add the static keyword
     28 * to the code. It is not as easy to convert a function to a method after enough code uses the old
     29 * way.
     30 *
     31 * Debugging includes several actions, which pass different variables for debugging the HTTP API.
     32 *
     33 * <strong>http_transport_get_debug</strong> - gives working, nonblocking, and blocking transports.
     34 *
     35 * <strong>http_transport_post_debug</strong> - gives working, nonblocking, and blocking transports.
    16936 *
    17037 * @package WordPress
     
    312179     * Send a HTTP request to a URI.
    313180     *
    314      * The body and headers are part of the arguments. The 'body' argument is
    315      * for the body and will accept either a string or an array. The 'headers'
    316      * argument should be an array, but a string is acceptable. If the 'body'
    317      * argument is an array, then it will automatically be escaped using
    318      * http_build_query().
    319      *
    320      * The only URI that are supported in the HTTP Transport implementation are
    321      * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
    322      * might not know how to handle the send headers. Other protocols are
    323      * unsupported and most likely will fail.
    324      *
    325      * The defaults are 'method', 'timeout', 'redirection', 'httpversion',
    326      * 'blocking' and 'user-agent'.
    327      *
    328      * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports
    329      * technically allow others, but should not be assumed. The 'timeout' is
    330      * used to sent how long the connection should stay open before failing when
    331      * no response. 'redirection' is used to track how many redirects were taken
    332      * and used to sent the amount for other transports, but not all transports
     181     * The body and headers are part of the arguments. The 'body' argument is for the body and will
     182     * accept either a string or an array. The 'headers' argument should be an array, but a string
     183     * is acceptable. If the 'body' argument is an array, then it will automatically be escaped
     184     * using http_build_query().
     185     *
     186     * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS
     187     * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send
     188     * headers. Other protocols are unsupported and most likely will fail.
     189     *
     190     * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and
     191     * 'user-agent'.
     192     *
     193     * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow
     194     * others, but should not be assumed. The 'timeout' is used to sent how long the connection
     195     * should stay open before failing when no response. 'redirection' is used to track how many
     196     * redirects were taken and used to sent the amount for other transports, but not all transports
    333197     * accept setting that value.
    334198     *
    335      * The 'httpversion' option is used to sent the HTTP version and accepted
    336      * values are '1.0', and '1.1' and should be a string. Version 1.1 is not
    337      * supported, because of chunk response. The 'user-agent' option is the
    338      * user-agent and is used to replace the default user-agent, which is
     199     * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and
     200     * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The
     201     * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is
    339202     * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
    340203     *
    341      * 'blocking' is the default, which is used to tell the transport, whether
    342      * it should halt PHP while it performs the request or continue regardless.
    343      * Actually, that isn't entirely correct. Blocking mode really just means
    344      * whether the fread should just pull what it can whenever it gets bytes or
    345      * if it should wait until it has enough in the buffer to read or finishes
    346      * reading the entire content. It doesn't actually always mean that PHP will
    347      * continue going after making the request.
     204     * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP
     205     * while it performs the request or continue regardless. Actually, that isn't entirely correct.
     206     * Blocking mode really just means whether the fread should just pull what it can whenever it
     207     * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading
     208     * the entire content. It doesn't actually always mean that PHP will continue going after making
     209     * the request.
    348210     *
    349211     * @access public
     
    570432        return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
    571433    }
    572    
     434
    573435    /**
    574436     * Takes the arguments for a ::request() and checks for the cookie array.
     
    598460     * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
    599461     *
    600      * Based off the HTTP http_encoding_dechunk function. Does not support
    601      * UTF-8. Does not support returning footer headers. Shouldn't be too
    602      * difficult to support it though.
     462     * Based off the HTTP http_encoding_dechunk function. Does not support UTF-8. Does not support
     463     * returning footer headers. Shouldn't be too difficult to support it though.
    603464     *
    604465     * @todo Add support for footer chunked headers.
     
    652513     * WP_ACCESSABLE_HOSTS will allow additional hosts to go through for requests.
    653514     *
    654      * @since unknown
     515     * @since 2.8.0
    655516     * @link http://core.trac.wordpress.org/ticket/8927 Allow preventing external requests.
    656517     *
     
    694555 * HTTP request method uses fsockopen function to retrieve the url.
    695556 *
    696  * This would be the preferred method, but the fsockopen implementation has the
    697  * most overhead of all the HTTP transport implementations.
     557 * This would be the preferred method, but the fsockopen implementation has the most overhead of all
     558 * the HTTP transport implementations.
    698559 *
    699560 * @package WordPress
     
    752613            }
    753614        } else {
    754             $arrURL['port'] = apply_filters('http_request_port', $arrURL['port']);
    755         }
    756 
    757         // There are issues with the HTTPS and SSL protocols that cause errors
    758         // that can be safely ignored and should be ignored.
     615            $arrURL['port'] = apply_filters('http_request_port', $arrURL['port'], $arrURL['host']);
     616        }
     617
     618        // There are issues with the HTTPS and SSL protocols that cause errors that can be safely
     619        // ignored and should be ignored.
    759620        if ( true === $secure_transport )
    760621            $error_reporting = error_reporting(0);
     
    762623        $startDelay = time();
    763624
    764         if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
    765             $handle = @fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
    766         else
    767             $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
     625        $proxy = new WP_HTTP_Proxy();
     626
     627        if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) {
     628            if( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
     629                $handle = @fsockopen($proxy->host(), $proxy->port(), $iError, $strError, $r['timeout'] );
     630            else
     631                $handle = @fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
     632        }
     633        else {
     634            if( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
     635                $handle = fsockopen($proxy->host(), $proxy->port(), $iError, $strError, $r['timeout'] );
     636            else
     637                $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
     638        }
    768639
    769640        $endDelay = time();
    770641
    771         // If the delay is greater than the timeout then fsockopen should't be
    772         // used, because it will cause a long delay.
     642        // If the delay is greater than the timeout then fsockopen should't be used, because it will
     643        // cause a long delay.
    773644        $elapseDelay = ($endDelay-$startDelay) > $r['timeout'];
    774645        if ( true === $elapseDelay )
     
    778649            return new WP_Error('http_request_failed', $iError . ': ' . $strError);
    779650
    780         // WordPress supports PHP 4.3, which has this function. Removed sanity
    781         // checking for performance reasons.
     651        // WordPress supports PHP 4.3, which has this function. Removed sanity checking for
     652        // performance reasons.
    782653        stream_set_timeout($handle, $r['timeout'] );
    783654
     
    787658        $strHeaders = '';
    788659        $strHeaders .= strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
    789         $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
     660
     661        if( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
     662            $strHeaders .= 'Host: ' . $arrURL['host'] .':'. $arrURL['port'] . "\r\n";
     663        else
     664            $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
    790665
    791666        if( isset($r['user-agent']) )
     
    797672        } else {
    798673            $strHeaders .= $r['headers'];
     674        }
     675
     676        if ( $proxy->use_authentication() ) {
     677            $strHeaders .= $proxy->authentication_header() . "\r\n";
    799678        }
    800679
     
    867746 * HTTP request method uses fopen function to retrieve the url.
    868747 *
    869  * Requires PHP version greater than 4.3.0 for stream support. Does not allow
    870  * for $context support, but should still be okay, to write the headers, before
    871  * getting the response. Also requires that 'allow_url_fopen' to be enabled.
     748 * Requires PHP version greater than 4.3.0 for stream support. Does not allow for $context support,
     749 * but should still be okay, to write the headers, before getting the response. Also requires that
     750 * 'allow_url_fopen' to be enabled.
    872751 *
    873752 * @package WordPress
     
    879758     * Send a HTTP request to a URI using fopen().
    880759     *
    881      * This transport does not support sending of headers and body, therefore
    882      * should not be used in the instances, where there is a body and headers.
     760     * This transport does not support sending of headers and body, therefore should not be used in
     761     * the instances, where there is a body and headers.
    883762     *
    884763     * Notes: Does not support non-blocking mode. Ignores 'redirection' option.
     
    985864 * HTTP request method uses Streams to retrieve the url.
    986865 *
    987  * Requires PHP 5.0+ and uses fopen with stream context. Requires that
    988  * 'allow_url_fopen' PHP setting to be enabled.
     866 * Requires PHP 5.0+ and uses fopen with stream context. Requires that 'allow_url_fopen' PHP setting
     867 * to be enabled.
    989868 *
    990869 * Second preferred method for getting the URL, for PHP 5.
     
    1057936        );
    1058937
     938        $proxy = new WP_HTTP_Proxy();
     939
     940        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
     941            $arrContext['http']['proxy'] = 'tcp://'.$proxy->host().':'.$proxy->port();
     942
     943            // We only support Basic authentication so this will only work if that is what your proxy supports.
     944            if ( $proxy->use_authentication() ) {
     945                $arrContext['http']['header'] .= $proxy->authentication_header() . "\r\n";
     946            }
     947        }
     948
    1059949        if ( ! is_null($r['body']) && ! empty($r['body'] ) )
    1060950            $arrContext['http']['content'] = $r['body'];
     
    1070960            return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
    1071961
    1072         // WordPress supports PHP 4.3, which has this function. Removed sanity
    1073         // checking for performance reasons.
     962        // WordPress supports PHP 4.3, which has this function. Removed sanity checking for
     963        // performance reasons.
    1074964        stream_set_timeout($handle, $r['timeout'] );
    1075965
     
    11231013 * HTTP request method uses HTTP extension to retrieve the url.
    11241014 *
    1125  * Requires the HTTP extension to be installed. This would be the preferred
    1126  * transport since it can handle a lot of the problems that forces the others to
    1127  * use the HTTP version 1.0. Even if PHP 5.2+ is being used, it doesn't mean
    1128  * that the HTTP extension will be enabled.
     1015 * Requires the HTTP extension to be installed. This would be the preferred transport since it can
     1016 * handle a lot of the problems that forces the others to use the HTTP version 1.0. Even if PHP 5.2+
     1017 * is being used, it doesn't mean that the HTTP extension will be enabled.
    11291018 *
    11301019 * @package WordPress
     
    11621051            unset($r['headers']['user-agent']);
    11631052        }
    1164        
     1053
    11651054        // Construct Cookie: header if any cookies are set
    11661055        WP_Http::buildCookieHeader( $r );
     
    11951084        );
    11961085
     1086        // The HTTP extensions offers really easy proxy support.
     1087        $proxy = new WP_HTTP_Proxy();
     1088
     1089        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
     1090            $options['proxyhost'] = $proxy->host();
     1091            $options['proxyport'] = $proxy->port();
     1092            $options['proxytype'] = HTTP_PROXY_HTTP;
     1093
     1094            if ( $proxy->use_authentication() ) {
     1095                $options['proxyauth'] = $proxy->authentication();
     1096                $options['proxyauthtype'] = HTTP_AUTH_BASIC;
     1097            }
     1098        }
     1099
    11971100        if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts
    11981101            $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info);
     
    12831186        }
    12841187
    1285         // Construct Cookie: header if any cookies are set
     1188        // Construct Cookie: header if any cookies are set.
    12861189        WP_Http::buildCookieHeader( $r );
    12871190
    1288         // cURL extension will sometimes fail when the timeout is less than 1 as
    1289         // it may round down to 0, which gives it unlimited timeout.
     1191        // cURL extension will sometimes fail when the timeout is less than 1 as it may round down
     1192        // to 0, which gives it unlimited timeout.
    12901193        if ( $r['timeout'] > 0 && $r['timeout'] < 1 )
    12911194            $r['timeout'] = 1;
    12921195
    12931196        $handle = curl_init();
     1197
     1198        // cURL offers really easy proxy support.
     1199        $proxy = new WP_HTTP_Proxy();
     1200
     1201        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
     1202            curl_setopt( $handle, CURLOPT_HTTPPROXYTUNNEL, true );
     1203
     1204            $isPHP5 = version_compare(PHP_VERSION, '5.0.0', '>=');
     1205
     1206            if ( $isPHP5 ) {
     1207                curl_setopt( $handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
     1208                curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() );
     1209                curl_setopt( $handle, CURLOPT_PROXYPORT, $proxy->port() );
     1210            } else {
     1211                curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() .':'. $proxy->port() );
     1212            }
     1213
     1214            if ( $proxy->use_authentication() ) {
     1215                if ( $isPHP5 )
     1216                    curl_setopt( $handle, CURLOPT_PROXYAUTH, CURLAUTH_BASIC );
     1217
     1218                curl_setopt( $handle, CURLOPT_PROXYUSERPWD, $proxy->authentication() );
     1219            }
     1220        }
    12941221
    12951222        curl_setopt( $handle, CURLOPT_URL, $url);
     
    13351262            curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
    13361263
    1337         // Cookies are not handled by the HTTP API currently. Allow for plugin
    1338         // authors to handle it themselves... Although, it is somewhat pointless
    1339         // without some reference.
     1264        // Cookies are not handled by the HTTP API currently. Allow for plugin authors to handle it
     1265        // themselves... Although, it is somewhat pointless without some reference.
    13401266        do_action_ref_array( 'http_api_curl', array(&$handle) );
    13411267
    1342         // We don't need to return the body, so don't. Just execute request
    1343         // and return.
     1268        // We don't need to return the body, so don't. Just execute request and return.
    13441269        if ( ! $r['blocking'] ) {
    13451270            curl_exec( $handle );
     
    13511276
    13521277        if ( !empty($theResponse) ) {
     1278            $parts = explode("\r\n\r\n", $theResponse);
     1279           
    13531280            $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
    13541281            $theHeaders = trim( substr($theResponse, 0, $headerLength) );
     
    13971324}
    13981325
     1326/**
     1327 * Adds Proxy support to the WordPress HTTP API.
     1328 *
     1329 * There are caveats to proxy support. It requires that defines be made in the wp-config.php file to
     1330 * enable proxy support. There are also a few filters that plugins can hook into for some of the
     1331 * constants.
     1332 *
     1333 * The constants are as follows:
     1334 * <ol>
     1335 * <li>WP_PROXY_HOST - Enable proxy support and host for connecting.</li>
     1336 * <li>WP_PROXY_PORT - Proxy port for connection. No default, must be defined.</li>
     1337 * <li>WP_PROXY_USERNAME - Proxy username, if it requires authentication.</li>
     1338 * <li>WP_PROXY_PASSWORD - Proxy password, if it requires authentication.</li>
     1339 * <li>WP_PROXY_BYPASS_HOSTS - Will prevent the hosts in this list from going through the proxy.
     1340 * You do not need to have localhost and the blog host in this list, because they will not be passed
     1341 * through the proxy.</li>
     1342 * </ol>
     1343 *
     1344 * An example can be as seen below.
     1345 * <code>
     1346 * define('WP_PROXY_HOST', '192.168.84.101');
     1347 * define('WP_PROXY_PORT', '8080');
     1348 * define('WP_PROXY_BYPASS_HOSTS', array('localhost', 'www.example.com'));
     1349 * </code>
     1350 *
     1351 * @link http://core.trac.wordpress.org/ticket/4011 Proxy support ticket in WordPress.
     1352 * @since 2.8
     1353 */
     1354class WP_HTTP_Proxy {
     1355
     1356    function WP_HTTP_Proxy() {
     1357        $this->__construct();
     1358    }
     1359
     1360    function __construct() {
     1361       
     1362    }
     1363
     1364    /**
     1365     * Whether proxy connection should be used.
     1366     *
     1367     * @since 2.8
     1368     * @use WP_PROXY_HOST
     1369     * @use WP_PROXY_PORT
     1370     *
     1371     * @return bool
     1372     */
     1373    function is_enabled() {
     1374        return ( defined('WP_PROXY_HOST') && defined('WP_PROXY_PORT') );
     1375    }
     1376
     1377    /**
     1378     * Whether authentication should be used.
     1379     *
     1380     * @since 2.8
     1381     * @use WP_PROXY_USERNAME
     1382     * @use WP_PROXY_PASSWORD
     1383     *
     1384     * @return bool
     1385     */
     1386    function use_authentication() {
     1387        return ( defined('WP_PROXY_USERNAME') && defined('WP_PROXY_PASSWORD') );
     1388    }
     1389
     1390    /**
     1391     * Retrieve the host for the proxy server.
     1392     *
     1393     * @since 2.8
     1394     *
     1395     * @return string
     1396     */
     1397    function host() {
     1398        if( defined('WP_PROXY_HOST') )
     1399            return WP_PROXY_HOST;
     1400
     1401        return '';
     1402    }
     1403
     1404    /**
     1405     * Retrieve the port for the proxy server.
     1406     *
     1407     * @since 2.8
     1408     *
     1409     * @return string
     1410     */
     1411    function port() {
     1412        if( defined('WP_PROXY_PORT') )
     1413            return WP_PROXY_PORT;
     1414
     1415        return '';
     1416    }
     1417
     1418    /**
     1419     * Retrieve the username for proxy authentication.
     1420     *
     1421     * @since 2.8
     1422     *
     1423     * @return string
     1424     */
     1425    function username() {
     1426        if( defined('WP_PROXY_USERNAME') )
     1427            return WP_PROXY_USERNAME;
     1428
     1429        return '';
     1430    }
     1431
     1432    /**
     1433     * Retrieve the password for proxy authentication.
     1434     *
     1435     * @since 2.8
     1436     *
     1437     * @return string
     1438     */
     1439    function password() {
     1440        if( defined('WP_PROXY_PASSWORD') )
     1441            return WP_PROXY_PASSWORD;
     1442
     1443        return '';
     1444    }
     1445
     1446    /**
     1447     * Retrieve authentication string for proxy authentication.
     1448     *
     1449     * @since 2.8
     1450     *
     1451     * @return string
     1452     */
     1453    function authentication() {
     1454        return $this->username() .':'. $this->password();
     1455    }
     1456
     1457    /**
     1458     * Retrieve header string for proxy authentication.
     1459     *
     1460     * @since 2.8
     1461     *
     1462     * @return string
     1463     */
     1464    function authentication_header() {
     1465        return 'Proxy-Authentication: Basic '. base64_encode( $this->authentication() );
     1466    }
     1467
     1468    /**
     1469     * Whether URL should be sent through the proxy server.
     1470     *
     1471     * We want to keep localhost and the blog URL from being sent through the proxy server, because
     1472     * some proxies can not handle this. We also have the constant available for defining other
     1473     * hosts that won't be sent through the proxy.
     1474     *
     1475     * @uses WP_PROXY_BYPASS_HOSTS
     1476     * @since unknown
     1477     *
     1478     * @param string $uri URI to check.
     1479     * @return bool True, to send through the proxy and false if, the proxy should not be used.
     1480     */
     1481    function send_through_proxy( $uri ) {
     1482        // parse_url() only handles http, https type URLs, and will emit E_WARNING on failure.
     1483        // This will be displayed on blogs, which is not reasonable.
     1484        $check = @parse_url($uri);
     1485
     1486        // Malformed URL, can not process, but this could mean ssl, so let through anyway.
     1487        if( $check === false )
     1488            return true;
     1489
     1490        $home = parse_url( get_bloginfo('site_url') );
     1491
     1492        if ( $uri == 'localhost' || $uri == $home['host'] )
     1493            return false;
     1494
     1495        if ( defined('WP_PROXY_BYPASS_HOSTS') && is_array( WP_PROXY_BYPASS_HOSTS ) && in_array( $check['host'], WP_PROXY_BYPASS_HOSTS ) ) {
     1496            return false;
     1497        }
     1498
     1499        return true;
     1500    }
     1501}
     1502
    13991503
    14001504/**
    14011505 * Internal representation of a single cookie.
    14021506 *
    1403  * Returned cookies are represented using this class, and when cookies are
    1404  * set, if they are not already a WP_Http_Cookie() object, then they are turned
    1405  * into one.
     1507 * Returned cookies are represented using this class, and when cookies are set, if they are not
     1508 * already a WP_Http_Cookie() object, then they are turned into one.
    14061509 *
    14071510 * @todo The WordPress convention is to use underscores instead of camelCase for function and method
     
    15991702
    16001703/**
     1704 * Implementation for deflate and gzip transfer encodings.
     1705 *
     1706 * Includes RFC 1950, RFC 1951, and RFC 1952.
     1707 *
     1708 * @since 2.8
     1709 * @package WordPress
     1710 * @subpackage HTTP
     1711 */
     1712class WP_Http_Encoding {
     1713
     1714    /**
     1715     * Compress raw string using the deflate format.
     1716     *
     1717     * Supports the RFC 1951 standard.
     1718     *
     1719     * @since 2.8
     1720     *
     1721     * @param string $raw String to compress.
     1722     * @param int $level Optional, default is 9. Compression level, 9 is highest.
     1723     * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
     1724     * @return string|bool False on failure.
     1725     */
     1726    function compress( $raw, $level = 9, $supports = null ) {
     1727        return gzdeflate( $raw, $level );
     1728    }
     1729
     1730    /**
     1731     * Decompression of deflated string.
     1732     *
     1733     * Will attempt to decompress using the RFC 1950 standard, and if that fails
     1734     * then the RFC 1951 standard deflate will be attempted. Finally, the RFC
     1735     * 1952 standard gzip decode will be attempted. If all fail, then the
     1736     * original compressed string will be returned.
     1737     *
     1738     * @since 2.8
     1739     *
     1740     * @param string $compressed String to decompress.
     1741     * @param int $length The optional length of the compressed data.
     1742     * @return string|bool False on failure.
     1743     */
     1744    function decompress( $compressed, $length = null ) {
     1745        $decompressed = gzinflate( $compressed );
     1746
     1747        if( false !== $decompressed )
     1748            return $decompressed;
     1749
     1750        $decompressed = gzuncompress( $compressed );
     1751
     1752        if( false !== $decompressed )
     1753            return $decompressed;
     1754
     1755        $decompressed = gzdecode( $compressed );
     1756
     1757        if( false !== $decompressed )
     1758            return $decompressed;
     1759
     1760        return $compressed;
     1761    }
     1762
     1763    /**
     1764     * What encoding types to accept and their priority values.
     1765     *
     1766     * @since 2.8
     1767     *
     1768     * @return string Types of encoding to accept.
     1769     */
     1770    function accept_encoding() {
     1771        $type = array();
     1772        if( function_exists( 'gzinflate' ) )
     1773            $type[] = 'deflate;q=1.0';
     1774
     1775        if( function_exists( 'gzuncompress' ) )
     1776            $type[] = 'compress;q=0.5';
     1777
     1778        if( function_exists( 'gzdecode' ) )
     1779            $type[] = 'gzip;q=0.5';
     1780
     1781        return implode(', ', $type);
     1782    }
     1783
     1784    /**
     1785     * What enconding the content used when it was compressed to send in the headers.
     1786     *
     1787     * @since 2.8
     1788     *
     1789     * @return string Content-Encoding string to send in the header.
     1790     */
     1791    function content_encoding() {
     1792        return 'deflate';
     1793    }
     1794
     1795    /**
     1796     * Whether the content be decoded based on the headers.
     1797     *
     1798     * @since 2.8
     1799     *
     1800     * @param array|string $headers All of the available headers.
     1801     * @return bool
     1802     */
     1803    function should_decode($headers) {
     1804        if( is_array( $headers ) ) {
     1805            if( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
     1806                return true;
     1807        } else if( is_string( $headers ) ) {
     1808            return ( stripos($headers, 'content-encoding:') !== false );
     1809        }
     1810
     1811        return false;
     1812    }
     1813
     1814    /**
     1815     * Whether decompression and compression are supported by the PHP version.
     1816     *
     1817     * Each function is tested instead of checking for the zlib extension, to
     1818     * ensure that the functions all exist in the PHP version and aren't
     1819     * disabled.
     1820     *
     1821     * @since 2.8
     1822     *
     1823     * @return bool
     1824     */
     1825    function is_available() {
     1826        return ( function_exists('gzuncompress') || function_exists('gzdeflate') ||
     1827                 function_exists('gzinflate') );
     1828    }
     1829}
     1830
     1831/**
    16011832 * Returns the initialized WP_Http Object
    16021833 *
Note: See TracChangeset for help on using the changeset viewer.