Filesystem API: Allow `download_url()` to return the response code and body on error as an additional `WP_Error` object data.

The error response body size is limited to 1 KB by default to avoid taking up too much memory. The size can be increased using `download_url_error_max_body_size` filter.

Props soulseekah, campusboy1987, mihdan, SergeyBiryukov.
Fixes #43329.
Built from https://develop.svn.wordpress.org/trunk@42773


git-svn-id: http://core.svn.wordpress.org/trunk@42603 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2018-03-04 17:14:31 +00:00
parent 8ae59374f3
commit 86c3c7e1c9
2 changed files with 26 additions and 3 deletions

View File

@ -993,9 +993,32 @@ function download_url( $url, $timeout = 300 ) {
return $response;
}
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 != $response_code ) {
$data = array(
'code' => $response_code,
);
// Retrieve a sample of the response body for debugging purposes.
$tmpf = fopen( $tmpfname, 'rb' );
if ( $tmpf ) {
/**
* Filters the maximum error response body size in `download_url()`.
*
* @since 5.0.0
*
* @see download_url()
*
* @param int $size The maximum error response body size. Default 1 KB.
*/
$response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
$data['body'] = fread( $tmpf, $response_size );
fclose( $tmpf );
}
unlink( $tmpfname );
return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
}
$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-42772';
$wp_version = '5.0-alpha-42773';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.