Make WordPress Core

Changeset 42773


Ignore:
Timestamp:
03/04/2018 05:13:35 PM (7 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r42761 r42773  
    994994    }
    995995
    996     if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
     996    $response_code = wp_remote_retrieve_response_code( $response );
     997
     998    if ( 200 != $response_code ) {
     999        $data = array(
     1000            'code' => $response_code,
     1001        );
     1002
     1003        // Retrieve a sample of the response body for debugging purposes.
     1004        $tmpf = fopen( $tmpfname, 'rb' );
     1005        if ( $tmpf ) {
     1006            /**
     1007             * Filters the maximum error response body size in `download_url()`.
     1008             *
     1009             * @since 5.0.0
     1010             *
     1011             * @see download_url()
     1012             *
     1013             * @param int $size The maximum error response body size. Default 1 KB.
     1014             */
     1015            $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
     1016            $data['body']  = fread( $tmpf, $response_size );
     1017            fclose( $tmpf );
     1018        }
     1019
    9971020        unlink( $tmpfname );
    998         return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
     1021        return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
    9991022    }
    10001023
  • trunk/tests/phpunit/tests/admin/includesFile.php

    r42343 r42773  
    3232        $_SERVER['SCRIPT_FILENAME'] = $sfn;
    3333    }
     34
     35    /**
     36     * @ticket 43329
     37     */
     38    public function test_download_url_non_200_response_code() {
     39        add_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ), 10, 3 );
     40
     41        $error = download_url( 'test_download_url_non_200' );
     42        $this->assertWPError( $error );
     43        $this->assertEquals( array(
     44            'code' => 418,
     45            'body' => 'This is an unexpected error message from your favorite server.',
     46        ), $error->get_error_data() );
     47
     48        add_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );
     49
     50        $error = download_url( 'test_download_url_non_200' );
     51        $this->assertWPError( $error );
     52        $this->assertEquals( array(
     53            'code' => 418,
     54            'body' => 'This ',
     55        ), $error->get_error_data() );
     56
     57        remove_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );
     58        remove_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ) );
     59    }
     60
     61    public function _fake_download_url_non_200_response_code( $response, $args, $url ) {
     62        file_put_contents( $args['filename'], 'This is an unexpected error message from your favorite server.' );
     63        return array(
     64            'response' => array(
     65                'code'    => 418,
     66                'message' => "I'm a teapot!",
     67            ),
     68        );
     69    }
     70
     71    public function __return_5() {
     72        return 5;
     73    }
    3474}
Note: See TracChangeset for help on using the changeset viewer.