Make WordPress Core

Changeset 17914


Ignore:
Timestamp:
05/13/2011 09:56:59 AM (13 years ago)
Author:
westi
Message:

Introduce wp_http_supports as a much less hacky replacement for the http_transport_(get|post)_debug hooks that plugins could have
been using to detect if things like ssl requests were working.
See #17251 props mdawaffe

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r17771 r17914  
    2727 *
    2828 * Debugging includes several actions, which pass different variables for debugging the HTTP API.
    29  *
    30  * <strong>http_transport_get_debug</strong> - gives working, nonblocking, and blocking transports.
    31  *
    32  * <strong>http_transport_post_debug</strong> - gives working, nonblocking, and blocking transports.
    3329 *
    3430 * @package WordPress
     
    197193
    198194    /**
     195     * Tests which transports are capabable of supporting the request.
     196     *
     197     * @since 3.2.0
     198     * @access private
     199     *
     200     * @param array $args Request arguments
     201     * @param string $url URL to Request
     202     *
     203     * @return string|false Class name for the first transport that claims to support the request.  False if no transport claims to support the request.
     204     */
     205    public function _get_first_available_transport( $args, $url = null ) {
     206        $request_order = array( 'curl', 'streams', 'fsockopen' );
     207
     208        // Loop over each transport on each HTTP request looking for one which will serve this request's needs
     209        foreach ( $request_order as $transport ) {
     210            $class = 'WP_HTTP_' . $transport;
     211
     212            // Check to see if this transport is a possibility, calls the transport statically
     213            if ( !call_user_func( array( $class, 'test' ), $args, $url ) )
     214                continue;
     215
     216            return $class;
     217        }
     218
     219        return false;
     220    }
     221
     222    /**
    199223     * Dispatches a HTTP request to a supporting transport.
    200224     *
     
    217241     * @return array|object Array containing 'headers', 'body', 'response', 'cookies'. A WP_Error instance upon error
    218242     */
    219     private function _dispatch_request($url, $args) {
     243    private function _dispatch_request( $url, $args ) {
    220244        static $transports = array();
    221245
    222         $request_order = array('curl', 'streams', 'fsockopen');
    223 
    224         // Loop over each transport on each HTTP request looking for one which will serve this requests needs
    225         foreach ( $request_order as $transport ) {
    226             $class = 'WP_HTTP_' . $transport;
    227 
    228             // Check to see if this transport is a possibility, calls the transport statically
    229             if ( ! call_user_func( array($class, 'test'), $args, $url) )
    230                 continue;
    231 
    232             // Transport claims to support request, Instantate it and give it a whirl.
    233             if ( empty( $transports[ $transport ] ) )
    234                 $transports[ $transport ] = new $class;
    235 
    236             $response = $transports[ $transport ]->request( $url, $args );
    237 
    238             do_action( 'http_api_debug', $response, 'response', $class );
    239 
    240             if ( is_wp_error( $response ) )
    241                 return $response;
    242 
    243             return apply_filters( 'http_response', $response, $args, $url );
    244         }
    245 
    246         return new WP_Error('http_failure', __('There are no HTTP transports available which can complete the requested request.') );
     246        $class = $this->_get_first_available_transport( $args, $url );
     247        if ( !$class )
     248            return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
     249
     250        // Transport claims to support request, Instantate it and give it a whirl.
     251        if ( empty( $transports[$class] ) )
     252            $transports[$class] = new $class;
     253
     254        $response = $transports[$class]->request( $url, $args );
     255
     256        do_action( 'http_api_debug', $response, 'response', $class );
     257
     258        if ( is_wp_error( $response ) )
     259            return $response;
     260
     261        return apply_filters( 'http_response', $response, $args, $url );
    247262    }
    248263
  • trunk/wp-includes/http.php

    r14079 r17914  
    192192}
    193193
    194 ?>
     194/**
     195 * Determins if there is an HTTP Transport that can process this request.
     196 *
     197 * @since 3.2.0
     198 *
     199 * @param array  $capabilities Array of capabilities to test or a wp_remote_request() $args array.
     200 * @param string $url Optional.  If given, will check if the URL requires SSL and adds that requirement to the capabilities array.
     201 *
     202 * @return bool
     203 */
     204function wp_http_supports( $capabilities = array(), $url = null ) {
     205    $objFetchSite = _wp_http_get_object();
     206
     207    $capabilities = wp_parse_args( $capabilities );
     208
     209    $count = count( $capabilities );
     210
     211    // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
     212    if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
     213        $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
     214    }
     215
     216    if ( $url && !isset( $capabilities['ssl'] ) ) {
     217        $scheme = parse_url( $url, PHP_URL_SCHEME );
     218        if ( 'https' == $scheme || 'ssl' == $scheme ) {
     219            $capabilities['ssl'] = true;
     220        }
     221    }
     222
     223    return (bool) $objFetchSite->_get_first_available_transport( $capabilities );
     224}
Note: See TracChangeset for help on using the changeset viewer.