diff --git a/src/wp-includes/class-http.php b/src/wp-includes/class-http.php index 6f2e28cc22..2b27658f89 100644 --- a/src/wp-includes/class-http.php +++ b/src/wp-includes/class-http.php @@ -1520,10 +1520,16 @@ class WP_Http_Curl { // If an error occurred, or, no response. if ( $curl_error || ( 0 == strlen( $theBody ) && empty( $theHeaders['headers'] ) ) ) { - if ( CURLE_WRITE_ERROR /* 23 */ == $curl_error && $r['stream'] ) { + if ( CURLE_WRITE_ERROR /* 23 */ == $curl_error ) { if ( ! $this->max_body_length || $this->max_body_length != $bytes_written_total ) { - fclose( $this->stream_handle ); - return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) ); + if ( $r['stream'] ) { + curl_close( $handle ); + fclose( $this->stream_handle ); + return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) ); + } else { + curl_close( $handle ); + return new WP_Error( 'http_request_failed', curl_error( $handle ) ); + } } } else { if ( $curl_error = curl_error( $handle ) ) { diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 63a5aa14bc..e4af6b8744 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -215,6 +215,22 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { } + /** + * Tests Limiting the response size when returning strings + * + * @ticket 31172 + */ + function test_request_limited_size() { + // we'll test against a file in the unit test data + $url = 'http://develop.svn.wordpress.org/trunk/tests/phpunit/data/images/2004-07-22-DSC_0007.jpg'; + $size = 10000; + + $res = wp_remote_request( $url, array( 'timeout' => 30, 'limit_response_size' => $size ) ); + + $this->assertFalse( is_wp_error( $res ) ); + $this->assertEquals( $size, strlen( $res['body'] ) ); + } + /** * Test POST redirection methods *