[wp-trac] [WordPress Trac] #43329: download_url function should return the body in response on error

WordPress Trac noreply at wordpress.org
Thu Feb 15 11:18:39 UTC 2018


#43329: download_url function should return the body in response on error
-------------------------+-----------------------------
 Reporter:  nextendweb   |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  4.9.4
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Our non-wordpress.org plugin uses custom url to fetch the update file from
 our server which needs authentication. So, when the downloading of the
 update file fails in WordPress we would like to get the response as it is
 a REST message which contains the exact error message what we should
 present to the user.

 download_url function use wp_safe_remote_get with filename argument which
 stores the body of the response.

 {{{#!php
 <?php
 function download_url( $url, $timeout = 300 ) {
         //WARNING: The file is not automatically deleted, The script must
 unlink() the file.
         if ( ! $url )
                 return new WP_Error('http_no_url', __('Invalid URL
 Provided.'));

         $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );

         $tmpfname = wp_tempnam( $url_filename );
         if ( ! $tmpfname )
                 return new WP_Error('http_no_file', __('Could not create
 Temporary file.'));

         $response = wp_safe_remote_get( $url, array( 'timeout' =>
 $timeout, 'stream' => true, 'filename' => $tmpfname ) );

         if ( is_wp_error( $response ) ) {
                 unlink( $tmpfname );
                 return $response;
         }

         if ( 200 != wp_remote_retrieve_response_code( $response ) ){
                 unlink( $tmpfname );
                 return new WP_Error( 'http_404', trim(
 wp_remote_retrieve_response_message( $response ) ) );
         }

         $content_md5 = wp_remote_retrieve_header( $response, 'content-md5'
 );
         if ( $content_md5 ) {
                 $md5_check = verify_file_md5( $tmpfname, $content_md5 );
                 if ( is_wp_error( $md5_check ) ) {
                         unlink( $tmpfname );
                         return $md5_check;
                 }
         }

         return $tmpfname;
 }
 }}}

 If the wp_safe_remote_get fails we are unable to access to the response
 body as the $tmpfname file is unlinked and does not read into the response
 object.

 {{{#!php
 <?php
         if ( is_wp_error( $response ) ) {
                 unlink( $tmpfname );
                 return $response;
         }

         if ( 200 != wp_remote_retrieve_response_code( $response ) ){
                 unlink( $tmpfname );
                 return new WP_Error( 'http_404', trim(
 wp_remote_retrieve_response_message( $response ) ) );
         }
 }}}

 It would be better if WordPress could read the content of the temporary
 file and parse it as a normal response body in the previous two if
 statement.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43329>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list