Ticket #33709: 33709.3.diff
File 33709.3.diff, 6.4 KB (added by , 10 years ago) |
---|
-
src/wp-includes/deprecated.php
diff --git src/wp-includes/deprecated.php src/wp-includes/deprecated.php index 582b2fd..914d137 100644
function post_permalink( $post_id = 0 ) { 3561 3561 return get_permalink( $post_id ); 3562 3562 } 3563 3563 3564 /** 3565 * Perform a HTTP HEAD or GET request. 3566 * 3567 * If $file_path is a writable filename, this will do a GET request and write 3568 * the file to that path. 3569 * 3570 * @since 2.5.0 3571 * @deprecated 4.4.0 Use WP_Http 3572 * @see WP_Http 3573 * 3574 * @param string $url URL to fetch. 3575 * @param string|bool $file_path Optional. File path to write request to. Default false. 3576 * @param int $red Optional. The number of Redirects followed, Upon 5 being hit, 3577 * returns false. Default 1. 3578 * @return bool|string False on failure and string of headers if HEAD request. 3579 */ 3580 function wp_get_http( $url, $file_path = false, $red = 1 ) { 3581 _deprecated_function( __FUNCTION__, '4.4', 'WP_Http' ); 3582 3583 @set_time_limit( 60 ); 3584 3585 if ( $red > 5 ) 3586 return false; 3587 3588 $options = array(); 3589 $options['redirection'] = 5; 3590 3591 if ( false == $file_path ) 3592 $options['method'] = 'HEAD'; 3593 else 3594 $options['method'] = 'GET'; 3595 3596 $response = wp_safe_remote_request( $url, $options ); 3597 3598 if ( is_wp_error( $response ) ) 3599 return false; 3600 3601 $headers = wp_remote_retrieve_headers( $response ); 3602 $headers['response'] = wp_remote_retrieve_response_code( $response ); 3603 3604 // WP_HTTP no longer follows redirects for HEAD requests. 3605 if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) { 3606 return wp_get_http( $headers['location'], $file_path, ++$red ); 3607 } 3608 3609 if ( false == $file_path ) 3610 return $headers; 3611 3612 // GET request - write it to the supplied filename 3613 $out_fp = fopen($file_path, 'w'); 3614 if ( !$out_fp ) 3615 return $headers; 3616 3617 fwrite( $out_fp, wp_remote_retrieve_body( $response ) ); 3618 fclose($out_fp); 3619 clearstatcache(); 3620 3621 return $headers; 3622 } -
src/wp-includes/functions.php
diff --git src/wp-includes/functions.php src/wp-includes/functions.php index 9206f9b..2f55528 100644
function do_enclose( $content, $post_ID ) { 564 564 } 565 565 566 566 /** 567 * Perform a HTTP HEAD or GET request.568 *569 * If $file_path is a writable filename, this will do a GET request and write570 * the file to that path.571 *572 * @since 2.5.0573 *574 * @param string $url URL to fetch.575 * @param string|bool $file_path Optional. File path to write request to. Default false.576 * @param int $red Optional. The number of Redirects followed, Upon 5 being hit,577 * returns false. Default 1.578 * @return bool|string False on failure and string of headers if HEAD request.579 */580 function wp_get_http( $url, $file_path = false, $red = 1 ) {581 @set_time_limit( 60 );582 583 if ( $red > 5 )584 return false;585 586 $options = array();587 $options['redirection'] = 5;588 589 if ( false == $file_path )590 $options['method'] = 'HEAD';591 else592 $options['method'] = 'GET';593 594 $response = wp_safe_remote_request( $url, $options );595 596 if ( is_wp_error( $response ) )597 return false;598 599 $headers = wp_remote_retrieve_headers( $response );600 $headers['response'] = wp_remote_retrieve_response_code( $response );601 602 // WP_HTTP no longer follows redirects for HEAD requests.603 if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {604 return wp_get_http( $headers['location'], $file_path, ++$red );605 }606 607 if ( false == $file_path )608 return $headers;609 610 // GET request - write it to the supplied filename611 $out_fp = fopen($file_path, 'w');612 if ( !$out_fp )613 return $headers;614 615 fwrite( $out_fp, wp_remote_retrieve_body( $response ) );616 fclose($out_fp);617 clearstatcache();618 619 return $headers;620 }621 622 /**623 567 * Retrieve HTTP Headers from URL. 624 568 * 625 569 * @since 1.5.1 -
tests/phpunit/tests/http/functions.php
diff --git tests/phpunit/tests/http/functions.php tests/phpunit/tests/http/functions.php index 7bb60e8..2547b2d 100644
class Tests_HTTP_Functions extends WP_UnitTestCase { 42 42 43 43 function test_get_request() { 44 44 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 45 $file = tempnam('/tmp', 'testfile');46 45 47 $headers = wp_get_http($url, $file); 46 $response = wp_remote_get( $url ); 47 $headers = wp_remote_retrieve_headers( $response ); 48 48 49 49 // should return the same headers as a head request 50 50 $this->assertInternalType( 'array', $headers, "Reply wasn't array." ); 51 51 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 52 52 $this->assertEquals( '40148', $headers['content-length'] ); 53 $this->assertEquals( '200', $headers['response'] ); 54 55 // make sure the file is ok 56 $this->assertEquals( 40148, filesize($file) ); 57 $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) ); 53 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 58 54 } 59 55 60 56 function test_get_redirect() { 61 57 // this will redirect to asdftestblog1.files.wordpress.com 62 58 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 63 $file = tempnam('/tmp', 'testfile');64 59 65 $headers = wp_get_http($url, $file); 60 $response = wp_remote_get($url); 61 $headers = wp_remote_retrieve_headers( $response ); 66 62 67 63 // should return the same headers as a head request 68 64 $this->assertInternalType( 'array', $headers, "Reply wasn't array." ); 69 65 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 70 66 $this->assertEquals( '40148', $headers['content-length'] ); 71 $this->assertEquals( '200', $headers['response'] ); 72 73 // make sure the file is ok 74 $this->assertEquals( 40148, filesize($file) ); 75 $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) ); 67 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 76 68 } 77 69 78 70 function test_get_redirect_limit_exceeded() { 79 71 // this will redirect to asdftestblog1.files.wordpress.com 80 72 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 81 $file = tempnam('/tmp', 'testfile'); 73 82 74 // pretend we've already redirected 5 times 83 $ headers = wp_get_http( $url, $file, 6);84 $this->assert False( $headers);75 $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); 76 $this->assertWPError( $response ); 85 77 } 86 78 }