Make WordPress Core

Changeset 38449


Ignore:
Timestamp:
08/30/2016 04:35:33 PM (10 years ago)
Author:
johnbillion
Message:

HTTP API: Add a $component parameter to wp_parse_url() to give it parity with PHP's parse_url() function.

Fixes #36356
Props jrf

Location:
trunk
Files:
2 edited

Legend:

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

    r37543 r38449  
    634634 *
    635635 * @since 4.4.0
    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 ) {
     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 */
     648function 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 ) {
    644656                // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
    645657                if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
    646658                        // Since we know it's a relative path, prefix with a scheme/host placeholder and try again
    647                         if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
     659                        if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url, $component ) ) {
    648660                                return $parts;
    649661                        }
    … …  
    655667        }
    656668
    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'] );
     669        // < PHP 5.4.7 compat, doesn't detect a schemeless URL's host field
     670        if ( '//' == substr( $url, 0, 2 ) ) {
     671                if ( -1 === $component && ! isset( $parts['host'] ) ) {
     672                        $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 );
     673                        $parts['host'] = $path_parts[0];
     674                        if ( isset( $path_parts[1] ) ) {
     675                                $parts['path'] = '/' . $path_parts[1];
     676                        } else {
     677                                unset( $parts['path'] );
     678                        }
     679                } elseif ( PHP_URL_HOST === $component || PHP_URL_PATH === $component ) {
     680                        $all_parts = @parse_url( $url );
     681                        if ( ! isset( $all_parts['host'] ) ) {
     682                                $path_parts = explode( '/', substr( $all_parts['path'], 2 ), 2 );
     683                                if ( PHP_URL_PATH === $component ) {
     684                                        if ( isset( $path_parts[1] ) ) {
     685                                                $parts = '/' . $path_parts[1];
     686                                        } else {
     687                                                $parts = null;
     688                                        }
     689                                } elseif ( PHP_URL_HOST === $component ) {
     690                                        $parts = $path_parts[0];
     691                                }
     692                        }
    665693                }
    666694        }
  • trunk/tests/phpunit/tests/http/http.php

    r38430 r38449  
    77class Tests_HTTP_HTTP extends WP_UnitTestCase {
    88
     9        protected static $full_test_url = 'http://username:password@host.name:9090/path?arg1=value1&arg2=value2#anchor';
     10
    911        /**
    1012         * @dataProvider make_absolute_url_testcases
    … …  
    1719
    1820                $actual = WP_Http::make_absolute_url( $relative_url, $absolute_url );
    19                 $this->assertEquals( $expected, $actual );
     21                $this->assertSame( $expected, $actual );
    2022        }
    2123
    … …  
    7375        function test_wp_parse_url( $url, $expected ) {
    7476                $actual = wp_parse_url( $url );
    75                 $this->assertEquals( $expected, $actual );
     77                $this->assertSame( $expected, $actual );
    7678        }
    7779
    … …  
    7981                // 0: The URL, 1: The expected resulting structure
    8082                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                        ) ),
    8193                        array( 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),
    8294
    … …  
    8698                        array( 'http://example.com//path/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '//path/' ) ),
    8799
    88                         // < PHP 5.4.7: Scheme seperator in the URL
     100                        // < PHP 5.4.7: Scheme separator in the URL
    89101                        array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ),
    90102                        array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ),
    … …  
    102114                  - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
    103115                */
     116        }
     117
     118        /**
     119         * @ticket 36356
     120         *
     121         * @dataProvider parse_url_component_testcases
     122         */
     123        function test_wp_parse_url_with_component( $url, $component, $expected ) {
     124                $actual = wp_parse_url( $url, $component );
     125                $this->assertSame( $expected, $actual );
     126        }
     127
     128        function parse_url_component_testcases() {
     129                // 0: The URL, 1: The requested component, 2: The expected resulting component.
     130                return array(
     131                        array( self::$full_test_url, -1, array(
     132                                'scheme'   => 'http',
     133                                'host'     => 'host.name',
     134                                'port'     => 9090,
     135                                'user'     => 'username',
     136                                'pass'     => 'password',
     137                                'path'     => '/path',
     138                                'query'    => 'arg1=value1&arg2=value2',
     139                                'fragment' => 'anchor',
     140                        ) ),
     141                        array( self::$full_test_url, PHP_URL_SCHEME, 'http' ),
     142                        array( self::$full_test_url, PHP_URL_USER, 'username' ),
     143                        array( self::$full_test_url, PHP_URL_PASS, 'password' ),
     144                        array( self::$full_test_url, PHP_URL_HOST, 'host.name' ),
     145                        array( self::$full_test_url, PHP_URL_PORT, 9090 ),
     146                        array( self::$full_test_url, PHP_URL_PATH, '/path' ),
     147                        array( self::$full_test_url, PHP_URL_QUERY, 'arg1=value1&arg2=value2' ),
     148                        array( self::$full_test_url, PHP_URL_FRAGMENT, 'anchor' ),
     149
     150                        // < PHP 5.4.7: Schemeless URL
     151                        array( '//example.com/path/', PHP_URL_HOST, 'example.com' ),
     152                        array( '//example.com/path/', PHP_URL_PATH, '/path/' ),
     153                        array( '//example.com/', PHP_URL_HOST, 'example.com' ),
     154                        array( '//example.com/', PHP_URL_PATH, '/' ),
     155                        array( 'http://example.com//path/', PHP_URL_HOST, 'example.com' ),
     156                        array( 'http://example.com//path/', PHP_URL_PATH, '//path/' ),
     157
     158                        // < PHP 5.4.7: Scheme separator in the URL
     159                        array( 'http://example.com/http://example.net/', PHP_URL_HOST, 'example.com' ),
     160                        array( 'http://example.com/http://example.net/', PHP_URL_PATH, '/http://example.net/' ),
     161                        array( '/path/http://example.net/', PHP_URL_HOST, null ),
     162                        array( '/path/http://example.net/', PHP_URL_PATH, '/path/http://example.net/' ),
     163
     164                        // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly.
     165                        array( '//[::FFFF::127.0.0.1]/', PHP_URL_HOST, '[::FFFF::127.0.0.1]' ),
     166                        array( '//[::FFFF::127.0.0.1]/', PHP_URL_PATH, '/' ),
     167
     168                        // PHP's parse_url() calls this an invalid URL, we handle it as a path
     169                        array( '/://example.com/', PHP_URL_PATH, '/://example.com/' ),
     170
     171                );
    104172        }
    105173
Note: See TracChangeset for help on using the changeset viewer.