MultiHttpClient: Also fallover to non-curl if curl_multi* is blocked
[lhc/web/wiklou.git] / includes / libs / http / MultiHttpClient.php
index e099fdb..85959d6 100644 (file)
@@ -176,7 +176,9 @@ class MultiHttpClient implements LoggerAwareInterface {
         * @return bool true if curl is available, false otherwise.
         */
        protected function isCurlEnabled() {
-               return extension_loaded( 'curl' );
+               // Explicitly test if curl_multi* is blocked, as some users' hosts provide
+               // them with a modified curl with the multi-threaded parts removed(!)
+               return extension_loaded( 'curl' ) && function_exists( 'curl_multi_init' );
        }
 
        /**
@@ -294,12 +296,14 @@ class MultiHttpClient implements LoggerAwareInterface {
 
        /**
         * @param array &$req HTTP request map
+        * @codingStandardsIgnoreStart
+        * @phan-param array{url:string,proxy?:?string,query:mixed,method:string,body:string|resource,headers:string[],stream?:resource,flags:array} $req
+        * @codingStandardsIgnoreEnd
         * @param array $opts
         *   - connTimeout : default connection timeout
         *   - reqTimeout : default request timeout
         * @return resource
         * @throws Exception
-        * @suppress PhanTypeMismatchArgumentInternal
         */
        protected function getCurlHandle( array &$req, array $opts ) {
                $ch = curl_init();
@@ -383,7 +387,7 @@ class MultiHttpClient implements LoggerAwareInterface {
                                }
                                $length = strlen( $header );
                                $matches = [];
-                               if ( preg_match( "/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header, $matches ) ) {
+                               if ( preg_match( "/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/", $header, $matches ) ) {
                                        $req['response']['code'] = (int)$matches[2];
                                        $req['response']['reason'] = trim( $matches[3] );
                                        return $length;
@@ -410,6 +414,7 @@ class MultiHttpClient implements LoggerAwareInterface {
                                if ( $hasOutputStream ) {
                                        return fwrite( $req['stream'], $data );
                                } else {
+                                       // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
                                        $req['response']['body'] .= $data;
 
                                        return strlen( $data );