One more fix for issue #55
[plcapi.git] / src / Client.php
index af17679..074c196 100644 (file)
@@ -9,6 +9,10 @@ use PhpXmlRpc\Helper\Logger;
  */
 class Client
 {
+    const USE_CURL_NEVER = 0;
+    const USE_CURL_ALWAYS = 1;
+    const USE_CURL_AUTO = 2;
+
     /// @todo: do these need to be public?
     public $method = 'http';
     public $server;
@@ -41,6 +45,7 @@ class Client
 
     public $cookies = array();
     public $extracurlopts = array();
+    public $use_curl = self::USE_CURL_AUTO;
 
     /**
      * @var bool
@@ -59,7 +64,7 @@ class Client
      * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since
      * in those cases it will be up to CURL to decide the compression methods
      * it supports. You might check for the presence of 'zlib' in the output of
-     * curl_version() to determine wheter compression is supported or not
+     * curl_version() to determine whether compression is supported or not
      */
     public $accepted_compression = array();
 
@@ -69,6 +74,7 @@ class Client
      */
 
     public $request_compression = '';
+
     /**
      * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see:
      * http://curl.haxx.se/docs/faq.html#7.3).
@@ -83,7 +89,7 @@ class Client
 
     /**
      * The charset encoding that will be used for serializing request sent by the client.
-     *  If defaults to NULL, which means using US-ASCII and encoding all characters outside of the ASCII range using
+     * It defaults to NULL, which means using US-ASCII and encoding all characters outside of the ASCII range using
      * their xml character entity representation (this has the benefit that line end characters will not be mangled in
      * the transfer, a CR-LF will be preserved as well as a singe LF).
      *  Valid values are 'US-ASCII', 'UTF-8' and 'ISO-8859-1'
@@ -125,7 +131,7 @@ class Client
     public function __construct($path, $server = '', $port = '', $method = '')
     {
         // allow user to specify all params in $path
-        if ($server == '' and $port == '' and $method == '') {
+        if ($server == '' && $port == '' && $method == '') {
             $parts = parse_url($path);
             $server = $parts['host'];
             $path = isset($parts['path']) ? $parts['path'] : '';
@@ -177,7 +183,7 @@ class Client
         $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');
 
         // Add all charsets which mbstring can handle, but remove junk not found in IANA registry at
-        // in http://www.iana.org/assignments/character-sets/character-sets.xhtml
+        // http://www.iana.org/assignments/character-sets/character-sets.xhtml
         // NB: this is disabled to avoid making all the requests sent huge... mbstring supports more than 80 charsets!
         /*if (function_exists('mb_list_encodings')) {
 
@@ -201,7 +207,7 @@ class Client
      * This option can be very useful when debugging servers as it allows you to see exactly what the client sends and
      * the server returns.
      *
-     * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
+     * @param integer $level values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
      */
     public function setDebug($level)
     {
@@ -413,6 +419,15 @@ class Client
         $this->extracurlopts = $options;
     }
 
+    /**
+     * @param int $useCurlMode self::USE_CURL_ALWAYS, self::USE_CURL_AUTO or self::USE_CURL_NEVER
+     */
+    public function setUseCurl($useCurlMode)
+    {
+        $this->use_curl = $useCurlMode;
+    }
+
+
     /**
      * Set user-agent string that will be used by this client instance in http headers sent to the server.
      *
@@ -448,7 +463,6 @@ class Client
      * @param string $method valid values are 'http', 'http11' and 'https'. If left unspecified, the http protocol
      *                       chosen during creation of the object will be used.
      *
-     *
      * @return Response|Response[] Note that the client will always return a Response object, even if the call fails
      */
     public function send($req, $timeout = 0, $method = '')
@@ -473,8 +487,13 @@ class Client
         // where req is a Request
         $req->setDebug($this->debug);
 
-        if ($method == 'https') {
-            $r = $this->sendPayloadHTTPS(
+        /// @todo we could be smarter about this and force usage of curl in scenarios where it is both available and
+        ///       needed, such as digest or ntlm auth. Do not attempt to use it for https if not present
+        $useCurl = ($this->use_curl == self::USE_CURL_ALWAYS) || ($this->use_curl == self::USE_CURL_AUTO &&
+            ($method == 'https' || $method == 'http11'));
+
+        if ($useCurl) {
+            $r = $this->sendPayloadCURL(
                 $req,
                 $this->server,
                 $this->port,
@@ -491,34 +510,16 @@ class Client
                 $this->proxy_user,
                 $this->proxy_pass,
                 $this->proxy_authtype,
+                // bc
+                $method == 'http11' ? 'http' : $method,
                 $this->keepalive,
                 $this->key,
                 $this->keypass,
                 $this->sslversion
             );
-        } elseif ($method == 'http11') {
-            $r = $this->sendPayloadCURL(
-                $req,
-                $this->server,
-                $this->port,
-                $timeout,
-                $this->username,
-                $this->password,
-                $this->authtype,
-                null,
-                null,
-                null,
-                null,
-                $this->proxy,
-                $this->proxyport,
-                $this->proxy_user,
-                $this->proxy_pass,
-                $this->proxy_authtype,
-                'http',
-                $this->keepalive
-            );
         } else {
-            $r = $this->sendPayloadHTTP10(
+            // plain 'http 1.0': default to using socket
+            $r = $this->sendPayloadSocket(
                 $req,
                 $this->server,
                 $this->port,
@@ -526,12 +527,19 @@ class Client
                 $this->username,
                 $this->password,
                 $this->authtype,
+                $this->cert,
+                $this->certpass,
+                $this->cacert,
+                $this->cacertdir,
                 $this->proxy,
                 $this->proxyport,
                 $this->proxy_user,
                 $this->proxy_pass,
                 $this->proxy_authtype,
-                $method
+                $method,
+                $this->key,
+                $this->keypass,
+                $this->sslversion
             );
         }
 
@@ -539,6 +547,7 @@ class Client
     }
 
     /**
+     * @deprecated
      * @param Request $req
      * @param string $server
      * @param int $port
@@ -557,9 +566,79 @@ class Client
     protected function sendPayloadHTTP10($req, $server, $port, $timeout = 0, $username = '', $password = '',
         $authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1,
         $method='http')
+    {
+        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+
+        return $this->sendPayloadSocket($req, $server, $port, $timeout, $username, $password, $authType, null, null,
+            null, null, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType, $method);
+    }
+
+    /**
+     * @deprecated
+     * @param Request $req
+     * @param string $server
+     * @param int $port
+     * @param int $timeout
+     * @param string $username
+     * @param string $password
+     * @param int $authType
+     * @param string $cert
+     * @param string $certPass
+     * @param string $caCert
+     * @param string $caCertDir
+     * @param string $proxyHost
+     * @param int $proxyPort
+     * @param string $proxyUsername
+     * @param string $proxyPassword
+     * @param int $proxyAuthType
+     * @param bool $keepAlive
+     * @param string $key
+     * @param string $keyPass
+     * @param int $sslVersion
+     * @return Response
+     */
+    protected function sendPayloadHTTPS($req, $server, $port, $timeout = 0, $username = '',  $password = '',
+        $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,
+        $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '',
+        $sslVersion = 0)
+    {
+        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+
+        return $this->sendPayloadCURL($req, $server, $port, $timeout, $username,
+            $password, $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort,
+            $proxyUsername, $proxyPassword, $proxyAuthType, 'https', $keepAlive, $key, $keyPass, $sslVersion);
+    }
+
+    /**
+     * @param Request $req
+     * @param string $server
+     * @param int $port
+     * @param int $timeout
+     * @param string $username
+     * @param string $password
+     * @param int $authType only value supported is 1
+     * @param string $cert
+     * @param string $certPass
+     * @param string $caCert
+     * @param string $caCertDir
+     * @param string $proxyHost
+     * @param int $proxyPort
+     * @param string $proxyUsername
+     * @param string $proxyPassword
+     * @param int $proxyAuthType only value supported is 1
+     * @param string $method 'http' (synonym for 'http10'), 'http10' or 'https'
+     * @param string $key
+     * @param string $keyPass @todo not implemented yet.
+     * @param int $sslVersion @todo not implemented yet. See http://php.net/manual/en/migration56.openssl.php
+     * @return Response
+     */
+    protected function sendPayloadSocket($req, $server, $port, $timeout = 0, $username = '', $password = '',
+        $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,
+        $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method='http', $key = '', $keyPass = '',
+        $sslVersion = 0)
     {
         if ($port == 0) {
-            $port = ( $method === "https" ) ? 443 : 80;
+            $port = ( $method === 'https' ) ? 443 : 80;
         }
 
         // Only create the payload if it was not created previously
@@ -569,6 +648,7 @@ class Client
 
         $payload = $req->payload;
         // Deflate request body and set appropriate request headers
+        $encodingHdr = '';
         if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) {
             if ($this->request_compression == 'gzip') {
                 $a = @gzencode($payload);
@@ -583,8 +663,6 @@ class Client
                     $encodingHdr = "Content-Encoding: deflate\r\n";
                 }
             }
-        } else {
-            $encodingHdr = '';
         }
 
         // thanks to Grant Rauscher <grant7@firstworld.net> for this
@@ -608,7 +686,7 @@ class Client
             }
             $connectServer = $proxyHost;
             $connectPort = $proxyPort;
-            $transport = "tcp";
+            $transport = 'tcp';
             $uri = 'http://' . $server . ':' . $port . $this->path;
             if ($proxyUsername != '') {
                 if ($proxyAuthType != 1) {
@@ -619,8 +697,7 @@ class Client
         } else {
             $connectServer = $server;
             $connectPort = $port;
-            /// @todo if supporting https, we should support all its current options as well: peer name verification etc...
-            $transport = ( $method === "https" ) ? "tls" : "tcp";
+            $transport = ( $method === 'https' ) ? 'tls' : 'tcp';
             $uri = $this->path;
         }
 
@@ -649,8 +726,12 @@ class Client
             $cookieHeader = 'Cookie:' . $version . substr($cookieHeader, 0, -1) . "\r\n";
         }
 
-        // omit port if 80
-        $port = ($port == 80) ? '' : (':' . $port);
+        // omit port if default
+        if (($port == 80 && in_array($method, array('http', 'http10'))) || ($port == 443 && $method == 'https')) {
+            $port =  '';
+        } else {
+            $port = ':' . $port;
+        }
 
         $op = 'POST ' . $uri . " HTTP/1.0\r\n" .
             'User-Agent: ' . $this->user_agent . "\r\n" .
@@ -669,16 +750,48 @@ class Client
             Logger::instance()->debugMessage("---SENDING---\n$op\n---END---");
         }
 
