Make WordPress Core

Changeset 29968


Ignore:
Timestamp:
10/20/2014 07:31:45 AM (10 years ago)
Author:
dd32
Message:

HTTP API: Support both the 'limit_response_size' and 'stream' parameters at the same time, allowing a partial file download.
Fixes #26726

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-http.php

    r29864 r29968  
    209209         * and pick its name using the basename of the $url.
    210210         */
    211         if ( $r['stream']  && empty( $r['filename'] ) )
    212             $r['filename'] = get_temp_dir() . basename( $url );
     211        if ( $r['stream']  && empty( $r['filename'] ) ) {
     212            $r['filename'] = wp_unique_filename( get_temp_dir(), basename( $url ) );
     213        }
    213214
    214215        /*
     
    10971098                $this_block_size = strlen( $block );
    10981099
    1099                 if ( isset( $r['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $r['limit_response_size'] )
    1100                     $block = substr( $block, 0, ( $r['limit_response_size'] - $bytes_written ) );
     1100                if ( isset( $r['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $r['limit_response_size'] ) {
     1101                    $this_block_size = ( $r['limit_response_size'] - $bytes_written );
     1102                    $block = substr( $block, 0, $this_block_size );
     1103                }
    11011104
    11021105                $bytes_written_to_file = fwrite( $stream_handle, $block );
     
    13241327     */
    13251328    private $stream_handle = false;
     1329
     1330    /**
     1331     * The total bytes written in the current request.
     1332     *
     1333     * @since 4.1.0
     1334     * @access prviate
     1335     * @var int
     1336     */
     1337    private $bytes_written_total = 0;
    13261338
    13271339    /**
     
    14971509        $theHeaders = WP_Http::processHeaders( $this->headers, $url );
    14981510        $theBody = $this->body;
     1511        $bytes_written_total = $this->bytes_written_total;
    14991512
    15001513        $this->headers = '';
    15011514        $this->body = '';
     1515        $this->bytes_written_total = 0;
    15021516
    15031517        $curl_error = curl_errno( $handle );
     
    15051519        // If an error occurred, or, no response.
    15061520        if ( $curl_error || ( 0 == strlen( $theBody ) && empty( $theHeaders['headers'] ) ) ) {
    1507             if ( CURLE_WRITE_ERROR /* 23 */ == $curl_error &&  $r['stream'] ) {
    1508                 fclose( $this->stream_handle );
    1509                 return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
    1510             }
    1511             if ( $curl_error = curl_error( $handle ) ) {
    1512                 curl_close( $handle );
    1513                 return new WP_Error( 'http_request_failed', $curl_error );
     1521            if ( CURLE_WRITE_ERROR /* 23 */ == $curl_error && $r['stream'] ) {
     1522                if ( ! $this->max_body_length || $this->max_body_length != $bytes_written_total ) {
     1523                    fclose( $this->stream_handle );
     1524                    return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
     1525                }
     1526            } else {
     1527                if ( $curl_error = curl_error( $handle ) ) {
     1528                    curl_close( $handle );
     1529                    return new WP_Error( 'http_request_failed', $curl_error );
     1530                }
    15141531            }
    15151532            if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) {
     
    15621579     *
    15631580     * The contents of the document are passed in chunks, so we append to the $body property for temporary storage.
    1564      * Returning a length shorter than the length of $data passed in will cause cURL to abort the request as "completed"
     1581     * Returning a length shorter than the length of $data passed in will cause cURL to abort the request with CURLE_WRITE_ERROR
    15651582     *
    15661583     * @since 3.6.0
     
    15711588        $data_length = strlen( $data );
    15721589
    1573         if ( $this->max_body_length && ( strlen( $this->body ) + $data_length ) > $this->max_body_length )
    1574             $data = substr( $data, 0, ( $this->max_body_length - $data_length ) );
     1590        if ( $this->max_body_length && ( $this->bytes_written_total + $data_length ) > $this->max_body_length ) {
     1591            $data_length = ( $this->max_body_length - $this->bytes_written_total );
     1592            $data = substr( $data, 0, $data_length );
     1593        }
    15751594
    15761595        if ( $this->stream_handle ) {
     
    15801599            $bytes_written = $data_length;
    15811600        }
     1601
     1602        $this->bytes_written_total += $bytes_written;
    15821603
    15831604        // Upon event of this function returning less than strlen( $data ) curl will error with CURLE_WRITE_ERROR.
  • trunk/tests/phpunit/tests/http/base.php

    r29120 r29968  
    185185        $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename.
    186186
     187        // Cleanup before we assert, as it'll return early.
     188        if ( ! is_wp_error( $res ) ) {
     189            $filesize = filesize( $res['filename'] );
     190            unlink( $res['filename'] );
     191        }       
     192
    187193        $this->assertFalse( is_wp_error( $res ) );
    188194        $this->assertEquals( '', $res['body'] ); // The body should be empty.
    189195        $this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..)
    190         $this->assertEquals( $size, filesize($res['filename']) ); // Check that the file is written to disk correctly without any extra characters
    191 
    192         unlink($res['filename']); // Remove the temporary file
     196        $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
     197    }
     198
     199    /**
     200     * @ticket 26726
     201     */
     202    function test_file_stream_limited_size() {
     203        $url = 'http://unit-tests.svn.wordpress.org/trunk/data/images/2004-07-22-DSC_0007.jpg'; // we'll test against a file in the unit test data
     204        $size = 10000;
     205        $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30, 'limit_response_size' => $size ) ); //Auto generate the filename.
     206
     207        // Cleanup before we assert, as it'll return early.
     208        if ( ! is_wp_error( $res ) ) {
     209            $filesize = filesize( $res['filename'] );
     210            unlink( $res['filename'] );
     211        }
     212
     213        $this->assertFalse( is_wp_error( $res ) );
     214        $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
     215
    193216    }
    194217
Note: See TracChangeset for help on using the changeset viewer.