From 0102df84893738d1013a278ed0339856c3ab8d02 Mon Sep 17 00:00:00 2001 From: Marooned Date: Thu, 18 Aug 2011 10:33:39 +0000 Subject: [PATCH] Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them. This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com --- includes/HttpFunctions.php | 39 ++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index de484d2c11..638788b33c 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -569,13 +569,44 @@ class MWHttpRequest { /** * Returns the final URL after all redirections. * - * @return String + * Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them. + * This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com + * + * @returns string */ public function getFinalUrl() { - $location = $this->getResponseHeader( "Location" ); + $headers = $this->getResponseHeaders(); + + //return full url (fix for incorrect but handled relative location) + if ( isset( $headers[ 'location' ] ) ) { + $locations = $headers[ 'location' ]; + $domain = ''; + $foundRelativeURI = false; + $countLocations = count($locations); - if ( $location ) { - return $location; + for ( $i = $countLocations - 1; $i >= 0; $i-- ) { + $url = parse_url( $locations[ $i ] ); + + if ( isset($url[ 'host' ]) ) { + $domain = $url[ 'scheme' ] . '://' . $url[ 'host' ]; + break; //found correct URI (with host) + } else { + $foundRelativeURI = true; + } + } + + if ( $foundRelativeURI ) { + if ( $domain ) { + return $domain . $locations[ $countLocations - 1 ]; + } else { + $url = parse_url( $this->url ); + if ( isset($url[ 'host' ]) ) { + return $url[ 'scheme' ] . '://' . $url[ 'host' ] . $locations[ $countLocations - 1 ]; + } + } + } else { + return $locations[ $countLocations - 1 ]; + } } return $this->url; -- 2.20.1