Make WordPress Core

Changeset 17659


Ignore:
Timestamp:
04/19/2011 04:22:59 AM (14 years ago)
Author:
dd32
Message:

Remove support for the PHP HTTP Extension from WP_HTTP. The PHP HTTP Extension is a wrapper around libcurl and fopen() providing limited configuration and is supported on a minority of servers due to its non-default inclusion. Props sivel. Fixes @16978

File:
1 edited

Legend:

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

    r17616 r17659  
    202202     * Also caches the transport instance to be used later.
    203203     *
    204      * The order for blocking requests is HTTP Extension, cURL, Streams, and finally Fsockopen.
    205      * The order for non-blocking requests is cURL, Streams, Fsockopen() and finally, HTTP Extension.
    206      * The HTTP Extension does not support non-blocking requests, but is included as a final resort.
     204     * The order for blocking requests is cURL, Streams, and finally Fsockopen.
     205     * The order for non-blocking requests is cURL, Streams and Fsockopen().
    207206     *
    208207     * There are currently issues with "localhost" not resolving correctly with DNS. This may cause
     
    221220        static $transports = array();
    222221
    223         $request_order = isset($args['blocking']) && !$args['blocking'] ?
    224                             array('curl', 'streams', 'fsockopen', 'exthttp') : // non-blocking order
    225                             array('exthttp', 'curl', 'streams', 'fsockopen'); // blocking order
     222        $request_order = array('curl', 'streams', 'fsockopen');
    226223
    227224        // Loop over each transport on each HTTP request looking for one which will serve this requests needs
     
    703700            fclose( $stream_handle );
    704701
    705         } else { 
     702        } else {
    706703            while ( ! feof($handle) )
    707704                $strResponse .= fread( $handle, 4096 );
     
    904901            $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
    905902
    906         // Streams does not provide an error code which we can use to see why the request stream stoped. 
     903        // Streams does not provide an error code which we can use to see why the request stream stoped.
    907904        // We can however test to see if a location header is present and return based on that.
    908905        if ( isset($processedHeaders['headers']['location']) && 0 !== $args['_redirection'] )
     
    940937
    941938        return apply_filters( 'use_streams_transport', true, $args );
    942     }
    943 }
    944 
    945 /**
    946  * HTTP request method uses HTTP extension to retrieve the url.
    947  *
    948  * Requires the HTTP extension to be installed. This would be the preferred transport since it can
    949  * handle a lot of the problems that forces the others to use the HTTP version 1.0. Even if PHP 5.2+
    950  * is being used, it doesn't mean that the HTTP extension will be enabled.
    951  *
    952  * @package WordPress
    953  * @subpackage HTTP
    954  * @since 2.7.0
    955  */
    956 class WP_Http_ExtHttp {
    957     /**
    958      * Send a HTTP request to a URI using HTTP extension.
    959      *
    960      * Does not support non-blocking.
    961      *
    962      * @access public
    963      * @since 2.7
    964      *
    965      * @param string $url
    966      * @param str|array $args Optional. Override the defaults.
    967      * @return array 'headers', 'body', 'cookies' and 'response' keys.
    968      */
    969     function request($url, $args = array()) {
    970         $defaults = array(
    971             'method' => 'GET', 'timeout' => 5,
    972             'redirection' => 5, 'httpversion' => '1.0',
    973             'blocking' => true,
    974             'headers' => array(), 'body' => null, 'cookies' => array()
    975         );
    976 
    977         $r = wp_parse_args( $args, $defaults );
    978 
    979         if ( isset($r['headers']['User-Agent']) ) {
    980             $r['user-agent'] = $r['headers']['User-Agent'];
    981             unset($r['headers']['User-Agent']);
    982         } else if ( isset($r['headers']['user-agent']) ) {
    983             $r['user-agent'] = $r['headers']['user-agent'];
    984             unset($r['headers']['user-agent']);
    985         }
    986 
    987         // Construct Cookie: header if any cookies are set
    988         WP_Http::buildCookieHeader( $r );
    989 
    990         switch ( $r['method'] ) {
    991             case 'POST':
    992                 $r['method'] = HTTP_METH_POST;
    993                 break;
    994             case 'HEAD':
    995                 $r['method'] = HTTP_METH_HEAD;
    996                 break;
    997             case 'PUT':
    998                 $r['method'] =  HTTP_METH_PUT;
    999                 break;
    1000             case 'GET':
    1001             default:
    1002                 $r['method'] = HTTP_METH_GET;
    1003         }
    1004 
    1005         $arrURL = parse_url($url);
    1006 
    1007         if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] )
    1008             $url = preg_replace('|^' . preg_quote($arrURL['scheme'], '|') . '|', 'http', $url);
    1009 
    1010         $is_local = isset($args['local']) && $args['local'];
    1011         $ssl_verify = isset($args['sslverify']) && $args['sslverify'];
    1012         if ( $is_local )
    1013             $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
    1014         elseif ( ! $is_local )
    1015             $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
    1016 
    1017         $r['timeout'] = (int) ceil( $r['timeout'] );
    1018 
    1019         $options = array(
    1020             'timeout' => $r['timeout'],
    1021             'connecttimeout' => $r['timeout'],
    1022             'redirect' => $r['redirection'],
    1023             'useragent' => $r['user-agent'],
    1024             'headers' => $r['headers'],
    1025             'ssl' => array(
    1026                 'verifypeer' => $ssl_verify,
    1027                 'verifyhost' => $ssl_verify
    1028             )
    1029         );
    1030 
    1031         // The HTTP extensions offers really easy proxy support.
    1032         $proxy = new WP_HTTP_Proxy();
    1033 
    1034         if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
    1035             $options['proxyhost'] = $proxy->host();
    1036             $options['proxyport'] = $proxy->port();
    1037             $options['proxytype'] = HTTP_PROXY_HTTP;
    1038 
    1039             if ( $proxy->use_authentication() ) {
    1040                 $options['proxyauth'] = $proxy->authentication();
    1041                 $options['proxyauthtype'] = HTTP_AUTH_ANY;
    1042             }
    1043         }
    1044 
    1045         if ( !WP_DEBUG ) //Emits warning level notices for max redirects and timeouts
    1046             $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info);
    1047         else
    1048             $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); //Emits warning level notices for max redirects and timeouts
    1049 
    1050         // Error may still be set, Response may return headers or partial document, and error
    1051         // contains a reason the request was aborted, eg, timeout expired or max-redirects reached.
    1052         if ( false === $strResponse || ! empty($info['error']) )
    1053             return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']);
    1054 
    1055         if ( ! $r['blocking'] )
    1056             return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
    1057 
    1058         $headers_body = WP_HTTP::processResponse($strResponse);
    1059         $theHeaders = $headers_body['headers'];
    1060         $theBody = $headers_body['body'];
    1061         unset($headers_body);
    1062 
    1063         $theHeaders = WP_Http::processHeaders($theHeaders);
    1064 
    1065         if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] ) {
    1066             if ( !WP_DEBUG )
    1067                 $theBody = @http_chunked_decode($theBody);
    1068             else
    1069                 $theBody = http_chunked_decode($theBody);
    1070         }
    1071 
    1072         if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) )
    1073             $theBody = http_inflate( $theBody );
    1074 
    1075         if ( $r['stream'] ) {
    1076             if ( !WP_DEBUG )
    1077                 $stream_handle = @fopen( $r['filename'], 'w+' );
    1078             else
    1079                 $stream_handle = fopen( $r['filename'], 'w+' );
    1080 
    1081             if ( ! $stream_handle )
    1082                 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
    1083 
    1084             fwrite( $stream_handle, $theBody );
    1085             fclose( $stream_handle );
    1086             $theBody = '';
    1087         }
    1088 
    1089         $theResponse = array();
    1090         $theResponse['code'] = $info['response_code'];
    1091         $theResponse['message'] = get_status_header_desc($info['response_code']);
    1092 
    1093         return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] );
    1094     }
    1095 
    1096     /**
    1097      * Whether this class can be used for retrieving an URL.
    1098      *
    1099      * @static
    1100      * @since 2.7.0
    1101      *
    1102      * @return boolean False means this class can not be used, true means it can.
    1103      */
    1104     function test($args = array()) {
    1105         return apply_filters('use_http_extension_transport', function_exists('http_request'), $args );
    1106939    }
    1107940}
     
    13421175 *
    13431176 * Please note that only BASIC authentication is supported by most transports.
    1344  * cURL and the PHP HTTP Extension MAY support more methods (such as NTLM authentication) depending on your environment.
     1177 * cURL MAY support more methods (such as NTLM authentication) depending on your environment.
    13451178 *
    13461179 * The constants are as follows:
Note: See TracChangeset for help on using the changeset viewer.