From 9e725641ad50cf083924a54f74e46cf6a7e8eac0 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 10 Nov 2012 22:58:36 -0500 Subject: [PATCH] Add context() to CakeSocket. This will help enable peer verification for HttpSocket later on. --- lib/Cake/Network/CakeSocket.php | 30 ++++++++++++++++--- lib/Cake/Test/Case/Network/CakeSocketTest.php | 22 ++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php index 941c0b261..ce482174f 100644 --- a/lib/Cake/Network/CakeSocket.php +++ b/lib/Cake/Network/CakeSocket.php @@ -126,16 +126,29 @@ class CakeSocket { } $scheme = null; - if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') { + if (isset($this->config['request']['uri']) && $this->config['request']['uri']['scheme'] == 'https') { $scheme = 'ssl://'; } - if ($this->config['persistent']) { - $this->connection = @pfsockopen($scheme . $this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); + if (!empty($this->config['request']['context'])) { + $context = stream_context_create($this->config['request']['context']); } else { - $this->connection = @fsockopen($scheme . $this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); + $context = stream_context_create(); } + $connectAs = STREAM_CLIENT_CONNECT; + if ($this->config['persistent']) { + $connectAs = STREAM_CLIENT_PERSISTENT; + } + $this->connection = @stream_socket_client( + $scheme . $this->config['host'] . ':' . $this->config['port'], + $errNum, + $errStr, + $this->config['timeout'], + $connectAs, + $context + ); + if (!empty($errNum) || !empty($errStr)) { $this->setLastError($errNum, $errStr); throw new SocketException($errStr, $errNum); @@ -148,6 +161,15 @@ class CakeSocket { return $this->connected; } +/** + * Get the connection context. + * + * @return array + */ + public function context() { + return stream_context_get_options($this->connection); + } + /** * Get the host name of the current connection. * diff --git a/lib/Cake/Test/Case/Network/CakeSocketTest.php b/lib/Cake/Test/Case/Network/CakeSocketTest.php index d4211e590..41489600d 100644 --- a/lib/Cake/Test/Case/Network/CakeSocketTest.php +++ b/lib/Cake/Test/Case/Network/CakeSocketTest.php @@ -326,4 +326,26 @@ class CakeSocketTest extends CakeTestCase { $this->assertTrue($this->Socket->encrypted); } +/** + * test getting the context for a socket. + * + * @return void + */ + public function testGetContext() { + $config = array( + 'host' => 'smtp.gmail.com', + 'port' => 465, + 'timeout' => 5, + 'request' => array( + 'context' => array( + 'ssl' => array('capture_peer' => true) + ) + ) + ); + $this->Socket = new CakeSocket($config); + $this->Socket->connect(); + $result = $this->Socket->context(); + $this->assertEquals($config['request']['context'], $result); + } + }