Changeset 38456
- Timestamp:
- 08/31/2016 12:23:26 AM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/http.php
r38450 r38456 634 634 * 635 635 * @since 4.4.0 636 * @since 4.7.0 The $component parameter was added for parity with PHP's parse_url(). 637 * 638 * @param string $url The URL to parse. 639 * @param int $component The specific component to retrieve. Use one of the PHP 640 * predefined constants to specify which one. 641 * Defaults to -1 (= return all parts as an array). 642 * @see http://php.net/manual/en/function.parse-url.php 643 * @return mixed False on failure; Array of URL components on success; 644 * When a specific component has been requested: null if the component doesn't 645 * exist in the given URL; a sting or - in the case of PHP_URL_PORT - integer 646 * when it does; See parse_url()'s return values. 647 */ 648 function wp_parse_url( $url, $component = -1 ) { 649 $parts = @parse_url( $url, $component ); 650 651 if ( version_compare( PHP_VERSION, '5.4.7', '>=' ) ) { 652 return $parts; 653 } 654 655 if ( false === $parts ) { 636 * 637 * @param string $url The URL to parse. 638 * @return bool|array False on failure; Array of URL components on success; 639 * See parse_url()'s return values. 640 */ 641 function wp_parse_url( $url ) { 642 $parts = @parse_url( $url ); 643 if ( ! $parts ) { 656 644 // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path 657 645 if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) { 658 646 // Since we know it's a relative path, prefix with a scheme/host placeholder and try again 659 if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url , $component) ) {647 if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) { 660 648 return $parts; 661 649 } 662 if ( is_array( $parts ) ) { 663 // Remove the placeholder values 664 unset( $parts['scheme'], $parts['host'] ); 665 } 650 // Remove the placeholder values 651 unset( $parts['scheme'], $parts['host'] ); 666 652 } else { 667 653 return $parts; … … 669 655 } 670 656 671 // < PHP 5.4.7 compat, doesn't detect a schemeless URL's host field 672 if ( '//' == substr( $url, 0, 2 ) ) { 673 if ( -1 === $component && ! isset( $parts['host'] ) ) { 674 $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 ); 675 $parts['host'] = $path_parts[0]; 676 if ( isset( $path_parts[1] ) ) { 677 $parts['path'] = '/' . $path_parts[1]; 678 } else { 679 unset( $parts['path'] ); 680 } 681 } elseif ( PHP_URL_HOST === $component || PHP_URL_PATH === $component ) { 682 $all_parts = @parse_url( $url ); 683 if ( ! isset( $all_parts['host'] ) ) { 684 $path_parts = explode( '/', substr( $all_parts['path'], 2 ), 2 ); 685 if ( PHP_URL_PATH === $component ) { 686 if ( isset( $path_parts[1] ) ) { 687 $parts = '/' . $path_parts[1]; 688 } else { 689 $parts = null; 690 } 691 } elseif ( PHP_URL_HOST === $component ) { 692 $parts = $path_parts[0]; 693 } 694 } 657 // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field 658 if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) { 659 $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 ); 660 $parts['host'] = $path_parts[0]; 661 if ( isset( $path_parts[1] ) ) { 662 $parts['path'] = '/' . $path_parts[1]; 663 } else { 664 unset( $parts['path'] ); 695 665 } 696 666 } -
trunk/tests/phpunit/tests/http/http.php
r38453 r38456 6 6 */ 7 7 class Tests_HTTP_HTTP extends WP_UnitTestCase { 8 9 protected static $full_test_url = 'http://username:password@host.name:9090/path?arg1=value1&arg2=value2#anchor';10 8 11 9 /** … … 81 79 // 0: The URL, 1: The expected resulting structure 82 80 return array( 83 array( self::$full_test_url, array(84 'scheme' => 'http',85 'host' => 'host.name',86 'port' => 9090,87 'user' => 'username',88 'pass' => 'password',89 'path' => '/path',90 'query' => 'arg1=value1&arg2=value2',91 'fragment' => 'anchor',92 ) ),93 81 array( 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ), 94 82 … … 98 86 array( 'http://example.com//path/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '//path/' ) ), 99 87 100 // < PHP 5.4.7: Scheme sep arator in the URL88 // < PHP 5.4.7: Scheme seperator in the URL 101 89 array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ), 102 90 array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ), … … 114 102 - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7 115 103 */ 116 }117 118 /**119 * @ticket 36356120 */121 function test_wp_parse_url_with_default_component() {122 $actual = wp_parse_url( self::$full_test_url, -1 );123 $this->assertEquals( array(124 'scheme' => 'http',125 'host' => 'host.name',126 'port' => 9090,127 'user' => 'username',128 'pass' => 'password',129 'path' => '/path',130 'query' => 'arg1=value1&arg2=value2',131 'fragment' => 'anchor',132 ), $actual );133 }134 135 /**136 * @ticket 36356137 *138 * @dataProvider parse_url_component_testcases139 */140 function test_wp_parse_url_with_component( $url, $component, $expected ) {141 $actual = wp_parse_url( $url, $component );142 $this->assertSame( $expected, $actual );143 }144 145 function parse_url_component_testcases() {146 // 0: The URL, 1: The requested component, 2: The expected resulting component.147 return array(148 array( self::$full_test_url, PHP_URL_SCHEME, 'http' ),149 array( self::$full_test_url, PHP_URL_USER, 'username' ),150 array( self::$full_test_url, PHP_URL_PASS, 'password' ),151 array( self::$full_test_url, PHP_URL_HOST, 'host.name' ),152 array( self::$full_test_url, PHP_URL_PORT, 9090 ),153 array( self::$full_test_url, PHP_URL_PATH, '/path' ),154 array( self::$full_test_url, PHP_URL_QUERY, 'arg1=value1&arg2=value2' ),155 array( self::$full_test_url, PHP_URL_FRAGMENT, 'anchor' ),156 157 // < PHP 5.4.7: Schemeless URL158 array( '//example.com/path/', PHP_URL_HOST, 'example.com' ),159 array( '//example.com/path/', PHP_URL_PATH, '/path/' ),160 array( '//example.com/', PHP_URL_HOST, 'example.com' ),161 array( '//example.com/', PHP_URL_PATH, '/' ),162 array( 'http://example.com//path/', PHP_URL_HOST, 'example.com' ),163 array( 'http://example.com//path/', PHP_URL_PATH, '//path/' ),164 165 // < PHP 5.4.7: Scheme separator in the URL166 array( 'http://example.com/http://example.net/', PHP_URL_HOST, 'example.com' ),167 array( 'http://example.com/http://example.net/', PHP_URL_PATH, '/http://example.net/' ),168 array( '/path/http://example.net/', PHP_URL_HOST, null ),169 array( '/path/http://example.net/', PHP_URL_PATH, '/path/http://example.net/' ),170 171 // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly.172 array( '//[::FFFF::127.0.0.1]/', PHP_URL_HOST, '[::FFFF::127.0.0.1]' ),173 array( '//[::FFFF::127.0.0.1]/', PHP_URL_PATH, '/' ),174 175 // PHP's parse_url() calls this an invalid URL, we handle it as a path176 array( '/://example.com/', PHP_URL_PATH, '/://example.com/' ),177 178 );179 104 } 180 105
Note: See TracChangeset
for help on using the changeset viewer.