WordPress.org

Make WordPress Core

Changeset 25303


Ignore:
Timestamp:
09/09/2013 01:17:17 AM (7 years ago)
Author:
dd32
Message:

WP_HTTP: When streaming to file, ensure that fwrite() suceeds and correctly writes the file to disk. Fixes #16057

File:
1 edited

Legend:

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

    r25225 r25303  
    865865                }
    866866
    867                 if ( isset( $r['limit_response_size'] ) && ( $bytes_written + strlen( $block ) ) > $r['limit_response_size'] )
     867                $this_block_size = strlen( $block );
     868
     869                if ( isset( $r['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $r['limit_response_size'] )
    868870                    $block = substr( $block, 0, ( $r['limit_response_size'] - $bytes_written ) );
    869871
    870                 $bytes_written += fwrite( $stream_handle, $block );
     872                $bytes_written_to_file = fwrite( $stream_handle, $block );
     873
     874                if ( $bytes_written_to_file != $this_block_size ) {
     875                    fclose( $handle );
     876                    fclose( $stream_handle );
     877                    return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
     878                }
     879
     880                $bytes_written += $bytes_written_to_file;
    871881
    872882                $keep_reading = !isset( $r['limit_response_size'] ) || $bytes_written < $r['limit_response_size'];
     
    12301240        $this->body = '';
    12311241
    1232         // If no response
    1233         if ( 0 == strlen( $theBody ) && empty( $theHeaders['headers'] ) ) {
     1242        $curl_error = curl_errno( $handle );
     1243
     1244        // If an error occured, or, no response
     1245        if ( $curl_error || ( 0 == strlen( $theBody ) && empty( $theHeaders['headers'] ) ) ) {
     1246            if ( CURLE_WRITE_ERROR /* 23 */ == $curl_error &&  $r['stream'] ) {
     1247                fclose( $this->stream_handle );
     1248                return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
     1249            }
    12341250            if ( $curl_error = curl_error( $handle ) ) {
    12351251                curl_close( $handle );
     
    13011317        }
    13021318
    1303         if ( $this->max_body_length && ( strlen( $this->body ) + strlen( $data ) ) > $this->max_body_length )
    1304             $data = substr( $data, 0, ( $this->max_body_length - strlen( $this->body ) ) );
    1305 
    1306         if ( $this->stream_handle )
    1307             fwrite( $this->stream_handle, $data );
    1308         else
     1319        $data_length = strlen( $data );
     1320
     1321        if ( $this->max_body_length && ( strlen( $this->body ) + $data_length ) > $this->max_body_length )
     1322            $data = substr( $data, 0, ( $this->max_body_length - $data_length ) );
     1323
     1324        if ( $this->stream_handle ) {
     1325            $bytes_written = fwrite( $this->stream_handle, $data );
     1326        } else {
    13091327            $this->body .= $data;
    1310 
    1311         $data_length = strlen( $data );
     1328            $bytes_written = $data_length;
     1329        }
    13121330
    13131331        if ( isset( $mb_encoding ) )
    13141332            mb_internal_encoding( $mb_encoding );
    13151333
    1316         return $data_length;
     1334        // Upon event of this function returning less than strlen( $data ) curl will error with CURLE_WRITE_ERROR
     1335        return $bytes_written;
    13171336    }
    13181337
Note: See TracChangeset for help on using the changeset viewer.