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


git-svn-id: http://svn.automattic.com/wordpress/trunk@17914 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2011-05-13 09:56:59 +00:00
parent 799e614e68
commit 81cd0218ec
2 changed files with 71 additions and 26 deletions

View File

@ -27,10 +27,6 @@
* *
* Debugging includes several actions, which pass different variables for debugging the HTTP API. * Debugging includes several actions, which pass different variables for debugging the HTTP API.
* *
* <strong>http_transport_get_debug</strong> - gives working, nonblocking, and blocking transports.
*
* <strong>http_transport_post_debug</strong> - gives working, nonblocking, and blocking transports.
*
* @package WordPress * @package WordPress
* @subpackage HTTP * @subpackage HTTP
* @since 2.7.0 * @since 2.7.0
@ -195,6 +191,34 @@ class WP_Http {
return $this->_dispatch_request($url, $r); return $this->_dispatch_request($url, $r);
} }
/**
* Tests which transports are capabable of supporting the request.
*
* @since 3.2.0
* @access private
*
* @param array $args Request arguments
* @param string $url URL to Request
*
* @return string|false Class name for the first transport that claims to support the request. False if no transport claims to support the request.
*/
public function _get_first_available_transport( $args, $url = null ) {
$request_order = array( 'curl', 'streams', 'fsockopen' );
// Loop over each transport on each HTTP request looking for one which will serve this request's needs
foreach ( $request_order as $transport ) {
$class = 'WP_HTTP_' . $transport;
// Check to see if this transport is a possibility, calls the transport statically
if ( !call_user_func( array( $class, 'test' ), $args, $url ) )
continue;
return $class;
}
return false;
}
/** /**
* Dispatches a HTTP request to a supporting transport. * Dispatches a HTTP request to a supporting transport.
* *
@ -216,24 +240,18 @@ class WP_Http {
* @param array $args Request arguments * @param array $args Request arguments
* @return array|object Array containing 'headers', 'body', 'response', 'cookies'. A WP_Error instance upon error * @return array|object Array containing 'headers', 'body', 'response', 'cookies'. A WP_Error instance upon error
*/ */
private function _dispatch_request($url, $args) { private function _dispatch_request( $url, $args ) {
static $transports = array(); static $transports = array();
$request_order = array('curl', 'streams', 'fsockopen'); $class = $this->_get_first_available_transport( $args, $url );
if ( !$class )
// Loop over each transport on each HTTP request looking for one which will serve this requests needs return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
foreach ( $request_order as $transport ) {
$class = 'WP_HTTP_' . $transport;
// Check to see if this transport is a possibility, calls the transport statically
if ( ! call_user_func( array($class, 'test'), $args, $url) )
continue;
// Transport claims to support request, Instantate it and give it a whirl. // Transport claims to support request, Instantate it and give it a whirl.
if ( empty( $transports[ $transport ] ) ) if ( empty( $transports[$class] ) )
$transports[ $transport ] = new $class; $transports[$class] = new $class;
$response = $transports[ $transport ]->request( $url, $args ); $response = $transports[$class]->request( $url, $args );
do_action( 'http_api_debug', $response, 'response', $class ); do_action( 'http_api_debug', $response, 'response', $class );
@ -243,9 +261,6 @@ class WP_Http {
return apply_filters( 'http_response', $response, $args, $url ); return apply_filters( 'http_response', $response, $args, $url );
} }
return new WP_Error('http_failure', __('There are no HTTP transports available which can complete the requested request.') );
}
/** /**
* Uses the POST HTTP method. * Uses the POST HTTP method.
* *

View File

@ -191,4 +191,34 @@ function wp_remote_retrieve_body(&$response) {
return $response['body']; return $response['body'];
} }
?> /**
* Determins if there is an HTTP Transport that can process this request.
*
* @since 3.2.0
*
* @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array.
* @param string $url Optional. If given, will check if the URL requires SSL and adds that requirement to the capabilities array.
*
* @return bool
*/
function wp_http_supports( $capabilities = array(), $url = null ) {
$objFetchSite = _wp_http_get_object();
$capabilities = wp_parse_args( $capabilities );
$count = count( $capabilities );
// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
}
if ( $url && !isset( $capabilities['ssl'] ) ) {
$scheme = parse_url( $url, PHP_URL_SCHEME );
if ( 'https' == $scheme || 'ssl' == $scheme ) {
$capabilities['ssl'] = true;
}
}
return (bool) $objFetchSite->_get_first_available_transport( $capabilities );
}