Index: trunk/phase3/includes/HttpFunctions.php |
— | — | @@ -569,13 +569,44 @@ |
570 | 570 | /** |
571 | 571 | * Returns the final URL after all redirections. |
572 | 572 | * |
573 | | - * @return String |
| 573 | + * Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them. |
| 574 | + * This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com |
| 575 | + * |
| 576 | + * @returns string |
574 | 577 | */ |
575 | 578 | public function getFinalUrl() { |
576 | | - $location = $this->getResponseHeader( "Location" ); |
| 579 | + $headers = $this->getResponseHeaders(); |
577 | 580 | |
578 | | - if ( $location ) { |
579 | | - return $location; |
| 581 | + //return full url (fix for incorrect but handled relative location) |
| 582 | + if ( isset( $headers[ 'location' ] ) ) { |
| 583 | + $locations = $headers[ 'location' ]; |
| 584 | + $domain = ''; |
| 585 | + $foundRelativeURI = false; |
| 586 | + $countLocations = count($locations); |
| 587 | + |
| 588 | + for ( $i = $countLocations - 1; $i >= 0; $i-- ) { |
| 589 | + $url = parse_url( $locations[ $i ] ); |
| 590 | + |
| 591 | + if ( isset($url[ 'host' ]) ) { |
| 592 | + $domain = $url[ 'scheme' ] . '://' . $url[ 'host' ]; |
| 593 | + break; //found correct URI (with host) |
| 594 | + } else { |
| 595 | + $foundRelativeURI = true; |
| 596 | + } |
| 597 | + } |
| 598 | + |
| 599 | + if ( $foundRelativeURI ) { |
| 600 | + if ( $domain ) { |
| 601 | + return $domain . $locations[ $countLocations - 1 ]; |
| 602 | + } else { |
| 603 | + $url = parse_url( $this->url ); |
| 604 | + if ( isset($url[ 'host' ]) ) { |
| 605 | + return $url[ 'scheme' ] . '://' . $url[ 'host' ] . $locations[ $countLocations - 1 ]; |
| 606 | + } |
| 607 | + } |
| 608 | + } else { |
| 609 | + return $locations[ $countLocations - 1 ]; |
| 610 | + } |
580 | 611 | } |
581 | 612 | |
582 | 613 | return $this->url; |