Log useful error messages on SSL errors in PhpHttpRequest
authorGergő Tisza <tgr.huwiki@gmail.com>
Fri, 4 Sep 2015 23:15:03 +0000 (23:15 +0000)
committerBryanDavis <bdavis@wikimedia.org>
Thu, 10 Sep 2015 01:08:22 +0000 (01:08 +0000)
Work around PHP's crappy stream error handling to extract the
OpenSSL message that gives actually useful information.

Change-Id: I9eba0f90935e25e2d07995f6181fa90b7d52ea55

includes/HttpFunctions.php

index 24c0dfc..bc5a957 100644 (file)
@@ -855,6 +855,8 @@ class CurlHttpRequest extends MWHttpRequest {
 
 class PhpHttpRequest extends MWHttpRequest {
 
+       private $fopenErrors = array();
+
        /**
         * @param string $url
         * @return string
@@ -909,6 +911,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 +999,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 +1048,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;
                }