Skip to content

Commit

Permalink
Add context() to CakeSocket.
Browse files Browse the repository at this point in the history
This will help enable peer verification for HttpSocket later on.
  • Loading branch information
markstory committed Nov 11, 2012
1 parent 3e3af1f commit 9e72564
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/Cake/Network/CakeSocket.php
Expand Up @@ -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);
Expand All @@ -148,6 +161,15 @@ public function connect() {
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.
*
Expand Down
22 changes: 22 additions & 0 deletions lib/Cake/Test/Case/Network/CakeSocketTest.php
Expand Up @@ -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.