-        if ($timeout > 0) {
-            $fp = @stream_socket_client("$transport://$connectServer:$connectPort", $this->errno, $this->errstr, $timeout);
+        $contextOptions = array();
+        if ($method == 'https') {
+            if ($cert != '') {
+                $contextOptions['ssl']['local_cert'] = $cert;
+                if ($certPass != '') {
+                    $contextOptions['ssl']['passphrase'] = $certPass;
+                }
+            }
+            if ($caCert != '') {
+                $contextOptions['ssl']['cafile'] = $caCert;
+            }
+            if ($caCertDir != '') {
+                $contextOptions['ssl']['capath'] = $caCertDir;
+            }
+            if ($key != '') {
+                $contextOptions['ssl']['local_pk'] = $key;
+            }
+            $contextOptions['ssl']['verify_peer'] = $this->verifypeer;
+            $contextOptions['ssl']['verify_peer_name'] = $this->verifypeer;
+        }
+        $context = stream_context_create($contextOptions);
+
+        if ($timeout <= 0) {
+            $connectTimeout = ini_get('default_socket_timeout');
         } else {
-            $fp = @stream_socket_client("$transport://$connectServer:$connectPort", $this->errno, $this->errstr);
+            $connectTimeout = $timeout;
         }
+
+        $this->errno = 0;
+        $this->errstr = '';
+
+        $fp = @stream_socket_client("$transport://$connectServer:$connectPort", $this->errno, $this->errstr, $connectTimeout,
+            STREAM_CLIENT_CONNECT, $context);
         if ($fp) {
             if ($timeout > 0) {
                 stream_set_timeout($fp, $timeout);
             }
         } else {
+            if ($this->errstr == '') {
+                $err = error_get_last();
+                $this->errstr = $err['message'];
+            }
             $this->errstr = 'Connect error: ' . $this->errstr;
             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')');
 
@@ -691,10 +804,8 @@ class Client
             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr);
 
             return $r;
