X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=includes%2FHttpFunctions.php;h=638788b33c157a9ef92eb90da2e6c10d0012fab6;hb=04e002d5823dbb63cdd52bb235846fd08d433585;hp=56f864c3a7d225ea3e859a766a913a183e56f417;hpb=4065e65d03cb7f0eb0a70a04ed5a4ec345faf6af;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index 56f864c3a7..638788b33c 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -14,7 +14,7 @@ class Http { * Perform an HTTP request * * @param $method String: HTTP method. Usually GET/POST - * @param $url String: full URL to act on + * @param $url String: full URL to act on. If protocol-relative, will be expanded to an http:// URL * @param $options Array: options to pass to MWHttpRequest object. * Possible keys for the array: * - timeout Timeout length in seconds @@ -32,7 +32,7 @@ class Http { * @return Mixed: (bool)false on failure or a string on success */ public static function request( $method, $url, $options = array() ) { - $url = wfExpandUrl( $url ); + $url = wfExpandUrl( $url, PROTO_HTTP ); wfDebug( "HTTP: $method: $url\n" ); $options['method'] = strtoupper( $method ); @@ -125,12 +125,14 @@ class Http { * protocols, because we only want protocols that both cURL * and php support. * + * @fixme this is wildly inaccurate and fails to actually check most stuff + * * @param $uri Mixed: URI to check for validity * @returns Boolean */ public static function isValidURI( $uri ) { return preg_match( - '/^(f|ht)tps?:\/\/[^\/\s]\S*$/D', + '/^https?:\/\/[^\/\s]\S*$/D', $uri ); } @@ -207,6 +209,15 @@ class MWHttpRequest { } } + /** + * Simple function to test if we can make any sort of requests at all, using + * cURL or fopen() + * @return bool + */ + public static function canMakeRequests() { + return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' ); + } + /** * Generate a new request object * @param $url String: url to use @@ -558,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; @@ -713,7 +755,8 @@ class PhpHttpRequest extends MWHttpRequest { $this->postData = wfArrayToCGI( $this->postData ); } - if ( $this->parsedUrl['scheme'] != 'http' ) { + if ( $this->parsedUrl['scheme'] != 'http' && + $this->parsedUrl['scheme'] != 'https' ) { $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] ); }