Make WordPress Core

Changeset 8630


Ignore:
Timestamp:
08/12/2008 09:21:11 PM (16 years ago)
Author:
westi
Message:

HTTP API improvements. Implements chunked transfer decoding. Moves plugin update checker over to api. see #4779 props santosj.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/plugin-install.php

    r8600 r8630  
    1111
    1212    if ( ! $res ) {
    13         $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array(), array(), array('action' => $action, 'request' => serialize($args)) );
     13        $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
    1414        $res = unserialize($request['body']);
    1515        if ( ! $res )
  • trunk/wp-includes/http.php

    r8600 r8630  
    138138     * Send a HTTP request to a URI.
    139139     *
     140     * The body and headers are part of the arguments. The 'body' argument is
     141     * for the body and will accept either a string or an array. The 'headers'
     142     * argument should be an array, but a string is acceptable. If the 'body'
     143     * argument is an array, then it will automatically be escaped using
     144     * http_build_query().
     145     *
    140146     * The only URI that are supported in the HTTP Transport implementation are
    141147     * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
     
    172178     * @param string $url URI resource.
    173179     * @param str|array $args Optional. Override the defaults.
    174      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
    175      * @param string $body Optional. The body that should be sent. Will be automatically escaped and processed.
    176180     * @return boolean
    177181     */
    178     function request($url, $args = array(), $headers = null, $body = null) {
     182    function request( $url, $args = array() ) {
    179183        global $wp_version;
    180184
     
    183187            'redirection' => 5, 'httpversion' => '1.0',
    184188            'user-agent' => apply_filters('http_headers_useragent', 'WordPress/' . $wp_version ),
    185             'blocking' => true
     189            'blocking' => true,
     190            'headers' => array(), 'body' => null
    186191        );
    187192
    188193        $r = wp_parse_args( $args, $defaults );
    189194
    190         if ( is_null($headers) )
    191             $headers = array();
    192 
    193         if ( ! is_array($headers) ) {
    194             $processedHeaders = WP_Http::processHeaders($headers);
    195             $headers = $processedHeaders['headers'];
    196         }
    197 
    198         if ( isset($headers['User-Agent']) ) {
    199             $headers['user-agent'] = $headers['User-Agent'];
    200             unset($headers['User-Agent']);
    201         }
    202 
    203         if ( ! isset($headers['user-agent']) )
    204             $headers['user-agent'] = $r['user-agent'];
    205 
    206         if ( is_null($body) ) {
     195        if ( is_null( $r['headers'] ) )
     196            $r['headers'] = array();
     197
     198        if ( ! is_array($r['headers']) ) {
     199            $processedHeaders = WP_Http::processHeaders($r['headers']);
     200            $r['headers'] = $processedHeaders['headers'];
     201        }
     202
     203        if ( isset($r['headers']['User-Agent']) ) {
     204            $r['user-agent'] = $headers['User-Agent'];
     205            unset($r['headers']['User-Agent']);
     206        }
     207
     208        if ( isset($r['headers']['user-agent']) ) {
     209            $r['user-agent'] = $r['headers']['user-agent'];
     210            unset($r['headers']['user-agent']);
     211        }
     212
     213        if ( is_null($r['body']) ) {
    207214            $transports = WP_Http::_getTransport();
    208215        } else {
    209             if ( is_array($body) || is_object($body) )
    210                 $body = http_build_query($body);
     216            if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
     217                $r['body'] = http_build_query($r['body']);
     218                $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
     219                $r['headers']['Content-Length'] = strlen($r['body']);
     220            }
    211221
    212222            $transports = WP_Http::_postTransport();
     
    215225        $response = array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
    216226        foreach( (array) $transports as $transport ) {
    217             $response = $transport->request($url, $r, $headers, $body);
     227            $response = $transport->request($url, $r);
    218228
    219229            if( !is_wp_error($response) )
     
    234244     * @param string $url URI resource.
    235245     * @param str|array $args Optional. Override the defaults.
    236      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    237      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    238246     * @return boolean
    239247     */
    240     function post($url, $args = array(), $headers = null, $body = null) {
     248    function post($url, $args = array()) {
    241249        $defaults = array('method' => 'POST');
    242250        $r = wp_parse_args( $args, $defaults );
    243         return $this->request($url, $r, $headers, $body);
     251        return $this->request($url, $r);
    244252    }
    245253
     
    254262     * @param string $url URI resource.
    255263     * @param str|array $args Optional. Override the defaults.
    256      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    257      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    258264     * @return boolean
    259265     */
    260     function get($url, $args = array(), $headers = null, $body = null) {
     266    function get($url, $args = array()) {
    261267        $defaults = array('method' => 'GET');
    262268        $r = wp_parse_args( $args, $defaults );
    263         return $this->request($url, $r, $headers, $body);
     269        return $this->request($url, $r);
    264270    }
    265271
     
    274280     * @param string $url URI resource.
    275281     * @param str|array $args Optional. Override the defaults.
    276      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    277      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    278282     * @return boolean
    279283     */
    280     function head($url, $args = array(), $headers = null, $body = null) {
     284    function head($url, $args = array()) {
    281285        $defaults = array('method' => 'HEAD');
    282286        $r = wp_parse_args( $args, $defaults );
    283         return $this->request($url, $r, $headers, $body);
     287        return $this->request($url, $r);
    284288    }
    285289
     
    297301        list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
    298302        return array('headers' => $theHeaders, 'body' => $theBody);
    299     }
    300 
    301     /**
    302      * Whether response code is in the 400 range.
    303      *
    304      * @access public
    305      * @static
    306      * @since 2.7
    307      *
    308      * @param array $response Array with code and message keys
    309      * @return bool True if 40x Response, false if something else.
    310      */
    311     function is400Response($response) {
    312         if ( (int) substr($response, 0, 1) == 4 )
    313             return true;
    314         return false;
    315     }
    316 
    317     /**
    318      * Whether the headers returned a redirect location.
    319      *
    320      * Actually just checks whether the location header exists.
    321      *
    322      * @access public
    323      * @static
    324      * @since 2.7
    325      *
    326      * @param array $headers Array with headers
    327      * @return bool True if Location header is found.
    328      */
    329     function isRedirect($headers) {
    330         if ( isset($headers['location']) )
    331             return true;
    332         return false;
    333303    }
    334304
     
    357327            if ( empty($tempheader) )
    358328                continue;
    359 
    360329
    361330            if ( false === strpos($tempheader, ':') ) {
     
    374343        return array('response' => $response, 'headers' => $newheaders);
    375344    }
     345
     346    /**
     347     * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
     348     *
     349     * Based off the HTTP http_encoding_dechunk function. Does not support
     350     * UTF-8. Does not support returning footer headers. Shouldn't be too
     351     * difficult to support it though.
     352     *
     353     * @todo Add support for footer chunked headers.
     354     *
     355     * @static
     356     * @param string $body Body content
     357     * @return bool|string|WP_Error False if not chunked encoded. WP_Error on failure. Chunked decoded body on success.
     358     */
     359    function chunkTransferDecode($body) {
     360        $body = str_replace(array("\r\n", "\r"), "\n", $body);
     361        // The body is not chunked encoding or is malformed.
     362        if ( ! preg_match( '/^[0-9a-f]+(\s|\n)+/mi', trim($body) ) )
     363            return false;
     364
     365        $hex = '';
     366        $dec = 0;
     367        $parsedBody = '';
     368        $parsedHeaders = array();
     369
     370        $done = false;
     371
     372        do {
     373            $hasChunk = (bool) preg_match( '/^([0-9a-f]+)(\s|\n)+/mi', $body, $match );
     374
     375            if ( $hasChunk ) {
     376                if ( empty($match[1]) ) {
     377                    return new WP_Error('http_chunked_decode', __('Does not appear to be chunked encoded or body is malformed.') );
     378                }
     379
     380                $length = hexdec( $match[1] );
     381                $chunkLength = strlen( $match[0] );
     382
     383                if( $body{$length+$chunkLength} == "\n" )
     384                    $length++;
     385
     386                $strBody = substr($body, strlen( $match[0] ), $length);
     387                $parsedBody .= $strBody;
     388                $body = str_replace(array($match[0], $strBody), '', $body);
     389
     390                if( "0" == $body ) {
     391                    $done = true;
     392                    return $parsedBody; // Ignore footer headers.
     393                    break;
     394                }
     395            } else {
     396                return new WP_Error('http_chunked_decode', __('Does not appear to be chunked encoded or body is malformed.') );
     397            }
     398        } while ( false === $done );
     399    }
    376400}
    377401
     
    392416     * Does not support non-blocking mode.
    393417     *
    394      * @see WP_Http::retrieve For default options descriptions.
     418     * @see WP_Http::request For default options descriptions.
    395419     *
    396420     * @since 2.7
     
    398422     * @param string $url URI resource.
    399423     * @param str|array $args Optional. Override the defaults.
    400      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
    401      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    402424     * @return array 'headers', 'body', and 'response' keys.
    403425     */
    404     function request($url, $args = array(), $headers = null, $body = null) {
     426    function request($url, $args = array()) {
    405427        $defaults = array(
    406428            'method' => 'GET', 'timeout' => 3,
    407429            'redirection' => 5, 'httpversion' => '1.0',
    408             'blocking' => true
     430            'blocking' => true,
     431            'headers' => array(), 'body' => null
    409432        );
    410433
    411434        $r = wp_parse_args( $args, $defaults );
     435
     436        if ( isset($r['headers']['User-Agent']) ) {
     437            $r['user-agent'] = $r['headers']['User-Agent'];
     438            unset($r['headers']['User-Agent']);
     439        } else if( isset($r['headers']['user-agent']) ) {
     440            $r['user-agent'] = $r['headers']['user-agent'];
     441            unset($r['headers']['user-agent']);
     442        }
    412443
    413444        $iError = null; // Store error number
     
    446477        $strHeaders .= strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
    447478        $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
    448         if ( ! is_null($body) ) {
    449             $strHeaders .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
    450             $strHeaders .= 'Content-Length: ' . strlen($body) . "\r\n";
    451         }
    452 
    453         if ( is_array($headers) ) {
    454             foreach ( (array) $headers as $header => $headerValue )
     479
     480        if( isset($r['user-agent']) )
     481            $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
     482
     483        if ( is_array($r['headers']) ) {
     484            foreach ( (array) $r['headers'] as $header => $headerValue )
    455485                $strHeaders .= $header . ': ' . $headerValue . "\r\n";
    456486        } else {
    457             $strHeaders .= $headers;
     487            $strHeaders .= $r['headers'];
    458488        }
    459489
    460490        $strHeaders .= "\r\n";
    461491
    462         if ( ! is_null($body) )
    463             $strHeaders .= $body;
     492        if ( ! is_null($r['body']) )
     493            $strHeaders .= $r['body'];
    464494
    465495        fwrite($handle, $strHeaders);
     
    482512        $arrHeaders = WP_Http::processHeaders($process['headers']);
    483513
    484         if ( WP_Http::is400Response($arrHeaders['response']) )
     514        if ( WP_Http_Fsockopen::is400Response($arrHeaders['response']['code']) )
    485515            return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']);
    486516
     517        // If location is found, then assume redirect and redirect to location.
    487518        if ( isset($arrHeaders['headers']['location']) ) {
    488             if ( $r['redirection']-- > 0 )
    489                 return $this->request($arrHeaders['headers']['location'], $r, $headers, $body);
     519            if ( $r['redirection']-- > 0 ) {
     520                return $this->request($arrHeaders['headers']['location'], $r);
     521            }
    490522            else
    491523                return new WP_Error('http_request_failed', __('Too many redirects.'));
     
    508540        return false;
    509541    }
     542
     543    /**
     544     * Whether response code is in the 400 range.
     545     *
     546     * @access public
     547     * @static
     548     * @since 2.7
     549     *
     550     * @param string $response Response code.
     551     * @return bool True if 40x Response, false if something else.
     552     */
     553    function is400Response($response) {
     554        if ( is_numeric($response) && (int) substr($response, 0, 1) == 4 )
     555            return true;
     556        return false;
     557    }
    510558}
    511559
     
    537585     * @param string $url URI resource.
    538586     * @param str|array $args Optional. Override the defaults.
    539      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
    540      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    541587     * @return array 'headers', 'body', and 'response' keys.
    542588     */
    543     function request($url, $args = array(), $headers = null, $body = null) {
     589    function request($url, $args = array()) {
    544590        global $http_response_header;
    545591
     
    547593            'method' => 'GET', 'timeout' => 3,
    548594            'redirection' => 5, 'httpversion' => '1.0',
    549             'blocking' => true
     595            'blocking' => true,
     596            'headers' => array(), 'body' => null
    550597        );
    551598
     
    592639        $processedHeaders = WP_Http::processHeaders($theHeaders);
    593640
     641        if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
     642            $theBody = WP_Http::chunkTransferDecode($strResponse);
     643
    594644        return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']);
    595645    }
     
    630680     * @param string $url
    631681     * @param str|array $args Optional. Override the defaults.
    632      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
    633      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    634682     * @return array 'headers', 'body', and 'response' keys.
    635683     */
    636     function request($url, $args = array(), $headers = null, $body = null) {
     684    function request($url, $args = array()) {
    637685        $defaults = array(
    638686            'method' => 'GET', 'timeout' => 3,
    639687            'redirection' => 5, 'httpversion' => '1.0',
    640             'blocking' => true
     688            'blocking' => true,
     689            'headers' => array(), 'body' => null
    641690        );
    642691
    643692        $r = wp_parse_args( $args, $defaults );
    644693
    645         if ( isset($headers['User-Agent']) ) {
    646             $r['user-agent'] = $headers['User-Agent'];
    647             unset($headers['User-Agent']);
    648         } else if( isset($headers['user-agent']) ) {
    649             $r['user-agent'] = $headers['user-agent'];
    650             unset($headers['user-agent']);
    651         } else {
    652             $r['user-agent'] = apply_filters('http_headers_useragent', 'WordPress/' . $wp_version );
     694        if ( isset($r['headers']['User-Agent']) ) {
     695            $r['user-agent'] = $r['headers']['User-Agent'];
     696            unset($r['headers']['User-Agent']);
     697        } else if( isset($r['headers']['user-agent']) ) {
     698            $r['user-agent'] = $r['headers']['user-agent'];
     699            unset($r['headers']['user-agent']);
    653700        }
    654701
     
    660707        if ( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] )
    661708            $url = str_replace($arrURL['scheme'], 'http', $url);
     709
     710        // Convert Header array to string.
     711        $strHeaders = '';
     712        if ( is_array( $r['headers'] ) )
     713            foreach( $r['headers'] as $name => $value )
     714                $strHeaders .= "{$name}: $value\r\n";
     715        else if ( is_string( $r['headers'] ) )
     716            $strHeaders = $r['headers'];
    662717
    663718        $arrContext = array('http' =>
     
    667722                'max_redirects' => $r['redirection'],
    668723                'protocol_version' => (float) $r['httpversion'],
    669                 'header' => $headers,
     724                'header' => $strHeaders,
    670725                'timeout' => $r['timeout']
    671726            )
    672727        );
    673728
    674         if ( ! is_null($body) )
    675             $arrContext['http']['content'] = $body;
     729        if ( ! is_null($r['body']) && ! empty($r['body'] ) )
     730            $arrContext['http']['content'] = $r['body'];
    676731
    677732        $context = stream_context_create($arrContext);
     
    696751        $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
    697752
     753        if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
     754            $theBody = WP_Http::chunkTransferDecode($strResponse);
     755
    698756        fclose($handle);
    699757
     
    744802     * @param string $url
    745803     * @param str|array $args Optional. Override the defaults.
    746      * @param array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
    747      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    748804     * @return array 'headers', 'body', and 'response' keys.
    749805     */
    750     function request($url, $args = array(), $headers = null, $body = null) {
    751         global $wp_version;
    752 
     806    function request($url, $args = array()) {
    753807        $defaults = array(
    754808            'method' => 'GET', 'timeout' => 3,
    755809            'redirection' => 5, 'httpversion' => '1.0',
    756             'blocking' => true
     810            'blocking' => true,
     811            'headers' => array(), 'body' => null
    757812        );
    758813
    759814        $r = wp_parse_args( $args, $defaults );
    760815
    761         if ( isset($headers['User-Agent']) ) {
    762             $r['user-agent'] = $headers['User-Agent'];
    763             unset($headers['User-Agent']);
    764         } else if( isset($headers['user-agent']) ) {
    765             $r['user-agent'] = $headers['user-agent'];
    766             unset($headers['user-agent']);
    767         } else {
    768             $r['user-agent'] = apply_filters('http_headers_useragent', 'WordPress/' . $wp_version );
     816        if ( isset($r['headers']['User-Agent']) ) {
     817            $r['user-agent'] = $r['headers']['User-Agent'];
     818            unset($r['headers']['User-Agent']);
     819        } else if( isset($r['headers']['user-agent']) ) {
     820            $r['user-agent'] = $r['headers']['user-agent'];
     821            unset($r['headers']['user-agent']);
    769822        }
    770823
     
    793846            'redirect' => $r['redirection'],
    794847            'useragent' => $r['user-agent'],
    795             'headers' => $headers,
     848            'headers' => $r['headers'],
    796849        );
    797850
    798         $strResponse = http_request($r['method'], $url, $body, $options, $info);
     851        $strResponse = http_request($r['method'], $url, $r['body'], $options, $info);
    799852
    800853        if ( false === $strResponse )
     
    851904     * @param string $url
    852905     * @param str|array $args Optional. Override the defaults.
    853      * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
    854      * @param string $body Optional. The body that should be sent. Expected to be already processed.
    855906     * @return array 'headers', 'body', and 'response' keys.
    856907     */
    857     function request($url, $args = array(), $headers = null, $body = null) {
    858         global $wp_version;
    859 
     908    function request($url, $args = array()) {
    860909        $defaults = array(
    861910            'method' => 'GET', 'timeout' => 3,
    862911            'redirection' => 5, 'httpversion' => '1.0',
    863             'blocking' => true
     912            'blocking' => true,
     913            'headers' => array(), 'body' => null
    864914        );
    865915
    866916        $r = wp_parse_args( $args, $defaults );
    867917
    868         if ( isset($headers['User-Agent']) ) {
    869             $r['user-agent'] = $headers['User-Agent'];
    870             unset($headers['User-Agent']);
    871         } else if( isset($headers['user-agent']) ) {
    872             $r['user-agent'] = $headers['user-agent'];
    873             unset($headers['user-agent']);
    874         } else {
    875             $r['user-agent'] = apply_filters('http_headers_useragent', 'WordPress/' . $wp_version );
     918        if ( isset($r['headers']['User-Agent']) ) {
     919            $r['user-agent'] = $r['headers']['User-Agent'];
     920            unset($r['headers']['User-Agent']);
     921        } else if( isset($r['headers']['user-agent']) ) {
     922            $r['user-agent'] = $r['headers']['user-agent'];
     923            unset($r['headers']['user-agent']);
    876924        }
    877925
     
    897945            curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
    898946
    899         if( ! is_null($headers) )
    900             curl_setopt( $handle, CURLOPT_HTTPHEADER, $headers );
     947        if( ! is_null($r['headers']) )
     948            curl_setopt( $handle, CURLOPT_HTTPHEADER, $r['headers'] );
    901949
    902950        if ( $r['httpversion'] == '1.0' )
     
    915963        list($theHeaders, $theBody) = explode("\r\n\r\n", $theResponse, 2);
    916964        $theHeaders = WP_Http::processHeaders($theHeaders);
     965
     966        if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] )
     967            $theBody = WP_Http::chunkTransferDecode($theBody);
    917968
    918969        $response = array();
     
    9871038 * @param string $url Site URL to retrieve.
    9881039 * @param array $args Optional. Override the defaults.
    989  * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    990  * @param string $body Optional. The body that should be sent. Expected to be already processed.
    9911040 * @return string The body of the response
    9921041 */
    993 function wp_remote_request($url, $args = array(), $headers = null, $body = null) {
     1042function wp_remote_request($url, $args = array()) {
    9941043    $objFetchSite = _wp_http_get_object();
    995 
    996     return $objFetchSite->request($url, $args, $headers, $body);
     1044    return $objFetchSite->request($url, $args);
    9971045}
    9981046
     
    10061054 * @param string $url Site URL to retrieve.
    10071055 * @param array $args Optional. Override the defaults.
    1008  * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    1009  * @param string $body Optional. The body that should be sent. Expected to be already processed.
    10101056 * @return string The body of the response
    10111057 */
    1012 function wp_remote_get($url, $args = array(), $headers = null, $body = null) {
     1058function wp_remote_get($url, $args = array()) {
    10131059    $objFetchSite = _wp_http_get_object();
    10141060
    1015     return $objFetchSite->get($url, $args, $headers, $body);
     1061    return $objFetchSite->get($url, $args);
    10161062}
    10171063
     
    10251071 * @param string $url Site URL to retrieve.
    10261072 * @param array $args Optional. Override the defaults.
    1027  * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    1028  * @param string $body Optional. The body that should be sent. Expected to be already processed.
    10291073 * @return string The body of the response
    10301074 */
    1031 function wp_remote_post($url, $args = array(), $headers = null, $body = null) {
     1075function wp_remote_post($url, $args = array()) {
    10321076    $objFetchSite = _wp_http_get_object();
    1033 
    1034     return $objFetchSite->post($url, $args, $headers, $body);
     1077    return $objFetchSite->post($url, $args);
    10351078}
    10361079
     
    10441087 * @param string $url Site URL to retrieve.
    10451088 * @param array $args Optional. Override the defaults.
    1046  * @param string|array $headers Optional. Either the header string or array of Header name and value pairs.
    1047  * @param string $body Optional. The body that should be sent. Expected to be already processed.
    10481089 * @return string The body of the response
    10491090 */
    1050 function wp_remote_head($url, $args = array(), $headers = null, $body = null) {
     1091function wp_remote_head($url, $args = array()) {
    10511092    $objFetchSite = _wp_http_get_object();
    1052 
    1053     return $objFetchSite->head($url, $args, $headers, $body);
     1093    return $objFetchSite->head($url, $args);
    10541094}
    10551095
  • trunk/wp-includes/update.php

    r8595 r8630  
    11<?php
    22/**
    3  * A simple set of functions to check our version 1.0 update service
     3 * A simple set of functions to check our version 1.0 update service.
    44 *
    55 * @package WordPress
     
    88
    99/**
    10  * Check WordPress version against the newest version.   *
     10 * Check WordPress version against the newest version.
     11 *
    1112 * The WordPress version, PHP version, and Locale is sent. Checks against the
    1213 * WordPress server at api.wordpress.org server. Will only check if WordPress
    1314 * isn't installing.
    14 
    1515 *
    1616 * @package WordPress
     
    4242
    4343    $url = "http://api.wordpress.org/core/version-check/1.2/?version=$wp_version&php=$php_version&locale=$locale";
     44
    4445    $options = array('timeout' => 3);
    45 
    46     $headers = array(
     46    $options['headers'] = array(
    4747        'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
    4848        'User-Agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
    4949    );
    5050
    51     $response = wp_remote_request($url, $options, $headers);
     51    $response = wp_remote_request($url, $options);
     52
     53    if ( is_wp_error( $response ) )
     54        return false;
    5255
    5356    if ( 200 != $response['response']['code'] )
    5457        return false;
    5558
    56     $body = $response['body'];
    57     $body = trim( $body );
     59    $body = trim( $response['body'] );
    5860    $body = str_replace(array("\r\n", "\r"), "\n", $body);
    5961    $returns = explode("\n", $body);
     
    7476
    7577/**
    76  * wp_update_plugins() - Check plugin versions against the latest versions hosted on WordPress.org.
     78 * Check plugin versions against the latest versions hosted on WordPress.org.
    7779 *
    78  * The WordPress version, PHP version, and Locale is sent along with a list of all plugins installed.
    79  * Checks against the WordPress server at api.wordpress.org.
    80  * Will only check if PHP has fsockopen enabled and WordPress isn't installing.
     80 * The WordPress version, PHP version, and Locale is sent along with a list of
     81 * all plugins installed. Checks against the WordPress server at
     82 * api.wordpress.org. Will only check if PHP has fsockopen enabled and WordPress
     83 * isn't installing.
    8184 *
    8285 * @package WordPress
     
    8992    global $wp_version;
    9093
    91     if ( !function_exists('fsockopen') || defined('WP_INSTALLING') )
     94    if ( defined('WP_INSTALLING') )
    9295        return false;
    9396
     
    117120    }
    118121
    119     foreach ( (array) $current->response as $plugin_file => $update_details ) {
    120         if ( ! isset($plugins[ $plugin_file ]) ) {
    121             $plugin_changed = true;
     122    if ( isset ( $current->response ) && is_array( $current->response ) ) {
     123        foreach ( $current->response as $plugin_file => $update_details ) {
     124            if ( ! isset($plugins[ $plugin_file ]) ) {
     125                $plugin_changed = true;
     126            }
    122127        }
    123128    }
     
    130135    $to_send->active = $active;
    131136    $send = serialize( $to_send );
     137    $body = 'plugins=' . urlencode( $send );
    132138
    133     $request = 'plugins=' . urlencode( $send );
    134     $http_request  = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
    135     $http_request .= "Host: api.wordpress.org\r\n";
    136     $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
    137     $http_request .= "Content-Length: " . strlen($request) . "\r\n";
    138     $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
    139     $http_request .= "\r\n";
    140     $http_request .= $request;
     139    $options = array('method' => 'POST', 'timeout' => 3, 'body' => $body);
     140    $options['headers'] = array(
     141        'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
     142        'Content-Length' => strlen($body),
     143        'User-Agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
     144    );
    141145
    142     $response = '';
    143     if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
    144         fwrite($fs, $http_request);
     146    $raw_response = wp_remote_request('http://api.wordpress.org/plugins/update-check/1.0/', $options);
    145147
    146         while ( !feof($fs) )
    147             $response .= fgets($fs, 1160); // One TCP-IP packet
    148         fclose($fs);
    149         $response = explode("\r\n\r\n", $response, 2);
     148    if( 200 != $raw_response['response']['code'] ) {
     149        return false;
    150150    }
    151151
    152     $response = unserialize( $response[1] );
     152    $response = unserialize( $raw_response['body'] );
    153153
    154154    if ( $response )
Note: See TracChangeset for help on using the changeset viewer.