improve https via socket
[plcapi.git] / src / Client.php
index c327bbb..4279ebc 100644 (file)
@@ -695,7 +695,6 @@ 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';
             $uri = $this->path;
         }
@@ -767,7 +766,7 @@ class Client
                 $contextOptions['ssl']['local_pk'] = $key;
             }
             $contextOptions['ssl']['verify_peer'] = $this->verifypeer;
-
+            $contextOptions['ssl']['verify_peer_name'] = $this->verifypeer;
         }
         $context = stream_context_create($contextOptions);
 
@@ -777,6 +776,9 @@ class Client
             $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) {
@@ -784,6 +786,10 @@ class Client
                 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 . ')');
 
@@ -796,10 +802,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 = '';
@@ -809,6 +813,7 @@ class Client
             $ipd .= fread($fp, 32768);
         } while (!feof($fp));
         fclose($fp);
+
         $r = $req->parseResponse($ipd, false, $this->return_type);
 
         return $r;