WordPress.org

Make WordPress Core

Ticket #17251: 17251.2.diff

File 17251.2.diff, 4.9 KB (added by mdawaffe, 7 years ago)
  • wp-includes/class-http.php

     
    2727 *
    2828 * Debugging includes several actions, which pass different variables for debugging the HTTP API.
    2929 *
    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.
    33  *
    3430 * @package WordPress
    3531 * @subpackage HTTP
    3632 * @since 2.7.0
     
    196192        }
    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         *
    201225         * Tests each transport in order to find a transport which matches the request arguements.
     
    216240         * @param array $args Request arguments
    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');
     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.' ) );
    223249
    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;
     250                // Transport claims to support request, Instantate it and give it a whirl.
     251                if ( empty( $transports[$class] ) )
     252                        $transports[$class] = new $class;
    227253
    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;
     254                $response = $transports[$class]->request( $url, $args );
    231255
    232                         // Transport claims to support request, Instantate it and give it a whirl.
    233                         if ( empty( $transports[ $transport ] ) )
    234                                 $transports[ $transport ] = new $class;
     256                do_action( 'http_api_debug', $response, 'response', $class );
    235257
    236                         $response = $transports[ $transport ]->request( $url, $args );
     258                if ( is_wp_error( $response ) )
     259                        return $response;
    237260
    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.') );
     261                return apply_filters( 'http_response', $response, $args, $url );
    247262        }
    248263
    249264        /**
  • wp-includes/http.php

     
    191191        return $response['body'];
    192192}
    193193
    194 ?>
    195  No newline at end of file
     194/**
     195 * Determins if ther 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}