Make WordPress Core


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

File:
1 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.
Note: See TracChangeset for help on using the changeset viewer.