Changeset 29968
- Timestamp:
- 10/20/2014 07:31:45 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-http.php
r29864 r29968 209 209 * and pick its name using the basename of the $url. 210 210 */ 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 } 213 214 214 215 /* … … 1097 1098 $this_block_size = strlen( $block ); 1098 1099 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 } 1101 1104 1102 1105 $bytes_written_to_file = fwrite( $stream_handle, $block ); … … 1324 1327 */ 1325 1328 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; 1326 1338 1327 1339 /** … … 1497 1509 $theHeaders = WP_Http::processHeaders( $this->headers, $url ); 1498 1510 $theBody = $this->body; 1511 $bytes_written_total = $this->bytes_written_total; 1499 1512 1500 1513 $this->headers = ''; 1501 1514 $this->body = ''; 1515 $this->bytes_written_total = 0; 1502 1516 1503 1517 $curl_error = curl_errno( $handle ); … … 1505 1519 // If an error occurred, or, no response. 1506 1520 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 } 1514 1531 } 1515 1532 if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) { … … 1562 1579 * 1563 1580 * 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 1565 1582 * 1566 1583 * @since 3.6.0 … … 1571 1588 $data_length = strlen( $data ); 1572 1589 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 } 1575 1594 1576 1595 if ( $this->stream_handle ) { … … 1580 1599 $bytes_written = $data_length; 1581 1600 } 1601 1602 $this->bytes_written_total += $bytes_written; 1582 1603 1583 1604 // 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 185 185 $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename. 186 186 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 187 193 $this->assertFalse( is_wp_error( $res ) ); 188 194 $this->assertEquals( '', $res['body'] ); // The body should be empty. 189 195 $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 193 216 } 194 217
Note: See TracChangeset
for help on using the changeset viewer.