Ticket #33709: 33709.diff
File 33709.diff, 6.3 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..595fd36 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 * 3573 * @param string $url URL to fetch. 3574 * @param string|bool $file_path Optional. File path to write request to. Default false. 3575 * @param int $red Optional. The number of Redirects followed, Upon 5 being hit, 3576 * returns false. Default 1. 3577 * @return bool|string False on failure and string of headers if HEAD request. 3578 */ 3579 function wp_get_http( $url, $file_path = false, $red = 1 ) { 3580 _deprecated_function( __FUNCTION__, '4.4', 'WP_HTTP' ); 3581 3582 @set_time_limit( 60 ); 3583 3584 if ( $red > 5 ) 3585 return false; 3586 3587 $options = array(); 3588 $options['redirection'] = 5; 3589 3590 if ( false == $file_path ) 3591 $options['method'] = 'HEAD'; 3592 else 3593 $options['method'] = 'GET'; 3594 3595 $response = wp_safe_remote_request( $url, $options ); 3596 3597 if ( is_wp_error( $response ) ) 3598 return false; 3599 3600 $headers = wp_remote_retrieve_headers( $response ); 3601 $headers['response'] = wp_remote_retrieve_response_code( $response ); 3602 3603 // WP_HTTP no longer follows redirects for HEAD requests. 3604 if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) { 3605 return wp_get_http( $headers['location'], $file_path, ++$red ); 3606 } 3607 3608 if ( false == $file_path ) 3609 return $headers; 3610 3611 // GET request - write it to the supplied filename 3612 $out_fp = fopen($file_path, 'w'); 3613 if ( !$out_fp ) 3614 return $headers; 3615 3616 fwrite( $out_fp, wp_remote_retrieve_body( $response ) ); 3617 fclose($out_fp); 3618 clearstatcache(); 3619 3620 return $headers; 3621 } -
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..b8df6da 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_safe_remote_get( $url, array( 'redirection' => -1 ) ); 76 $this->assertWPError( $response ); 85 77 } 86 78 }