Make WordPress Core

Ticket #16236: 16236.7.diff

File 16236.7.diff, 12.3 KB (added by sivel, 15 years ago)

Docs, cleanup and blacklist ExtHttp transport for streaming to file

  • wp-includes/class-http.php

     
    227227                        'body' => null,
    228228                        'compress' => false,
    229229                        'decompress' => true,
    230                         'sslverify' => true
     230                        'sslverify' => true,
     231                        'stream' => false,
     232                        'filename' => null
    231233                );
    232234
    233235                $r = wp_parse_args( $args, $defaults );
     
    255257                $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host'];
    256258                unset( $homeURL );
    257259
     260                // If we are streaming to a file but no filename was given drop it in the WP temp dir
     261                // and pick it's name using the basename of the $url
     262                if ( $r['stream']  && ! $r['filename'] )
     263                        $r['filename'] = get_temp_dir() . basename( $url );
     264
     265                // Force some settings if we are streaming to a file and check for existence and perms of destination directory
     266                if ( $r['stream'] ) {
     267                        $r['blocking'] = true;
     268                        if ( ! is_dir( dirname( $r['filename'] ) ) || ! is_writable( dirname( $r['filename'] ) ) )
     269                                return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
     270                }
     271
    258272                if ( is_null( $r['headers'] ) )
    259273                        $r['headers'] = array();
    260274
     
    748762                }
    749763
    750764                $strResponse = '';
    751                 while ( ! feof($handle) )
    752                         $strResponse .= fread($handle, 4096);
     765                $bodyStarted = false;
    753766
    754                 fclose($handle);
     767                // If streaming to a file setup the file handle
     768                if ( $r['stream'] )
     769                        $stream_handle = fopen( $r['filename'], 'w+' );
    755770
     771                while ( ! feof($handle) ) {
     772                        if ( $r['stream'] ) {
     773                                $block = fread( $handle, 4096 );
     774                                if ( $bodyStarted ) {
     775                                        fwrite( $stream_handle, $block );
     776                                } else {
     777                                        $strResponse .= $block;
     778                                        if ( strpos( $strResponse, "\r\n\r\n" ) ) {
     779                                                $process = WP_Http::processResponse( $strResponse );
     780                                                $bodyStarted = true;
     781                                                fwrite( $stream_handle, $process['body'] );
     782                                                unset( $strResponse );
     783                                                $process['body'] = '';
     784                                        }
     785                                }
     786                        } else {
     787                                $strResponse .= fread( $handle, 4096 );
     788                        }
     789                }
     790
     791                fclose( $handle );
     792
    756793                if ( true === $secure_transport )
    757794                        error_reporting($error_reporting);
    758795
    759                 $process = WP_Http::processResponse($strResponse);
    760                 $arrHeaders = WP_Http::processHeaders($process['headers']);
     796                if ( $r['stream'] ) {
     797                        fclose( $stream_handle );
     798                        $arrHeaders = WP_Http::processHeaders( $process['headers'] );
     799                } else {
     800                        $process = WP_Http::processResponse( $strResponse );
     801                        $arrHeaders = WP_Http::processHeaders( $process['headers'] );
     802                        unset( $strResponse );
     803                }
    761804
    762805                // Is the response code within the 400 range?
    763806                if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 )
     
    779822                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) )
    780823                        $process['body'] = WP_Http_Encoding::decompress( $process['body'] );
    781824
    782                 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']);
     825                return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename'] );
    783826        }
    784827
    785828        /**
     
    10751118                        return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
    10761119                }
    10771120
    1078                 $strResponse = stream_get_contents($handle);
    1079                 $meta = stream_get_meta_data($handle);
     1121                if ( $r['stream'] ) {
     1122                        $stream_handle = fopen( $r['filename'], 'w+' );
     1123                        stream_copy_to_stream( $handle, $stream_handle );
     1124                        fclose( $stream_handle );
     1125                        $strResponse = '';
     1126                } else {
     1127                        $strResponse = stream_get_contents( $handle );
     1128                }
    10801129
    1081                 fclose($handle);
     1130                $meta = stream_get_meta_data( $handle );
    10821131
     1132                fclose( $handle );
     1133
    10831134                $processedHeaders = array();
    10841135                if ( isset( $meta['wrapper_data']['headers'] ) )
    10851136                        $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']['headers']);
     
    10921143                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) )
    10931144                        $strResponse = WP_Http_Encoding::decompress( $strResponse );
    10941145
    1095                 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
     1146                return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename'] );
    10961147        }
    10971148
    10981149        /**
     
    12751326         *
    12761327         * @return boolean False means this class can not be used, true means it can.
    12771328         */
    1278         function test($args = array()) {
    1279                 return apply_filters('use_http_extension_transport', function_exists('http_request'), $args );
     1329        function test( $args = array() ) {
     1330                $use = true;
     1331
     1332                if ( ! function_exists( 'http_request' ) )
     1333                        $use = false;
     1334
     1335                $stream = isset( $args['stream'] ) && $args['stream'];
     1336
     1337                if ( $stream )
     1338                        $use = false;
     1339
     1340                return apply_filters( 'use_http_extension_transport', $use, $args );
    12801341        }
    12811342}
    12821343
     
    12921353class WP_Http_Curl {
    12931354
    12941355        /**
     1356         * Temporary header storage for use with streaming to a file.
     1357         *
     1358         * @since 3.2.0
     1359         * @access private
     1360         * @var string
     1361         */
     1362        private $headers;
     1363
     1364        /**
    12951365         * Send a HTTP request to a URI using cURL extension.
    12961366         *
    12971367         * @access public
     
    13871457                else
    13881458                        curl_setopt( $handle, CURLOPT_HEADER, false );
    13891459
     1460                // If streaming to a file open a file handle, and setup our callback for grabbing the headers
     1461                if ( $r['stream'] ) {
     1462                        $stream_handle = fopen( $r['filename'], 'w+' );
     1463                        curl_setopt( $handle, CURLOPT_FILE, $stream_handle );
     1464                        curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) );
     1465                }
     1466
    13901467                // The option doesn't work with safe mode or when open_basedir is set.
    13911468                // Disable HEAD when making HEAD requests.
    13921469                if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] )
     
    14191496
    14201497                $theResponse = curl_exec( $handle );
    14211498
    1422                 if ( !empty($theResponse) ) {
    1423                         $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
    1424                         $theHeaders = trim( substr($theResponse, 0, $headerLength) );
    1425                         if ( strlen($theResponse) > $headerLength )
    1426                                 $theBody = substr( $theResponse, $headerLength );
    1427                         else
     1499                if ( ! empty( $theResponse ) ) {
     1500                        // If we are streaming to a file the $headers property will hold our headers
     1501                        if ( ! empty( $this->headers ) ) {
     1502                                $theHeaders = $this->headers;
     1503                                unset( $this->headers );
    14281504                                $theBody = '';
    1429                         if ( false !== strpos($theHeaders, "\r\n\r\n") ) {
    1430                                 $headerParts = explode("\r\n\r\n", $theHeaders);
    1431                                 $theHeaders = $headerParts[ count($headerParts) -1 ];
     1505                        } else {
     1506                                $headerLength = curl_getinfo( $handle, CURLINFO_HEADER_SIZE );
     1507                                $theHeaders = trim( substr( $theResponse, 0, $headerLength ) );
     1508                                if ( strlen( $theResponse ) > $headerLength )
     1509                                        $theBody = substr( $theResponse, $headerLength );
     1510                                else
     1511                                        $theBody = '';
     1512                                if ( false !== strpos( $theHeaders, "\r\n\r\n" ) ) {
     1513                                        $headerParts = explode( "\r\n\r\n", $theHeaders );
     1514                                        $theHeaders = $headerParts[ count( $headerParts ) - 1 ];
     1515                                }
    14321516                        }
    1433                         $theHeaders = WP_Http::processHeaders($theHeaders);
     1517                        $theHeaders = WP_Http::processHeaders( $theHeaders );
    14341518                } else {
    1435                         if ( $curl_error = curl_error($handle) )
    1436                                 return new WP_Error('http_request_failed', $curl_error);
    1437                         if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) )
    1438                                 return new WP_Error('http_request_failed', __('Too many redirects.'));
     1519                        if ( $curl_error = curl_error( $handle ) )
     1520                                return new WP_Error( 'http_request_failed', $curl_error );
     1521                        if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) )
     1522                                return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
    14391523
    14401524                        $theHeaders = array( 'headers' => array(), 'cookies' => array() );
    14411525                        $theBody = '';
     
    14471531
    14481532                curl_close( $handle );
    14491533
     1534                if ( $r['stream'] )
     1535                        fclose( $stream_handle );
     1536
    14501537                // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
    14511538                if ( !empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) ) {
    14521539                        if ( $r['redirection']-- > 0 ) {
     
    14591546                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) )
    14601547                        $theBody = WP_Http_Encoding::decompress( $theBody );
    14611548
    1462                 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']);
     1549                return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] );
    14631550        }
    14641551
    14651552        /**
     1553         * Grab the headers of the cURL request when steraming to a file
     1554         *
     1555         * Each header is sent individually to this callback, so we append to the $header property for temporary storage
     1556         *
     1557         * @since 3.2.0
     1558         * @access private
     1559         * @return int
     1560         */
     1561        private function stream_headers( $handle, $headers ) {
     1562                $this->headers .= $headers;
     1563                return strlen( $headers );
     1564        }
     1565
     1566        /**
    14661567         * Whether this class can be used for retrieving an URL.
    14671568         *
    14681569         * @static
     
    14701571         *
    14711572         * @return boolean False means this class can not be used, true means it can.
    14721573         */
    1473         function test($args = array()) {
     1574        function test( $args = array() ) {
    14741575                if ( function_exists('curl_init') && function_exists('curl_exec') )
    14751576                        return apply_filters('use_curl_transport', true, $args);
    14761577
  • wp-includes/functions.php

     
    21112111}
    21122112
    21132113/**
     2114 * Determines a writable directory for temporary files.
     2115 * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/
     2116 *
     2117 * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file.
     2118 *
     2119 * @since 2.5.0
     2120 *
     2121 * @return string Writable temporary directory
     2122 */
     2123function get_temp_dir() {
     2124        static $temp;
     2125        if ( defined('WP_TEMP_DIR') )
     2126                return trailingslashit(WP_TEMP_DIR);
     2127
     2128        if ( $temp )
     2129                return trailingslashit($temp);
     2130
     2131        $temp = WP_CONTENT_DIR . '/';
     2132        if ( is_dir($temp) && @is_writable($temp) )
     2133                return $temp;
     2134
     2135        if  ( function_exists('sys_get_temp_dir') ) {
     2136                $temp = sys_get_temp_dir();
     2137                if ( @is_writable($temp) )
     2138                        return trailingslashit($temp);
     2139        }
     2140
     2141        $temp = ini_get('upload_tmp_dir');
     2142        if ( is_dir($temp) && @is_writable($temp) )
     2143                return trailingslashit($temp);
     2144
     2145        $temp = '/tmp/';
     2146        return $temp;
     2147}
     2148
     2149/**
    21142150 * Get an array containing the current upload directory's path and url.
    21152151 *
    21162152 * Checks the 'upload_path' option, which should be from the web root folder,
  • wp-admin/includes/file.php

     
    153153}
    154154
    155155/**
    156  * Determines a writable directory for temporary files.
    157  * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/
    158  *
    159  * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file.
    160  *
    161  * @since 2.5.0
    162  *
    163  * @return string Writable temporary directory
    164  */
    165 function get_temp_dir() {
    166         static $temp;
    167         if ( defined('WP_TEMP_DIR') )
    168                 return trailingslashit(WP_TEMP_DIR);
    169 
    170         if ( $temp )
    171                 return trailingslashit($temp);
    172 
    173         $temp = WP_CONTENT_DIR . '/';
    174         if ( is_dir($temp) && @is_writable($temp) )
    175                 return $temp;
    176 
    177         if  ( function_exists('sys_get_temp_dir') ) {
    178                 $temp = sys_get_temp_dir();
    179                 if ( @is_writable($temp) )
    180                         return trailingslashit($temp);
    181         }
    182 
    183         $temp = ini_get('upload_tmp_dir');
    184         if ( is_dir($temp) && @is_writable($temp) )
    185                 return trailingslashit($temp);
    186 
    187         $temp = '/tmp/';
    188         return $temp;
    189 }
    190 
    191 /**
    192156 * Returns a filename of a Temporary unique file.
    193157 * Please note that the calling function must unlink() this itself.
    194158 *