Browse Source

Split the protocol out of the host name.

This avoids issues in PHP 5.6 where certificate validation fails when
`ssl://smtp.gmail.com` is used as a host name.

Refs #7579
Mark Story 10 years ago
parent
commit
3d18f280bc
2 changed files with 24 additions and 1 deletions
  1. 5 1
      src/Network/Socket.php
  2. 19 0
      tests/TestCase/Network/SocketTest.php

+ 5 - 1
src/Network/Socket.php

@@ -128,8 +128,12 @@ class Socket
             $this->disconnect();
         }
 
+        $hasProtocol = strpos($this->_config['host'], '://') !== false;
+        if ($hasProtocol) {
+            list($this->_config['protocol'], $this->_config['host']) = explode('://', $this->_config['host']);
+        }
         $scheme = null;
-        if (!empty($this->_config['protocol']) && strpos($this->_config['host'], '://') === false) {
+        if (!empty($this->_config['protocol'])) {
             $scheme = $this->_config['protocol'] . '://';
         }
 

+ 19 - 0
tests/TestCase/Network/SocketTest.php

@@ -292,6 +292,25 @@ class SocketTest extends TestCase
     }
 
     /**
+     * Test that protocol in the host doesn't cause cert errors.
+     *
+     * @return void
+     */
+    public function testConnectProtocolInHost()
+    {
+        $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
+        $configSslTls = ['host' => 'ssl://smtp.gmail.com', 'port' => 465, 'timeout' => 5];
+        $socket = new Socket($configSslTls);
+        try {
+            $socket->connect();
+            $this->assertEquals('smtp.gmail.com', $socket->config('host'));
+            $this->assertEquals('ssl', $socket->config('protocol'));
+        } catch (SocketException $e) {
+            $this->markTestSkipped('Cannot test network, skipping.');
+        }
+    }
+
+    /**
      * _connectSocketToSslTls
      *
      * @return void