-        } else {
-            // reset errno and errstr on successful socket connection
-            $this->errstr = '';
         }
+
         // G. Giunta 2005/10/24: close socket before parsing.
         // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)
         $ipd = '';
@@ -704,44 +815,12 @@ class Client
             $ipd .= fread($fp, 32768);
         } while (!feof($fp));
         fclose($fp);
+
         $r = $req->parseResponse($ipd, false, $this->return_type);
 
         return $r;
     }
 
-    /**
-     * @param Request $req
-     * @param string $server
-     * @param int $port
-     * @param int $timeout
-     * @param string $username
-     * @param string $password
-     * @param int $authType
-     * @param string $cert
-     * @param string $certPass
-     * @param string $caCert
-     * @param string $caCertDir
-     * @param string $proxyHost
-     * @param int $proxyPort
-     * @param string $proxyUsername
-     * @param string $proxyPassword
-     * @param int $proxyAuthType
-     * @param bool $keepAlive
-     * @param string $key
-     * @param string $keyPass
-     * @param int $sslVersion
-     * @return Response
-     */
-    protected function sendPayloadHTTPS($req, $server, $port, $timeout = 0, $username = '',  $password = '',
-        $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,
-        $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '',
-        $sslVersion = 0)
-    {
-        return $this->sendPayloadCURL($req, $server, $port, $timeout, $username,
-            $password, $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort,
-            $proxyUsername, $proxyPassword, $proxyAuthType, 'https', $keepAlive, $key, $keyPass, $sslVersion);
-    }
-
     /**
      * Contributed by Justin Miller <justin@voxel.net>
      * Requires curl to be built into PHP
@@ -763,7 +842,7 @@ class Client
      * @param string $proxyUsername
      * @param string $proxyPassword
      * @param int $proxyAuthType
-     * @param string $method
+     * @param string $method 'http' (let curl decide), 'http10', 'http11' or 'https'
      * @param bool $keepAlive
      * @param string $key
      * @param string $keyPass
@@ -789,7 +868,7 @@ class Client
         }
 
         if ($port == 0) {
-            if ($method == 'http') {
+            if (in_array($method, array('http', 'http10', 'http11'))) {
                 $port = 80;
             } else {
                 $port = 443;
@@ -826,7 +905,12 @@ class Client
         }
 
         if (!$keepAlive || !$this->xmlrpc_curl_handle) {
-            $curl = curl_init($method . '://' . $server . ':' . $port . $this->path);
+            if ($method == 'http11' || $method == 'http10') {
+                $protocol = 'http';
+            } else {
+                $protocol = $method;
+            }
+            $curl = curl_init($protocol . '://' . $server . ':' . $port . $this->path);
             if ($keepAlive) {
                 $this->xmlrpc_curl_handle = $curl;
             }
@@ -873,12 +957,22 @@ class Client
             $headers[] = $encodingHdr;
         }
 
+        // Fix the HTTP/1.1 417 Expectation Failed Bug (curl by default adds a 'Expect: 100-continue' header when POST
+        // size exceeds 1025 bytes, apparently)
+        $headers[] = 'Expect:';
+
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
         // timeout is borked
         if ($timeout) {
             curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1);
         }
 
+        if ($method == 'http10') {
+            curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+        } elseif ($method == 'http11') {
+            curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+        }
+
         if ($username && $password) {
             curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);
             if (defined('CURLOPT_HTTPAUTH')) {
@@ -961,7 +1055,7 @@ class Client
                 }
                 $message .= $name . ': ' . $val . "\n";
             }
-            $message .= "---END---";
+            $message .= '---END---';
             Logger::instance()->debugMessage($message);
         }
 
@@ -1168,10 +1262,12 @@ class Client
                         break;
                     case 'struct':
                         $code = $val['faultCode'];
+                        /** @var Value $code */
                         if ($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') {
                             return false;
                         }
                         $str = $val['faultString'];
+                        /** @var Value $str */
                         if ($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') {
                             return false;
                         }