-class WP_Http {
-
- /**
- * PHP4 style Constructor - Calls PHP5 Style Constructor
- *
- * @since 2.7
- * @return WP_Http
- */
- function WP_Http() {
- $this->__construct();
- }
-
- /**
- * PHP5 style Constructor - Setup available transport if not available.
- *
- * PHP4 does not have the 'self' keyword and since WordPress supports PHP4,
- * the class needs to be used for the static call.
- *
- * The transport are setup to save time. This should only be called once, so
- * the overhead should be fine.
- *
- * @since 2.7
- * @return WP_Http
- */
- function __construct() {
- WP_Http::_getTransport();
- WP_Http::_postTransport();
- }
-
- /**
- * Tests the WordPress HTTP objects for an object to use and returns it.
- *
- * Tests all of the objects and returns the object that passes. Also caches
- * that object to be used later.
- *
- * The order for the GET/HEAD requests are Streams, HTTP Extension, Fopen,
- * and finally Fsockopen. fsockopen() is used last, because it has the most
- * overhead in its implementation. There isn't any real way around it, since
- * redirects have to be supported, much the same way the other transports
- * also handle redirects.
- *
- * There are currently issues with "localhost" not resolving correctly with
- * DNS. This may cause an error "failed to open stream: A connection attempt
- * failed because the connected party did not properly respond after a
- * period of time, or established connection failed because connected host
- * has failed to respond."
- *
- * @since 2.7
- * @access private
- *
- * @param array $args Request args, default us an empty array
- * @return object|null Null if no transports are available, HTTP transport object.
- */
- function &_getTransport( $args = array() ) {
- static $working_transport, $blocking_transport, $nonblocking_transport;
-
- if ( is_null($working_transport) ) {
- if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
- $working_transport['exthttp'] = new WP_Http_ExtHttp();
- $blocking_transport[] = &$working_transport['exthttp'];
- } else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) ) {
- $working_transport['curl'] = new WP_Http_Curl();
- $blocking_transport[] = &$working_transport['curl'];
- } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
- $working_transport['streams'] = new WP_Http_Streams();
- $blocking_transport[] = &$working_transport['streams'];
- } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) ) {
- $working_transport['fopen'] = new WP_Http_Fopen();
- $blocking_transport[] = &$working_transport['fopen'];
- } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
- $working_transport['fsockopen'] = new WP_Http_Fsockopen();
- $blocking_transport[] = &$working_transport['fsockopen'];
- }
-
- foreach ( array('curl', 'streams', 'fopen', 'fsockopen', 'exthttp') as $transport ) {
- if ( isset($working_transport[$transport]) )
- $nonblocking_transport[] = &$working_transport[$transport];
- }
- }
-
- if ( isset($args['blocking']) && !$args['blocking'] )
- return $nonblocking_transport;
- else
- return $blocking_transport;
- }
-
- /**
- * Tests the WordPress HTTP objects for an object to use and returns it.
- *
- * Tests all of the objects and returns the object that passes. Also caches
- * that object to be used later. This is for posting content to a URL and
- * is used when there is a body. The plain Fopen Transport can not be used
- * to send content, but the streams transport can. This is a limitation that
- * is addressed here, by just not including that transport.
- *
- * @since 2.7
- * @access private
- *
- * @param array $args Request args, default us an empty array
- * @return object|null Null if no transports are available, HTTP transport object.
- */
- function &_postTransport( $args = array() ) {
- static $working_transport, $blocking_transport, $nonblocking_transport;
-
- if ( is_null($working_transport) ) {
- if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
- $working_transport['exthttp'] = new WP_Http_ExtHttp();
- $blocking_transport[] = &$working_transport['exthttp'];
- } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
- $working_transport['streams'] = new WP_Http_Streams();
- $blocking_transport[] = &$working_transport['streams'];
- } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
- $working_transport['fsockopen'] = new WP_Http_Fsockopen();
- $blocking_transport[] = &$working_transport['fsockopen'];
- }
-
- foreach ( array('streams', 'fsockopen', 'exthttp') as $transport ) {
- if ( isset($working_transport[$transport]) )
- $nonblocking_transport[] = &$working_transport[$transport];
- }
- }
-
- if ( isset($args['blocking']) && !$args['blocking'] )
- return $nonblocking_transport;
- else
- return $blocking_transport;
- }
-
- /**
- * Send a HTTP request to a URI.
- *
- * The body and headers are part of the arguments. The 'body' argument is
- * for the body and will accept either a string or an array. The 'headers'
- * argument should be an array, but a string is acceptable. If the 'body'
- * argument is an array, then it will automatically be escaped using
- * http_build_query().
- *
- * The only URI that are supported in the HTTP Transport implementation are
- * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
- * might not know how to handle the send headers. Other protocols are
- * unsupported and most likely will fail.
- *
- * The defaults are 'method', 'timeout', 'redirection', 'httpversion',
- * 'blocking' and 'user-agent'.
- *
- * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports
- * technically allow others, but should not be assumed. The 'timeout' is
- * used to sent how long the connection should stay open before failing when
- * no response. 'redirection' is used to track how many redirects were taken
- * and used to sent the amount for other transports, but not all transports
- * accept setting that value.
- *
- * The 'httpversion' option is used to sent the HTTP version and accepted
- * values are '1.0', and '1.1' and should be a string. Version 1.1 is not
- * supported, because of chunk response. The 'user-agent' option is the
- * user-agent and is used to replace the default user-agent, which is
- * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
- *
- * 'blocking' is the default, which is used to tell the transport, whether
- * it should halt PHP while it performs the request or continue regardless.
- * Actually, that isn't entirely correct. Blocking mode really just means
- * whether the fread should just pull what it can whenever it gets bytes or
- * if it should wait until it has enough in the buffer to read or finishes
- * reading the entire content. It doesn't actually always mean that PHP will
- * continue going after making the request.
- *
- * @access public
- * @since 2.7
- *
- * @param string $url URI resource.
- * @param str|array $args Optional. Override the defaults.
- * @return boolean
- */
- function request( $url, $args = array() ) {
- global $wp_version;
-
- $defaults = array(
- 'method' => 'GET',
- 'timeout' => apply_filters( 'http_request_timeout', 5),
- 'redirection' => apply_filters( 'http_request_redirection_count', 5),
- 'httpversion' => apply_filters( 'http_request_version', '1.0'),
- 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ),
- 'blocking' => true,
- 'headers' => array(), 'body' => null
- );