Make WordPress Core

Changeset 35369


Ignore:
Timestamp:
10/23/2015 05:53:05 AM (9 years ago)
Author:
dd32
Message:

WP_HTTP: Promote the WP_HTTP::parse_url() method to a more generic wp_parse_url() function.

Fixes #34408

Location:
trunk
Files:
3 edited

Legend:

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

    r34585 r35369  
    689689     * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
    690690     *
    691      * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
    692      * schemeless and relative url's with :// in the path, this works around those
    693      * limitations providing a standard output on PHP 5.2~5.4+.
    694      *
    695      * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
    696      * when URL parsing failed.
    697      *
    698      * @since 4.1.0
    699      *
    700      * @static
    701691     * @access protected
     692     * @deprecated 4.4.0 See wp_parse_url()
    702693     *
    703694     * @param string $url The URL to parse.
     
    706697     */
    707698    protected static function parse_url( $url ) {
    708         $parts = @parse_url( $url );
    709         if ( ! $parts ) {
    710             // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
    711             if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
    712                 // Since we know it's a relative path, prefix with a scheme/host placeholder and try again
    713                 if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
    714                     return $parts;
    715                 }
    716                 // Remove the placeholder values
    717                 unset( $parts['scheme'], $parts['host'] );
    718             } else {
    719                 return $parts;
    720             }
    721         }
    722 
    723         // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
    724         if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
    725             list( $parts['host'], $slashless_path ) = explode( '/', substr( $parts['path'], 2 ), 2 );
    726             $parts['path'] = "/{$slashless_path}";
    727         }
    728 
    729         return $parts;
     699        _deprecated_function( __METHOD__, '4.4.0', 'wp_parse_url()' );
     700        return wp_parse_url( $url );
    730701    }
    731702
     
    748719            return $maybe_relative_path;
    749720
    750         if ( ! $url_parts = WP_Http::parse_url( $url ) ) {
     721        if ( ! $url_parts = wp_parse_url( $url ) ) {
    751722            return $maybe_relative_path;
    752723        }
    753724
    754         if ( ! $relative_url_parts = WP_Http::parse_url( $maybe_relative_path ) ) {
     725        if ( ! $relative_url_parts = wp_parse_url( $maybe_relative_path ) ) {
    755726            return $maybe_relative_path;
    756727        }
  • trunk/src/wp-includes/http-functions.php

    r35170 r35369  
    613613    return $queried[ $host ];
    614614}
     615
     616/**
     617 * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
     618 *
     619 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
     620 * schemeless and relative url's with :// in the path, this works around those
     621 * limitations providing a standard output on PHP 5.2~5.4+.
     622 *
     623 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
     624 * when URL parsing failed.
     625 *
     626 * @since 4.4.0
     627 *
     628 * @param string $url The URL to parse.
     629 * @return bool|array False on failure; Array of URL components on success;
     630 *                    See parse_url()'s return values.
     631 */
     632function wp_parse_url( $url ) {
     633    $parts = @parse_url( $url );
     634    if ( ! $parts ) {
     635        // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
     636        if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
     637            // Since we know it's a relative path, prefix with a scheme/host placeholder and try again
     638            if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
     639                return $parts;
     640            }
     641            // Remove the placeholder values
     642            unset( $parts['scheme'], $parts['host'] );
     643        } else {
     644            return $parts;
     645        }
     646    }
     647
     648    // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
     649    if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
     650        $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 );
     651        $parts['host'] = $path_parts[0];
     652        if ( isset( $path_parts[1] ) ) {
     653            $parts['path'] = '/' . $path_parts[1];
     654        } else {
     655            unset( $parts['path'] );
     656        }
     657    }
     658
     659    return $parts;
     660}
  • trunk/tests/phpunit/tests/http/http.php

    r34123 r35369  
    7171     * @dataProvider parse_url_testcases
    7272     */
    73     function test_parse_url( $url, $expected ) {
    74         if ( ! is_callable( array( 'WP_HTTP_Testable', 'parse_url' ) ) ) {
    75             $this->markTestSkipped( "This version of WP_HTTP doesn't support WP_HTTP::parse_url()" );
    76             return;
    77         }
    78         $actual = WP_HTTP_Testable::parse_url( $url );
     73    function test_wp_parse_url( $url, $expected ) {
     74        $actual = wp_parse_url( $url );
    7975        $this->assertEquals( $expected, $actual );
    8076    }
     
    9389            array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ),
    9490            array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ),
     91
     92            // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly.
     93            array( '//[::FFFF::127.0.0.1]/', array( 'host' => '[::FFFF::127.0.0.1]', 'path' => '/' ) ),
     94
    9595            // PHP's parse_url() calls this an invalid url, we handle it as a path
    9696            array( '/://example.com/', array( 'path' => '/://example.com/' ) ),
     
    104104    }
    105105}
    106 
    107 /**
    108  * A Wrapper of WP_Http to make parse_url() publicaly accessible for testing purposes.
    109  */
    110 class WP_HTTP_Testable extends WP_Http {
    111     public static function parse_url( $url ) {
    112         return parent::parse_url( $url );
    113     }
    114 }
Note: See TracChangeset for help on using the changeset viewer.