Merge "Made convertNamespace() use APC"
[lhc/web/wiklou.git] / includes / HttpFunctions.php
index 24c0dfc..578b535 100644 (file)
@@ -678,7 +678,7 @@ class MWHttpRequest {
        public function getFinalUrl() {
                $headers = $this->getResponseHeaders();
 
-               //return full url (fix for incorrect but handled relative location)
+               // return full url (fix for incorrect but handled relative location)
                if ( isset( $headers['location'] ) ) {
                        $locations = $headers['location'];
                        $domain = '';
@@ -690,7 +690,7 @@ class MWHttpRequest {
 
                                if ( isset( $url['host'] ) ) {
                                        $domain = $url['scheme'] . '://' . $url['host'];
-                                       break; //found correct URI (with host)
+                                       break; // found correct URI (with host)
                                } else {
                                        $foundRelativeURI = true;
                                }
@@ -838,23 +838,27 @@ class CurlHttpRequest extends MWHttpRequest {
         * @return bool
         */
        public function canFollowRedirects() {
-               if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
-                       wfDebug( "Cannot follow redirects in safe mode\n" );
-                       return false;
-               }
-
                $curlVersionInfo = curl_version();
                if ( $curlVersionInfo['version_number'] < 0x071304 ) {
                        wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
                        return false;
                }
 
+               if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
+                       if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
+                               wfDebug( "Cannot follow redirects in safe mode\n" );
+                               return false;
+                       }
+               }
+
                return true;
        }
 }
 
 class PhpHttpRequest extends MWHttpRequest {
 
+       private $fopenErrors = array();
+
        /**
         * @param string $url
         * @return string
@@ -893,7 +897,7 @@ class PhpHttpRequest extends MWHttpRequest {
                        ) );
                }
 
-               foreach( $certLocations as $key => $cert ) {
+               foreach ( $certLocations as $key => $cert ) {
                        if ( is_dir( $cert ) ) {
                                $certOptions['capath'] = $cert;
                                break;
@@ -909,6 +913,16 @@ class PhpHttpRequest extends MWHttpRequest {
                return $certOptions;
        }
 
+       /**
+        * Custom error handler for dealing with fopen() errors. fopen() tends to fire multiple errors in succession, and the last one
+        * is completely useless (something like "fopen: failed to open stream") so normal methods of handling errors programmatically
+        * like get_last_error() don't work.
+        */
+       public function errorHandler( $errno, $errstr ) {
+               $n = count( $this->fopenErrors ) + 1;
+               $this->fopenErrors += array( "errno$n" => $errno, "errstr$n" => $errstr );
+       }
+
        public function execute() {
 
                parent::execute();
@@ -987,9 +1001,10 @@ class PhpHttpRequest extends MWHttpRequest {
                }
                do {
                        $reqCount++;
-                       MediaWiki\suppressWarnings();
+                       $this->fopenErrors = array();
+                       set_error_handler( array( $this, 'errorHandler' ) );
                        $fh = fopen( $url, "r", false, $context );
-                       MediaWiki\restoreWarnings();
+                       restore_error_handler();
 
                        if ( !$fh ) {
                                // HACK for instant commons.
@@ -1035,6 +1050,10 @@ class PhpHttpRequest extends MWHttpRequest {
                $this->setStatus();
 
                if ( $fh === false ) {
+                       if ( $this->fopenErrors ) {
+                               LoggerFactory::getInstance( 'http' )->warning( __CLASS__
+                                       . ': error opening connection: {errstr1}', $this->fopenErrors );
+                       }
                        $this->status->fatal( 'http-request-error' );
                        return $this->status;
                }