Make WordPress Core

Ticket #16236: 16236.5.diff

File 16236.5.diff, 9.2 KB (added by sivel, 15 years ago)

Performance improvement for fsockopen, to prevent inspecting every block after the headers are finished

  • 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 ( $r['stream']  && ! $r['filename'] )
     261                        $r['filename'] = get_temp_dir() . basename( $url );
     262
     263                if ( $r['stream'] ) {
     264                        $r['blocking'] = true;
     265                        if ( ! is_dir( dirname( $r['filename'] ) ) || ! is_writable( dirname( $r['filename'] ) ) )
     266                                return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
     267                }
     268
    258269                if ( is_null( $r['headers'] ) )
    259270                        $r['headers'] = array();
    260271
     
    748759                }
    749760
    750761                $strResponse = '';
    751                 while ( ! feof($handle) )
    752                         $strResponse .= fread($handle, 4096);
     762                $theHeaders = '';
     763                $bodyStarted = false;
    753764
     765                if ( $r['stream'] )
     766                        $stream_handle = fopen( $r['filename'], 'w+' );
     767
     768                while ( ! feof($handle) ) {
     769                        if ( $r['stream'] ) {
     770                                $block = fread( $handle, 4096 );
     771                                if ( $bodyStarted ) {
     772                                        fwrite( $stream_handle, $block );
     773                                } else {
     774                                        if ( strpos( $block, "\r\n\r\n" ) ) {
     775                                                $res = explode("\r\n\r\n", $block, 2);
     776                                                $bodyStarted = true;
     777                                                $theHeaders .= $res[0];
     778                                                fwrite( $stream_handle, $res[1] );                                     
     779                                        } else {
     780                                                $theHeaders .= $block;
     781                                        }
     782                                }
     783                        } else {
     784                                $strResponse .= fread($handle, 4096);
     785                        }
     786                }
     787
    754788                fclose($handle);
    755789
    756790                if ( true === $secure_transport )
    757791                        error_reporting($error_reporting);
    758792
    759                 $process = WP_Http::processResponse($strResponse);
    760                 $arrHeaders = WP_Http::processHeaders($process['headers']);
     793                if ( $r['stream'] ) {
     794                        $process = array(
     795                                'body' => '',
     796                                'headers' => $theHeaders
     797                        );
     798                        $arrHeaders = WP_Http::processHeaders( $process['headers'] );
     799                } else {
     800                        $process = WP_Http::processResponse($strResponse);
     801                        $arrHeaders = WP_Http::processHeaders($process['headers']);
     802                }
    761803
    762804                // Is the response code within the 400 range?
    763805                if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 )
     
    779821                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) )
    780822                        $process['body'] = WP_Http_Encoding::decompress( $process['body'] );
    781823
    782                 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']);
     824                return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename']);
    783825        }
    784826
    785827        /**
     
    10751117                        return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
    10761118                }
    10771119
    1078                 $strResponse = stream_get_contents($handle);
     1120                if ( $r['stream'] ) {
     1121                        $stream_handle = fopen( $r['filename'], 'w+' );
     1122                        stream_copy_to_stream( $handle, $stream_handle );
     1123                        $strResponse = '';
     1124                } else {
     1125                        $strResponse = stream_get_contents($handle);
     1126                }
    10791127                $meta = stream_get_meta_data($handle);
    10801128
    10811129                fclose($handle);
     
    10921140                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) )
    10931141                        $strResponse = WP_Http_Encoding::decompress( $strResponse );
    10941142
    1095                 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
     1143                return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename']);
    10961144        }
    10971145
    10981146        /**
     
    12911339 */
    12921340class WP_Http_Curl {
    12931341
     1342        private $headers;
     1343
    12941344        /**
    12951345         * Send a HTTP request to a URI using cURL extension.
    12961346         *
     
    13871437                else
    13881438                        curl_setopt( $handle, CURLOPT_HEADER, false );
    13891439
     1440                if ( $r['stream'] ) {
     1441                        $stream_handle = fopen( $r['filename'], 'w+' );
     1442                        curl_setopt( $handle, CURLOPT_FILE, $stream_handle );
     1443                        curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) );
     1444                }
     1445
    13901446                // The option doesn't work with safe mode or when open_basedir is set.
    13911447                // Disable HEAD when making HEAD requests.
    13921448                if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] )
     
    14201476                $theResponse = curl_exec( $handle );
    14211477
    14221478                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
     1479                        if ( ! empty( $this->headers ) ) {
     1480                                $theHeaders = $this->headers;
    14281481                                $theBody = '';
    1429                         if ( false !== strpos($theHeaders, "\r\n\r\n") ) {
    1430                                 $headerParts = explode("\r\n\r\n", $theHeaders);
    1431                                 $theHeaders = $headerParts[ count($headerParts) -1 ];
     1482                        } else {
     1483                                $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
     1484                                $theHeaders = trim( substr($theResponse, 0, $headerLength) );
     1485                                if ( strlen($theResponse) > $headerLength )
     1486                                        $theBody = substr( $theResponse, $headerLength );
     1487                                else
     1488                                        $theBody = '';
     1489                                if ( false !== strpos($theHeaders, "\r\n\r\n") ) {
     1490                                        $headerParts = explode("\r\n\r\n", $theHeaders);
     1491                                        $theHeaders = $headerParts[ count($headerParts) - 1 ];
     1492                                }
    14321493                        }
    14331494                        $theHeaders = WP_Http::processHeaders($theHeaders);
    14341495                } else {
     
    14591520                if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) )
    14601521                        $theBody = WP_Http_Encoding::decompress( $theBody );
    14611522
    1462                 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']);
     1523                return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename']);
    14631524        }
    14641525
     1526        function stream_headers( $handle, $headers ) {
     1527                $this->headers .= $headers;
     1528                return strlen($headers);
     1529        }
     1530
    14651531        /**
    14661532         * Whether this class can be used for retrieving an URL.
    14671533         *
  • 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 *