Permalink
Browse files

Add context() to CakeSocket.

This will help enable peer verification for HttpSocket later on.
  • Loading branch information...
1 parent 3e3af1f commit 9e725641ad50cf083924a54f74e46cf6a7e8eac0 @markstory markstory committed Nov 11, 2012
Showing with 48 additions and 4 deletions.
  1. +26 −4 lib/Cake/Network/CakeSocket.php
  2. +22 −0 lib/Cake/Test/Case/Network/CakeSocketTest.php
@@ -126,16 +126,29 @@ public function connect() {
}
$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);
@@ -149,6 +162,15 @@ public function connect() {
}
/**
+ * Get the connection context.
+ *
+ * @return array
+ */
+ public function context() {
+ return stream_context_get_options($this->connection);
+ }
+
+/**
* Get the host name of the current connection.
*
* @return string Host name
@@ -326,4 +326,26 @@ public function testEnableCryptoEnableStatus() {
$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);
+ }
+
}

0 comments on commit 9e72564

Please sign in to comment.