]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-http.php
Wordpress 3.3.1-scripts
[autoinstalls/wordpress.git] / wp-includes / class-http.php
index d913d6dbba7a05e8c2611cccdfc38ffc2edc639f..a68711c8cd95fa1f8a4b96482c45d9fcf6716eaa 100644 (file)
  *
  * 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
  * @subpackage HTTP
  * @since 2.7.0
  */
 class WP_Http {
 
-       /**
-        * PHP4 style Constructor - Calls PHP5 Style Constructor
-        *
-        * @since 2.7.0
-        * @return WP_Http
-        */
-       function WP_Http() {
-               $this->__construct();
-       }
-
-       /**
-        * PHP5 style Constructor - Set up 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 set up to save time and will only be created
-        * once. This class can be created many times without having to go through the step of finding
-        * which transports are available.
-        *
-        * @since 2.7.0
-        * @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 HTTP Extension, cURL, Streams, 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 [the]
-        * connected host has failed to respond."
-        *
-        * @since 2.7.0
-        * @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($args) ) {
-                               $working_transport['exthttp'] = new WP_Http_ExtHttp();
-                               $blocking_transport[] = &$working_transport['exthttp'];
-                       } else if ( true === WP_Http_Curl::test($args) ) {
-                               $working_transport['curl'] = new WP_Http_Curl();
-                               $blocking_transport[] = &$working_transport['curl'];
-                       } else if ( true === WP_Http_Streams::test($args) ) {
-                               $working_transport['streams'] = new WP_Http_Streams();
-                               $blocking_transport[] = &$working_transport['streams'];
-                       } else if ( true === WP_Http_Fopen::test($args) ) {
-                               $working_transport['fopen'] = new WP_Http_Fopen();
-                               $blocking_transport[] = &$working_transport['fopen'];
-                       } else if ( true === WP_Http_Fsockopen::test($args) ) {
-                               $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];
-                       }
-               }
-
-               do_action( 'http_transport_get_debug', $working_transport, $blocking_transport, $nonblocking_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.0
-        * @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($args) ) {
-                               $working_transport['exthttp'] = new WP_Http_ExtHttp();
-                               $blocking_transport[] = &$working_transport['exthttp'];
-                       } else if ( true === WP_Http_Curl::test($args) ) {
-                               $working_transport['curl'] = new WP_Http_Curl();
-                               $blocking_transport[] = &$working_transport['curl'];
-                       } else if ( true === WP_Http_Streams::test($args) ) {
-                               $working_transport['streams'] = new WP_Http_Streams();
-                               $blocking_transport[] = &$working_transport['streams'];
-                       } else if ( true === WP_Http_Fsockopen::test($args) ) {
-                               $working_transport['fsockopen'] = new WP_Http_Fsockopen();
-                               $blocking_transport[] = &$working_transport['fsockopen'];
-                       }
-
-                       foreach ( array('curl', 'streams', 'fsockopen', 'exthttp') as $transport ) {
-                               if ( isset($working_transport[$transport]) )
-                                       $nonblocking_transport[] = &$working_transport[$transport];
-                       }
-               }
-
-               do_action( 'http_transport_post_debug', $working_transport, $blocking_transport, $nonblocking_transport );
-
-               if ( isset($args['blocking']) && !$args['blocking'] )
-                       return $nonblocking_transport;
-               else
-                       return $blocking_transport;
-       }
-
        /**
         * Send a HTTP request to a URI.
         *
@@ -210,7 +76,7 @@ class WP_Http {
         *
         * @param string $url URI resource.
         * @param str|array $args Optional. Override the defaults.
-        * @return array containing 'headers', 'body', 'response', 'cookies'
+        * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
         */
        function request( $url, $args = array() ) {
                global $wp_version;
@@ -227,50 +93,75 @@ class WP_Http {
                        'body' => null,
                        'compress' => false,
                        'decompress' => true,
-                       'sslverify' => true
+                       'sslverify' => true,
+                       'stream' => false,
+                       'filename' => null
                );
 
+
+               // Pre-parse for the HEAD checks.
+               $args = wp_parse_args( $args );
+
+               // By default, Head requests do not cause redirections.
+               if ( isset($args['method']) && 'HEAD' == $args['method'] )
+                       $defaults['redirection'] = 0;
+
                $r = wp_parse_args( $args, $defaults );
                $r = apply_filters( 'http_request_args', $r, $url );
 
+               // Certain classes decrement this, store a copy of the original value for loop purposes.
+               $r['_redirection'] = $r['redirection'];
+
                // Allow plugins to short-circuit the request
                $pre = apply_filters( 'pre_http_request', false, $r, $url );
                if ( false !== $pre )
                        return $pre;
 
-               $arrURL = parse_url($url);
+               $arrURL = parse_url( $url );
 
-               if ( empty( $url ) || empty($url['scheme'] ) )
+               if ( empty( $url ) || empty( $arrURL['scheme'] ) )
                        return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
 
                if ( $this->block_request( $url ) )
-                       return new WP_Error('http_request_failed', __('User has blocked requests through HTTP.'));
+                       return new WP_Error( 'http_request_failed', __( 'User has blocked requests through HTTP.' ) );
 
                // Determine if this is a https call and pass that on to the transport functions
                // so that we can blacklist the transports that do not support ssl verification
                $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl';
 
                // Determine if this request is to OUR install of WordPress
-               $homeURL = parse_url( get_bloginfo('url') );
+               $homeURL = parse_url( get_bloginfo( 'url' ) );
                $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host'];
-               unset($homeURL);
+               unset( $homeURL );
+
+               // If we are streaming to a file but no filename was given drop it in the WP temp dir
+               // and pick it's name using the basename of the $url
+               if ( $r['stream']  && empty( $r['filename'] ) )
+                       $r['filename'] = get_temp_dir() . basename( $url );
+
+               // Force some settings if we are streaming to a file and check for existence and perms of destination directory
+               if ( $r['stream'] ) {
+                       $r['blocking'] = true;
+                       if ( ! is_writable( dirname( $r['filename'] ) ) )
+                               return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
+               }
 
                if ( is_null( $r['headers'] ) )
                        $r['headers'] = array();
 
-               if ( ! is_array($r['headers']) ) {
-                       $processedHeaders = WP_Http::processHeaders($r['headers']);
+               if ( ! is_array( $r['headers'] ) ) {
+                       $processedHeaders = WP_Http::processHeaders( $r['headers'] );
                        $r['headers'] = $processedHeaders['headers'];
                }
 
-               if ( isset($r['headers']['User-Agent']) ) {
+               if ( isset( $r['headers']['User-Agent'] ) ) {
                        $r['user-agent'] = $r['headers']['User-Agent'];
-                       unset($r['headers']['User-Agent']);
+                       unset( $r['headers']['User-Agent'] );
                }
 
-               if ( isset($r['headers']['user-agent']) ) {
+               if ( isset( $r['headers']['user-agent'] ) ) {
                        $r['user-agent'] = $r['headers']['user-agent'];
-                       unset($r['headers']['user-agent']);
+                       unset( $r['headers']['user-agent'] );
                }
 
                // Construct Cookie: header if any cookies are set
@@ -280,49 +171,95 @@ class WP_Http {
                        $r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
 
                if ( empty($r['body']) ) {
+                       $r['body'] = null;
                        // Some servers fail when sending content without the content-length header being set.
                        // Also, to fix another bug, we only send when doing POST and PUT and the content-length
                        // header isn't already set.
-                       if( ($r['method'] == 'POST' || $r['method'] == 'PUT') && ! isset($r['headers']['Content-Length']) )
+                       if ( ($r['method'] == 'POST' || $r['method'] == 'PUT') && ! isset( $r['headers']['Content-Length'] ) )
                                $r['headers']['Content-Length'] = 0;
-
-                       // The method is ambiguous, because we aren't talking about HTTP methods, the "get" in
-                       // this case is simply that we aren't sending any bodies and to get the transports that
-                       // don't support sending bodies along with those which do.
-                       $transports = WP_Http::_getTransport($r);
                } else {
                        if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
-                               if ( ! version_compare(phpversion(), '5.1.2', '>=') )
-                                       $r['body'] = _http_build_query($r['body'], null, '&');
-                               else
-                                       $r['body'] = http_build_query($r['body'], null, '&');
-                               $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
-                               $r['headers']['Content-Length'] = strlen($r['body']);
+                               $r['body'] = http_build_query( $r['body'], null, '&' );
+                               if ( ! isset( $r['headers']['Content-Type'] ) )
+                                       $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' );
+                               $r['headers']['Content-Length'] = strlen( $r['body'] );
                        }
 
                        if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
-                               $r['headers']['Content-Length'] = strlen($r['body']);
-
-                       // The method is ambiguous, because we aren't talking about HTTP methods, the "post" in
-                       // this case is simply that we are sending HTTP body and to get the transports that do
-                       // support sending the body. Not all do, depending on the limitations of the PHP core
-                       // limitations.
-                       $transports = WP_Http::_postTransport($r);
+                               $r['headers']['Content-Length'] = strlen( $r['body'] );
                }
 
-               do_action( 'http_api_debug', $transports, 'transports_list' );
+               return $this->_dispatch_request($url, $r);
+       }
 
-               $response = array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
-               foreach ( (array) $transports as $transport ) {
-                       $response = $transport->request($url, $r);
+       /**
+        * Tests which transports are capable 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' );
 
-                       do_action( 'http_api_debug', $response, 'response', get_class($transport) );
+               // 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;
 
-                       if ( ! is_wp_error($response) )
-                               return apply_filters( 'http_response', $response, $r, $url );
+                       // 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 $response;
+               return false;
+       }
+
+       /**
+        * Dispatches a HTTP request to a supporting transport.
+        *
+        * Tests each transport in order to find a transport which matches the request arguments.
+        * Also caches the transport instance to be used later.
+        *
+        * The order for blocking requests is cURL, Streams, and finally Fsockopen.
+        * The order for non-blocking requests is cURL, Streams and Fsockopen().
+        *
+        * 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 [the]
+        * connected host has failed to respond."
+        *
+        * @since 3.2.0
+        * @access private
+        *
+        * @param string $url URL to Request
+        * @param array $args Request arguments
+        * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
+        */
+       private function _dispatch_request( $url, $args ) {
+               static $transports = array();
+
+               $class = $this->_get_first_available_transport( $args, $url );
+               if ( !$class )
+                       return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
+
+               // Transport claims to support request, instantiate it and give it a whirl.
+               if ( empty( $transports[$class] ) )
+                       $transports[$class] = new $class;
+
+               $response = $transports[$class]->request( $url, $args );
+
+               do_action( 'http_api_debug', $response, 'response', $class, $args, $url );
+
+               if ( is_wp_error( $response ) )
+                       return $response;
+
+               return apply_filters( 'http_response', $response, $args, $url );
        }
 
        /**
@@ -335,7 +272,7 @@ class WP_Http {
         *
         * @param string $url URI resource.
         * @param str|array $args Optional. Override the defaults.
-        * @return boolean
+        * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
         */
        function post($url, $args = array()) {
                $defaults = array('method' => 'POST');
@@ -353,7 +290,7 @@ class WP_Http {
         *
         * @param string $url URI resource.
         * @param str|array $args Optional. Override the defaults.
-        * @return boolean
+        * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
         */
        function get($url, $args = array()) {
                $defaults = array('method' => 'GET');
@@ -371,7 +308,7 @@ class WP_Http {
         *
         * @param string $url URI resource.
         * @param str|array $args Optional. Override the defaults.
-        * @return boolean
+        * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
         */
        function head($url, $args = array()) {
                $defaults = array('method' => 'HEAD');
@@ -392,7 +329,7 @@ class WP_Http {
        function processResponse($strResponse) {
                $res = explode("\r\n\r\n", $strResponse, 2);
 
-               return array('headers' => isset($res[0]) ? $res[0] : array(), 'body' => isset($res[1]) ? $res[1] : '');
+               return array('headers' => $res[0], 'body' => isset($res[1]) ? $res[1] : '');
        }
 
        /**
@@ -409,7 +346,7 @@ class WP_Http {
         * @return array Processed string headers. If duplicate headers are encountered,
         *                                      Then a numbered array is returned as the value of that header-key.
         */
-       function processHeaders($headers) {
+       public static function processHeaders($headers) {
                // split headers, one per array element
                if ( is_string($headers) ) {
                        // tolerate line terminator: CRLF = LF (RFC 2616 19.3)
@@ -433,12 +370,14 @@ class WP_Http {
 
                $cookies = array();
                $newheaders = array();
-               foreach ( $headers as $tempheader ) {
+               foreach ( (array) $headers as $tempheader ) {
                        if ( empty($tempheader) )
                                continue;
 
                        if ( false === strpos($tempheader, ':') ) {
-                               list( , $response['code'], $response['message']) = explode(' ', $tempheader, 3);
+                               $stack = explode(' ', $tempheader, 3);
+                               $stack[] = '';
+                               list( , $response['code'], $response['message']) = $stack;
                                continue;
                        }
 
@@ -453,7 +392,7 @@ class WP_Http {
                                } else {
                                        $newheaders[$key] = trim( $value );
                                }
-                               if ( 'set-cookie' == strtolower( $key ) )
+                               if ( 'set-cookie' == $key )
                                        $cookies[] = new WP_Http_Cookie( $value );
                        }
                }
@@ -474,7 +413,7 @@ class WP_Http {
         *
         * @param array $r Full array of args passed into ::request()
         */
-       function buildCookieHeader( &$r ) {
+       public static function buildCookieHeader( &$r ) {
                if ( ! empty($r['cookies']) ) {
                        $cookies_header = '';
                        foreach ( (array) $r['cookies'] as $cookie ) {
@@ -540,10 +479,12 @@ class WP_Http {
         * You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL as true in your wp-config.php
         * file and this will only allow localhost and your blog to make requests. The constant
         * WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the
-        * WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow.
+        * WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow, wildcard domains
+        * are supported, eg *.wordpress.org will allow for all subdomains of wordpress.org to be contacted.
         *
         * @since 2.8.0
         * @link http://core.trac.wordpress.org/ticket/8927 Allow preventing external requests.
+        * @link http://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_ACCESSIBLE_HOSTS
         *
         * @param string $uri URI of url.
         * @return bool True to block, false to allow.
@@ -577,10 +518,25 @@ class WP_Http {
                        return true;
 
                static $accessible_hosts;
-               if ( null == $accessible_hosts )
+               static $wildcard_regex = false;
+               if ( null == $accessible_hosts ) {
                        $accessible_hosts = preg_split('|,\s*|', WP_ACCESSIBLE_HOSTS);
 
-               return !in_array( $check['host'], $accessible_hosts ); //Inverse logic, If its in the array, then we can't access it.
+                       if ( false !== strpos(WP_ACCESSIBLE_HOSTS, '*') ) {
+                               $wildcard_regex = array();
+                               foreach ( $accessible_hosts as $host )
+                                       $wildcard_regex[] = str_replace('\*', '[\w.]+?', preg_quote($host, '/'));
+                               $wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
+                       }
+               }
+
+               if ( !empty($wildcard_regex) )
+                       return !preg_match($wildcard_regex, $check['host']);
+               else
+                       return !in_array( $check['host'], $accessible_hosts ); //Inverse logic, If its in the array, then we can't access it.
+
+
+
        }
 }
 
@@ -606,7 +562,7 @@ class WP_Http_Fsockopen {
         * @access public
         * @param string $url URI resource.
         * @param str|array $args Optional. Override the defaults.
-        * @return array 'headers', 'body', 'cookies' and 'response' keys.
+        * @return array 'headers', 'body', 'response', 'cookies' and 'filename' keys.
         */
        function request($url, $args = array()) {
                $defaults = array(
@@ -621,7 +577,7 @@ class WP_Http_Fsockopen {
                if ( isset($r['headers']['User-Agent']) ) {
                        $r['user-agent'] = $r['headers']['User-Agent'];
                        unset($r['headers']['User-Agent']);
-               } else if( isset($r['headers']['user-agent']) ) {
+               } else if ( isset($r['headers']['user-agent']) ) {
                        $r['user-agent'] = $r['headers']['user-agent'];
                        unset($r['headers']['user-agent']);
                }
@@ -676,7 +632,7 @@ class WP_Http_Fsockopen {
 
                $endDelay = time();
 
-               // If the delay is greater than the timeout then fsockopen should't be used, because it will
+               // If the delay is greater than the timeout then fsockopen shouldn't be used, because it will
                // cause a long delay.
                $elapseDelay = ($endDelay-$startDelay) > $r['timeout'];
                if ( true === $elapseDelay )
@@ -730,23 +686,52 @@ class WP_Http_Fsockopen {
                }
 
                $strResponse = '';
-               while ( ! feof($handle) )
-                       $strResponse .= fread($handle, 4096);
+               $bodyStarted = false;
+
+               // If streaming to a file setup the file handle
+               if ( $r['stream'] ) {
+                       if ( ! WP_DEBUG )
+                               $stream_handle = @fopen( $r['filename'], 'w+' );
+                       else
+                               $stream_handle = fopen( $r['filename'], 'w+' );
+                       if ( ! $stream_handle )
+                               return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
+
+                       while ( ! feof($handle) ) {
+                               $block = fread( $handle, 4096 );
+                               if ( $bodyStarted ) {
+                                       fwrite( $stream_handle, $block );
+                               } else {
+                                       $strResponse .= $block;
+                                       if ( strpos( $strResponse, "\r\n\r\n" ) ) {
+                                               $process = WP_Http::processResponse( $strResponse );
+                                               $bodyStarted = true;
+                                               fwrite( $stream_handle, $process['body'] );
+                                               unset( $strResponse );
+                                               $process['body'] = '';
+                                       }
+                               }
+                       }
 
-               fclose($handle);
+                       fclose( $stream_handle );
+
+               } else {
+                       while ( ! feof($handle) )
+                               $strResponse .= fread( $handle, 4096 );
+
+                       $process = WP_Http::processResponse( $strResponse );
+                       unset( $strResponse );
+               }
+
+               fclose( $handle );
 
                if ( true === $secure_transport )
                        error_reporting($error_reporting);
 
-               $process = WP_Http::processResponse($strResponse);
-               $arrHeaders = WP_Http::processHeaders($process['headers']);
-
-               // Is the response code within the 400 range?
-               if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 )
-                       return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']);
+               $arrHeaders = WP_Http::processHeaders( $process['headers'] );
 
                // If location is found, then assume redirect and redirect to location.
-               if ( 'HEAD' != $r['method'] && isset($arrHeaders['headers']['location']) ) {
+               if ( isset($arrHeaders['headers']['location']) && 0 !== $r['_redirection'] ) {
                        if ( $r['redirection']-- > 0 ) {
                                return $this->request($arrHeaders['headers']['location'], $r);
                        } else {
@@ -761,7 +746,7 @@ class WP_Http_Fsockopen {
                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) )
                        $process['body'] = WP_Http_Encoding::decompress( $process['body'] );
 
-               return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']);
+               return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename'] );
        }
 
        /**
@@ -771,169 +756,19 @@ class WP_Http_Fsockopen {
         * @static
         * @return boolean False means this class can not be used, true means it can.
         */
-       function test( $args = array() ) {
-               if ( false !== ($option = get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours
+       public static function test( $args = array() ) {
+               if ( ! function_exists( 'fsockopen' ) )
                        return false;
 
-               $is_ssl = isset($args['ssl']) && $args['ssl'];
-
-               if ( ! $is_ssl && function_exists( 'fsockopen' ) )
-                       $use = true;
-               elseif ( $is_ssl && extension_loaded('openssl') && function_exists( 'fsockopen' ) )
-                       $use = true;
-               else
-                       $use = false;
-
-               return apply_filters('use_fsockopen_transport', $use, $args);
-       }
-}
-
-/**
- * HTTP request method uses fopen function to retrieve the url.
- *
- * Requires PHP version greater than 4.3.0 for stream support. Does not allow for $context support,
- * but should still be okay, to write the headers, before getting the response. Also requires that
- * 'allow_url_fopen' to be enabled.
- *
- * @package WordPress
- * @subpackage HTTP
- * @since 2.7.0
- */
-class WP_Http_Fopen {
-       /**
-        * Send a HTTP request to a URI using fopen().
-        *
-        * This transport does not support sending of headers and body, therefore should not be used in
-        * the instances, where there is a body and headers.
-        *
-        * Notes: Does not support non-blocking mode. Ignores 'redirection' option.
-        *
-        * @see WP_Http::retrieve For default options descriptions.
-        *
-        * @access public
-        * @since 2.7.0
-        *
-        * @param string $url URI resource.
-        * @param str|array $args Optional. Override the defaults.
-        * @return array 'headers', 'body', 'cookies' and 'response' keys.
-        */
-       function request($url, $args = array()) {
-               $defaults = array(
-                       'method' => 'GET', 'timeout' => 5,
-                       'redirection' => 5, 'httpversion' => '1.0',
-                       'blocking' => true,
-                       'headers' => array(), 'body' => null, 'cookies' => array()
-               );
-
-               $r = wp_parse_args( $args, $defaults );
-
-               $arrURL = parse_url($url);
-
-               if ( false === $arrURL )
-                       return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url));
-
-               if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] )
-                       $url = str_replace($arrURL['scheme'], 'http', $url);
-
-               if ( is_null( $r['headers'] ) )
-                       $r['headers'] = array();
-
-               if ( is_string($r['headers']) ) {
-                       $processedHeaders = WP_Http::processHeaders($r['headers']);
-                       $r['headers'] = $processedHeaders['headers'];
-               }
-
-               $initial_user_agent = ini_get('user_agent');
-
-               if ( !empty($r['headers']) && is_array($r['headers']) ) {
-                       $user_agent_extra_headers = '';
-                       foreach ( $r['headers'] as $header => $value )
-                               $user_agent_extra_headers .= "\r\n$header: $value";
-                       @ini_set('user_agent', $r['user-agent'] . $user_agent_extra_headers);
-               } else {
-                       @ini_set('user_agent', $r['user-agent']);
-               }
-
-               if ( !WP_DEBUG )
-                       $handle = @fopen($url, 'r');
-               else
-                       $handle = fopen($url, 'r');
-
-               if (! $handle)
-                       return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
-
-               $timeout = (int) floor( $r['timeout'] );
-               $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
-               stream_set_timeout( $handle, $timeout, $utimeout );
-
-               if ( ! $r['blocking'] ) {
-                       fclose($handle);
-                       @ini_set('user_agent', $initial_user_agent); //Clean up any extra headers added
-                       return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
-               }
-
-               $strResponse = '';
-               while ( ! feof($handle) )
-                       $strResponse .= fread($handle, 4096);
-
-               if ( function_exists('stream_get_meta_data') ) {
-                       $meta = stream_get_meta_data($handle);
-
-                       $theHeaders = $meta['wrapper_data'];
-                       if ( isset( $meta['wrapper_data']['headers'] ) )
-                               $theHeaders = $meta['wrapper_data']['headers'];
-               } else {
-                       //$http_response_header is a PHP reserved variable which is set in the current-scope when using the HTTP Wrapper
-                       //see http://php.oregonstate.edu/manual/en/reserved.variables.httpresponseheader.php
-                       $theHeaders = $http_response_header;
-               }
-
-               fclose($handle);
-
-               @ini_set('user_agent', $initial_user_agent); //Clean up any extra headers added
-
-               $processedHeaders = WP_Http::processHeaders($theHeaders);
-
-               if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
-                       $strResponse = WP_Http::chunkTransferDecode($strResponse);
-
-               if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) )
-                       $strResponse = WP_Http_Encoding::decompress( $strResponse );
-
-               return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
-       }
-
-       /**
-        * Whether this class can be used for retrieving an URL.
-        *
-        * @since 2.7.0
-        * @static
-        * @return boolean False means this class can not be used, true means it can.
-        */
-       function test($args = array()) {
-               if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
+               if ( false !== ($option = get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours
                        return false;
 
-               if ( isset($args['method']) && 'HEAD' == $args['method'] ) //This transport cannot make a HEAD request
-                       return false;
+               $is_ssl = isset( $args['ssl'] ) && $args['ssl'];
 
-               $use = true;
-               //PHP does not verify SSL certs, We can only make a request via this transports if SSL Verification is turned off.
-               $is_ssl = isset($args['ssl']) && $args['ssl'];
-               if ( $is_ssl ) {
-                       $is_local = isset($args['local']) && $args['local'];
-                       $ssl_verify = isset($args['sslverify']) && $args['sslverify'];
-                       if ( $is_local && true != apply_filters('https_local_ssl_verify', true) )
-                               $use = true;
-                       elseif ( !$is_local && true != apply_filters('https_ssl_verify', true) )
-                               $use = true;
-                       elseif ( !$ssl_verify )
-                               $use = true;
-                       else
-                               $use = false;
-               }
+               if ( $is_ssl && ! extension_loaded( 'openssl' ) )
+                       return false;
 
-               return apply_filters('use_fopen_transport', $use, $args);
+               return apply_filters( 'use_fsockopen_transport', true, $args );
        }
 }
 
@@ -958,7 +793,7 @@ class WP_Http_Streams {
         *
         * @param string $url
         * @param str|array $args Optional. Override the defaults.
-        * @return array 'headers', 'body', 'cookies' and 'response' keys.
+        * @return array 'headers', 'body', 'response', 'cookies' and 'filename' keys.
         */
        function request($url, $args = array()) {
                $defaults = array(
@@ -973,7 +808,7 @@ class WP_Http_Streams {
                if ( isset($r['headers']['User-Agent']) ) {
                        $r['user-agent'] = $r['headers']['User-Agent'];
                        unset($r['headers']['User-Agent']);
-               } else if( isset($r['headers']['user-agent']) ) {
+               } else if ( isset($r['headers']['user-agent']) ) {
                        $r['user-agent'] = $r['headers']['user-agent'];
                        unset($r['headers']['user-agent']);
                }
@@ -1031,9 +866,6 @@ class WP_Http_Streams {
                                $arrContext['http']['header'] .= $proxy->authentication_header() . "\r\n";
                }
 
-               if ( 'HEAD' == $r['method'] ) // Disable redirects for HEAD requests
-                       $arrContext['http']['max_redirects'] = 1;
-
                if ( ! empty($r['body'] ) )
                        $arrContext['http']['content'] = $r['body'];
 
@@ -1057,10 +889,26 @@ class WP_Http_Streams {
                        return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
                }
 
-               $strResponse = stream_get_contents($handle);
-               $meta = stream_get_meta_data($handle);
+               if ( $r['stream'] ) {
+                       if ( ! WP_DEBUG )
+                               $stream_handle = @fopen( $r['filename'], 'w+' );
+                       else
+                               $stream_handle = fopen( $r['filename'], 'w+' );
+
+                       if ( ! $stream_handle )
+                               return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
 
-               fclose($handle);
+                       stream_copy_to_stream( $handle, $stream_handle );
+
+                       fclose( $stream_handle );
+                       $strResponse = '';
+               } else {
+                       $strResponse = stream_get_contents( $handle );
+               }
+
+               $meta = stream_get_meta_data( $handle );
+
+               fclose( $handle );
 
                $processedHeaders = array();
                if ( isset( $meta['wrapper_data']['headers'] ) )
@@ -1068,13 +916,18 @@ class WP_Http_Streams {
                else
                        $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
 
+               // Streams does not provide an error code which we can use to see why the request stream stopped.
+               // We can however test to see if a location header is present and return based on that.
+               if ( isset($processedHeaders['headers']['location']) && 0 !== $args['_redirection'] )
+                       return new WP_Error('http_request_failed', __('Too many redirects.'));
+
                if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
                        $strResponse = WP_Http::chunkTransferDecode($strResponse);
 
                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) )
                        $strResponse = WP_Http_Encoding::decompress( $strResponse );
 
-               return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
+               return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename'] );
        }
 
        /**
@@ -1086,179 +939,19 @@ class WP_Http_Streams {
         *
         * @return boolean False means this class can not be used, true means it can.
         */
-       function test($args = array()) {
-               if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
+       public static function test( $args = array() ) {
+               if ( ! function_exists( 'fopen' ) )
                        return false;
 
-               if ( version_compare(PHP_VERSION, '5.0', '<') )
+               if ( ! function_exists( 'ini_get' ) || true != ini_get( 'allow_url_fopen' ) )
                        return false;
 
-               //HTTPS via Proxy was added in 5.1.0
-               $is_ssl = isset($args['ssl']) && $args['ssl'];
-               if ( $is_ssl && version_compare(PHP_VERSION, '5.1.0', '<') ) {
-                       $proxy = new WP_HTTP_Proxy();
-                       /**
-                        * No URL check, as its not currently passed to the ::test() function
-                        * In the case where a Proxy is in use, Just bypass this transport for HTTPS.
-                        */
-                       if ( $proxy->is_enabled() )
-                               return false;
-               }
-
-               return apply_filters('use_streams_transport', true, $args);
-       }
-}
-
-/**
- * HTTP request method uses HTTP extension to retrieve the url.
- *
- * Requires the HTTP extension to be installed. This would be the preferred transport since it can
- * handle a lot of the problems that forces the others to use the HTTP version 1.0. Even if PHP 5.2+
- * is being used, it doesn't mean that the HTTP extension will be enabled.
- *
- * @package WordPress
- * @subpackage HTTP
- * @since 2.7.0
- */
-class WP_Http_ExtHTTP {
-       /**
-        * Send a HTTP request to a URI using HTTP extension.
-        *
-        * Does not support non-blocking.
-        *
-        * @access public
-        * @since 2.7
-        *
-        * @param string $url
-        * @param str|array $args Optional. Override the defaults.
-        * @return array 'headers', 'body', 'cookies' and 'response' keys.
-        */
-       function request($url, $args = array()) {
-               $defaults = array(
-                       'method' => 'GET', 'timeout' => 5,
-                       'redirection' => 5, 'httpversion' => '1.0',
-                       'blocking' => true,
-                       'headers' => array(), 'body' => null, 'cookies' => array()
-               );
-
-               $r = wp_parse_args( $args, $defaults );
-
-               if ( isset($r['headers']['User-Agent']) ) {
-                       $r['user-agent'] = $r['headers']['User-Agent'];
-                       unset($r['headers']['User-Agent']);
-               } else if( isset($r['headers']['user-agent']) ) {
-                       $r['user-agent'] = $r['headers']['user-agent'];
-                       unset($r['headers']['user-agent']);
-               }
-
-               // Construct Cookie: header if any cookies are set
-               WP_Http::buildCookieHeader( $r );
+               $is_ssl = isset( $args['ssl'] ) && $args['ssl'];
 
-               switch ( $r['method'] ) {
-                       case 'POST':
-                               $r['method'] = HTTP_METH_POST;
-                               break;
-                       case 'HEAD':
-                               $r['method'] = HTTP_METH_HEAD;
-                               break;
-                       case 'PUT':
-                               $r['method'] =  HTTP_METH_PUT;
-                               break;
-                       case 'GET':
-                       default:
-                               $r['method'] = HTTP_METH_GET;
-               }
-
-               $arrURL = parse_url($url);
-
-               if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] )
-                       $url = preg_replace('|^' . preg_quote($arrURL['scheme'], '|') . '|', 'http', $url);
-
-               $is_local = isset($args['local']) && $args['local'];
-               $ssl_verify = isset($args['sslverify']) && $args['sslverify'];
-               if ( $is_local )
-                       $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
-               elseif ( ! $is_local )
-                       $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
-
-               $r['timeout'] = (int) ceil( $r['timeout'] );
-
-               $options = array(
-                       'timeout' => $r['timeout'],
-                       'connecttimeout' => $r['timeout'],
-                       'redirect' => $r['redirection'],
-                       'useragent' => $r['user-agent'],
-                       'headers' => $r['headers'],
-                       'ssl' => array(
-                               'verifypeer' => $ssl_verify,
-                               'verifyhost' => $ssl_verify
-                       )
-               );
-
-               if ( HTTP_METH_HEAD == $r['method'] )
-                       $options['redirect'] = 0; // Assumption: Docs seem to suggest that this means do not follow. Untested.
-
-               // The HTTP extensions offers really easy proxy support.
-               $proxy = new WP_HTTP_Proxy();
-
-               if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
-                       $options['proxyhost'] = $proxy->host();
-                       $options['proxyport'] = $proxy->port();
-                       $options['proxytype'] = HTTP_PROXY_HTTP;
-
-                       if ( $proxy->use_authentication() ) {
-                               $options['proxyauth'] = $proxy->authentication();
-                               $options['proxyauthtype'] = HTTP_AUTH_ANY;
-                       }
-               }
-
-               if ( !WP_DEBUG ) //Emits warning level notices for max redirects and timeouts
-                       $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info);
-               else
-                       $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); //Emits warning level notices for max redirects and timeouts
-
-               // Error may still be set, Response may return headers or partial document, and error
-               // contains a reason the request was aborted, eg, timeout expired or max-redirects reached.
-               if ( false === $strResponse || ! empty($info['error']) )
-                       return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']);
-
-               if ( ! $r['blocking'] )
-                       return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
-
-               $headers_body = WP_HTTP::processResponse($strResponse);
-               $theHeaders = $headers_body['headers'];
-               $theBody = $headers_body['body'];
-               unset($headers_body);
-
-               $theHeaders = WP_Http::processHeaders($theHeaders);
-
-               if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] ) {
-                       if ( !WP_DEBUG )
-                               $theBody = @http_chunked_decode($theBody);
-                       else
-                               $theBody = http_chunked_decode($theBody);
-               }
-
-               if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) )
-                       $theBody = http_inflate( $theBody );
-
-               $theResponse = array();
-               $theResponse['code'] = $info['response_code'];
-               $theResponse['message'] = get_status_header_desc($info['response_code']);
-
-               return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse, 'cookies' => $theHeaders['cookies']);
-       }
+               if ( $is_ssl && ! extension_loaded( 'openssl' ) )
+                       return false;
 
-       /**
-        * Whether this class can be used for retrieving an URL.
-        *
-        * @static
-        * @since 2.7.0
-        *
-        * @return boolean False means this class can not be used, true means it can.
-        */
-       function test($args = array()) {
-               return apply_filters('use_http_extension_transport', function_exists('http_request'), $args );
+               return apply_filters( 'use_streams_transport', true, $args );
        }
 }
 
@@ -1273,6 +966,15 @@ class WP_Http_ExtHTTP {
  */
 class WP_Http_Curl {
 
+       /**
+        * Temporary header storage for use with streaming to a file.
+        *
+        * @since 3.2.0
+        * @access private
+        * @var string
+        */
+       private $headers = '';
+
        /**
         * Send a HTTP request to a URI using cURL extension.
         *
@@ -1281,7 +983,7 @@ class WP_Http_Curl {
         *
         * @param string $url
         * @param str|array $args Optional. Override the defaults.
-        * @return array 'headers', 'body', 'cookies' and 'response' keys.
+        * @return array 'headers', 'body', 'response', 'cookies' and 'filename' keys.
         */
        function request($url, $args = array()) {
                $defaults = array(
@@ -1296,7 +998,7 @@ class WP_Http_Curl {
                if ( isset($r['headers']['User-Agent']) ) {
                        $r['user-agent'] = $r['headers']['User-Agent'];
                        unset($r['headers']['User-Agent']);
-               } else if( isset($r['headers']['user-agent']) ) {
+               } else if ( isset($r['headers']['user-agent']) ) {
                        $r['user-agent'] = $r['headers']['user-agent'];
                        unset($r['headers']['user-agent']);
                }
@@ -1311,20 +1013,12 @@ class WP_Http_Curl {
 
                if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
 
-                       $isPHP5 = version_compare(PHP_VERSION, '5.0.0', '>=');
-
-                       if ( $isPHP5 ) {
-                               curl_setopt( $handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
-                               curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() );
-                               curl_setopt( $handle, CURLOPT_PROXYPORT, $proxy->port() );
-                       } else {
-                               curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() .':'. $proxy->port() );
-                       }
+                       curl_setopt( $handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
+                       curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() );
+                       curl_setopt( $handle, CURLOPT_PROXYPORT, $proxy->port() );
 
                        if ( $proxy->use_authentication() ) {
-                               if ( $isPHP5 )
-                                       curl_setopt( $handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
-
+                               curl_setopt( $handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
                                curl_setopt( $handle, CURLOPT_PROXYUSERPWD, $proxy->authentication() );
                        }
                }
@@ -1338,14 +1032,14 @@ class WP_Http_Curl {
 
 
                // CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers.  Have to use ceil since
-               // a value of 0 will allow an ulimited timeout.
+               // a value of 0 will allow an unlimited timeout.
                $timeout = (int) ceil( $r['timeout'] );
                curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $timeout );
                curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
 
                curl_setopt( $handle, CURLOPT_URL, $url);
                curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
-               curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify );
+               curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, ( $ssl_verify === true ) ? 2 : false );
                curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
                curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
                curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );
@@ -1365,13 +1059,23 @@ class WP_Http_Curl {
                }
 
                if ( true === $r['blocking'] )
-                       curl_setopt( $handle, CURLOPT_HEADER, true );
-               else
-                       curl_setopt( $handle, CURLOPT_HEADER, false );
+                       curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) );
+
+               curl_setopt( $handle, CURLOPT_HEADER, false );
+
+               // If streaming to a file open a file handle, and setup our curl streaming handler
+               if ( $r['stream'] ) {
+                       if ( ! WP_DEBUG )
+                               $stream_handle = @fopen( $r['filename'], 'w+' );
+                       else
+                               $stream_handle = fopen( $r['filename'], 'w+' );
+                       if ( ! $stream_handle )
+                               return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
+                       curl_setopt( $handle, CURLOPT_FILE, $stream_handle );
+               }
 
                // The option doesn't work with safe mode or when open_basedir is set.
-               // Disable HEAD when making HEAD requests.
-               if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] )
+               if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 0 !== $r['_redirection'] )
                        curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
 
                if ( !empty( $r['headers'] ) ) {
@@ -1400,48 +1104,58 @@ class WP_Http_Curl {
                }
 
                $theResponse = curl_exec( $handle );
+               $theBody = '';
+               $theHeaders = WP_Http::processHeaders( $this->headers );
 
-               if ( !empty($theResponse) ) {
-                       $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
-                       $theHeaders = trim( substr($theResponse, 0, $headerLength) );
-                       if ( strlen($theResponse) > $headerLength )
-                               $theBody = substr( $theResponse, $headerLength );
-                       else
-                               $theBody = '';
-                       if ( false !== strrpos($theHeaders, "\r\n\r\n") ) {
-                               $headerParts = explode("\r\n\r\n", $theHeaders);
-                               $theHeaders = $headerParts[ count($headerParts) -1 ];
-                       }
-                       $theHeaders = WP_Http::processHeaders($theHeaders);
-               } else {
+               if ( strlen($theResponse) > 0 && ! is_bool( $theResponse ) ) // is_bool: when using $args['stream'], curl_exec will return (bool)true
+                       $theBody = $theResponse;
+
+               // If no response, and It's not a HEAD request with valid headers returned
+               if ( 0 == strlen($theResponse) && ('HEAD' != $args['method'] || empty($this->headers)) ) {
                        if ( $curl_error = curl_error($handle) )
                                return new WP_Error('http_request_failed', $curl_error);
                        if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) )
                                return new WP_Error('http_request_failed', __('Too many redirects.'));
-
-                       $theHeaders = array( 'headers' => array(), 'cookies' => array() );
-                       $theBody = '';
                }
 
+               $this->headers = '';
+
                $response = array();
                $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
                $response['message'] = get_status_header_desc($response['code']);
 
                curl_close( $handle );
 
+               if ( $r['stream'] )
+                       fclose( $stream_handle );
+
                // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
-               if ( !empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) ) {
+               if ( ! empty( $theHeaders['headers']['location'] ) && ( ini_get( 'safe_mode' ) || ini_get( 'open_basedir' ) ) && 0 !== $r['_redirection'] ) {
                        if ( $r['redirection']-- > 0 ) {
-                               return $this->request($theHeaders['headers']['location'], $r);
+                               return $this->request( $theHeaders['headers']['location'], $r );
                        } else {
-                               return new WP_Error('http_request_failed', __('Too many redirects.'));
+                               return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
                        }
                }
 
                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) )
                        $theBody = WP_Http_Encoding::decompress( $theBody );
 
-               return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']);
+               return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] );
+       }
+
+       /**
+        * Grab the headers of the cURL request
+        *
+        * Each header is sent individually to this callback, so we append to the $header property for temporary storage
+        *
+        * @since 3.2.0
+        * @access private
+        * @return int
+        */
+       private function stream_headers( $handle, $headers ) {
+               $this->headers .= $headers;
+               return strlen( $headers );
        }
 
        /**
@@ -1452,11 +1166,19 @@ class WP_Http_Curl {
         *
         * @return boolean False means this class can not be used, true means it can.
         */
-       function test($args = array()) {
-               if ( function_exists('curl_init') && function_exists('curl_exec') )
-                       return apply_filters('use_curl_transport', true, $args);
+       public static function test( $args = array() ) {
+               if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) )
+                       return false;
 
-               return false;
+               $is_ssl = isset( $args['ssl'] ) && $args['ssl'];
+
+               if ( $is_ssl ) {
+                       $curl_version = curl_version();
+                       if ( ! (CURL_VERSION_SSL & $curl_version['features']) ) // Does this cURL version support SSL requests?
+                               return false;
+               }
+
+               return apply_filters( 'use_curl_transport', true, $args );
        }
 }
 
@@ -1468,7 +1190,7 @@ class WP_Http_Curl {
  * constants.
  *
  * Please note that only BASIC authentication is supported by most transports.
- * cURL and the PHP HTTP Extension MAY support more methods (such as NTLM authentication) depending on your environment.
+ * cURL MAY support more methods (such as NTLM authentication) depending on your environment.
  *
  * The constants are as follows:
  * <ol>
@@ -1478,17 +1200,18 @@ class WP_Http_Curl {
  * <li>WP_PROXY_PASSWORD - Proxy password, if it requires authentication.</li>
  * <li>WP_PROXY_BYPASS_HOSTS - Will prevent the hosts in this list from going through the proxy.
  * You do not need to have localhost and the blog host in this list, because they will not be passed
- * through the proxy. The list should be presented in a comma separated list</li>
+ * through the proxy. The list should be presented in a comma separated list, wildcards using * are supported, eg. *.wordpress.org</li>
  * </ol>
  *
  * An example can be as seen below.
  * <code>
  * define('WP_PROXY_HOST', '192.168.84.101');
  * define('WP_PROXY_PORT', '8080');
- * define('WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com');
+ * define('WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com, *.wordpress.org');
  * </code>
  *
  * @link http://core.trac.wordpress.org/ticket/4011 Proxy support ticket in WordPress.
+ * @link http://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_PROXY_BYPASS_HOSTS
  * @since 2.8
  */
 class WP_HTTP_Proxy {
@@ -1605,7 +1328,7 @@ class WP_HTTP_Proxy {
         * hosts that won't be sent through the proxy.
         *
         * @uses WP_PROXY_BYPASS_HOSTS
-        * @since unknown
+        * @since 2.8.0
         *
         * @param string $uri URI to check.
         * @return bool True, to send through the proxy and false if, the proxy should not be used.
@@ -1628,10 +1351,22 @@ class WP_HTTP_Proxy {
                        return true;
 
                static $bypass_hosts;
-               if ( null == $bypass_hosts )
+               static $wildcard_regex = false;
+               if ( null == $bypass_hosts ) {
                        $bypass_hosts = preg_split('|,\s*|', WP_PROXY_BYPASS_HOSTS);
 
-               return !in_array( $check['host'], $bypass_hosts );
+                       if ( false !== strpos(WP_PROXY_BYPASS_HOSTS, '*') ) {
+                               $wildcard_regex = array();
+                               foreach ( $bypass_hosts as $host )
+                                       $wildcard_regex[] = str_replace('\*', '[\w.]+?', preg_quote($host, '/'));
+                               $wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
+                       }
+               }
+
+               if ( !empty($wildcard_regex) )
+                       return !preg_match($wildcard_regex, $check['host']);
+               else
+                       return !in_array( $check['host'], $bypass_hosts );
        }
 }
 /**
@@ -1689,17 +1424,6 @@ class WP_Http_Cookie {
         */
        var $domain;
 
-       /**
-        * PHP4 style Constructor - Calls PHP5 Style Constructor.
-        *
-        * @access public
-        * @since 2.8.0
-        * @param string|array $data Raw cookie data.
-        */
-       function WP_Http_Cookie( $data ) {
-               $this->__construct( $data );
-       }
-
        /**
         * Sets up this cookie object.
         *
@@ -1856,7 +1580,7 @@ class WP_Http_Encoding {
         * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
         * @return string|bool False on failure.
         */
-       function compress( $raw, $level = 9, $supports = null ) {
+       public static function compress( $raw, $level = 9, $supports = null ) {
                return gzdeflate( $raw, $level );
        }
 
@@ -1874,7 +1598,7 @@ class WP_Http_Encoding {
         * @param int $length The optional length of the compressed data.
         * @return string|bool False on failure.
         */
-       function decompress( $compressed, $length = null ) {
+       public static function decompress( $compressed, $length = null ) {
 
                if ( empty($compressed) )
                        return $compressed;
@@ -1902,17 +1626,25 @@ class WP_Http_Encoding {
         * Decompression of deflated string while staying compatible with the majority of servers.
         *
         * Certain Servers will return deflated data with headers which PHP's gziniflate()
-        * function cannot handle out of the box. The following function lifted from
-        * http://au2.php.net/manual/en/function.gzinflate.php#77336 will attempt to deflate
-        * the various return forms used.
+        * function cannot handle out of the box. The following function has been created from
+        * various snippets on the gzinflate() PHP documentation.
+        *
+        * Warning: Magic numbers within. Due to the potential different formats that the compressed
+        * data may be returned in, some "magic offsets" are needed to ensure proper decompression
+        * takes place. For a simple progmatic way to determine the magic offset in use, see:
+        * http://core.trac.wordpress.org/ticket/18273
         *
         * @since 2.8.1
+        * @link http://core.trac.wordpress.org/ticket/18273
+        * @link http://au2.php.net/manual/en/function.gzinflate.php#70875
         * @link http://au2.php.net/manual/en/function.gzinflate.php#77336
         *
         * @param string $gzData String to decompress.
         * @return string|bool False on failure.
         */
-       function compatible_gzinflate($gzData) {
+       public static function compatible_gzinflate($gzData) {
+
+               // Compressed data might contain a full header, if so strip it for gzinflate()
                if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
                        $i = 10;
                        $flg = ord( substr($gzData, 3, 1) );
@@ -1928,10 +1660,17 @@ class WP_Http_Encoding {
                                if ( $flg & 2 )
                                        $i = $i + 2;
                        }
-                       return gzinflate( substr($gzData, $i, -8) );
-               } else {
-                       return false;
+                       $decompressed = @gzinflate( substr($gzData, $i, -8) );
+                       if ( false !== $decompressed )
+                               return $decompressed;
                }
+
+               // Compressed data from java.util.zip.Deflater amongst others.
+               $decompressed = @gzinflate( substr($gzData, 2) );
+               if ( false !== $decompressed )
+                       return $decompressed;
+
+               return false;
        }
 
        /**
@@ -1941,7 +1680,7 @@ class WP_Http_Encoding {
         *
         * @return string Types of encoding to accept.
         */
-       function accept_encoding() {
+       public static function accept_encoding() {
                $type = array();
                if ( function_exists( 'gzinflate' ) )
                        $type[] = 'deflate;q=1.0';
@@ -1956,13 +1695,13 @@ class WP_Http_Encoding {
        }
 
        /**
-        * What enconding the content used when it was compressed to send in the headers.
+        * What encoding the content used when it was compressed to send in the headers.
         *
         * @since 2.8
         *
         * @return string Content-Encoding string to send in the header.
         */
-       function content_encoding() {
+       public static function content_encoding() {
                return 'deflate';
        }
 
@@ -1974,7 +1713,7 @@ class WP_Http_Encoding {
         * @param array|string $headers All of the available headers.
         * @return bool
         */
-       function should_decode($headers) {
+       public static function should_decode($headers) {
                if ( is_array( $headers ) ) {
                        if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
                                return true;
@@ -1996,7 +1735,7 @@ class WP_Http_Encoding {
         *
         * @return bool
         */
-       function is_available() {
+       public static function is_available() {
                return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );
        }
 }