WordPress.org

Make WordPress Core

Changeset 20767


Ignore:
Timestamp:
05/10/2012 09:32:02 PM (8 years ago)
Author:
ryan
Message:

Handle relative urls when processing redirects. Introduce WP_Http::make_absolute_url(). Props dd32. fixes #20434

File:
1 edited

Legend:

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

    r20399 r20767  
    536536
    537537    }
     538
     539    static function make_absolute_url( $maybe_relative_path, $url ) {
     540        if ( empty( $url ) )
     541            return $maybe_relative_path;
     542
     543        // Check for a scheme
     544        if ( false !== strpos( $maybe_relative_path, '://' ) )
     545            return $maybe_relative_path;
     546
     547        if ( ! $url_parts = @parse_url( $url ) )
     548            return $maybe_relative_path;
     549
     550        if ( ! $relative_url_parts = @parse_url( $maybe_relative_path ) )
     551            return $maybe_relative_path;
     552
     553        $absolute_path = $url_parts['scheme'] . '://' . $url_parts['host'];
     554        if ( isset( $url_parts['port'] ) )
     555            $absolute_path .= ':' . $url_parts['port'];
     556
     557        // Start off with the Absolute URL path
     558        $path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/';
     559
     560        // If the it's a root-relative path, then great
     561        if ( ! empty( $relative_url_parts['path'] ) && '/' == $relative_url_parts['path'][0] ) {
     562            $path = $relative_url_parts['path'];
     563
     564        // Else it's a relative path
     565        } elseif ( ! empty( $relative_url_parts['path'] ) ) {
     566            // Strip off any file components from the absolute path
     567            $path = substr( $path, 0, strrpos( $path, '/' ) + 1 );
     568
     569            // Build the new path
     570            $path .= $relative_url_parts['path'];
     571
     572            // Strip all /path/../ out of the path
     573            while ( strpos( $path, '../' ) > 1 ) {
     574                $path = preg_replace( '![^/]+/\.\./!', '', $path );
     575            }
     576
     577            // Strip any final leading ../ from the path
     578            $path = preg_replace( '!^/(\.\./)+!', '', $path );
     579        }
     580
     581        // Add the Query string
     582        if ( ! empty( $relative_url_parts['query'] ) )
     583            $path .= '?' . $relative_url_parts['query'];
     584
     585        return $absolute_path . '/' . ltrim( $path, '/' );
     586    }
    538587}
    539588
     
    731780        if ( isset($arrHeaders['headers']['location']) && 0 !== $r['_redirection'] ) {
    732781            if ( $r['redirection']-- > 0 ) {
    733                 return $this->request($arrHeaders['headers']['location'], $r);
     782                return $this->request( WP_HTTP::make_absolute_url( $arrHeaders['headers']['location'], $url ), $r);
    734783            } else {
    735784                return new WP_Error('http_request_failed', __('Too many redirects.'));
     
    11321181        if ( ! empty( $theHeaders['headers']['location'] ) && 0 !== $r['_redirection'] ) { // _redirection: The requested number of redirections
    11331182            if ( $r['redirection']-- > 0 ) {
    1134                 return $this->request( $theHeaders['headers']['location'], $r );
     1183                return $this->request( WP_HTTP::make_absolute_url( $theHeaders['headers']['location'], $url ), $r );
    11351184            } else {
    11361185                return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
Note: See TracChangeset for help on using the changeset viewer.