Add context() to CakeSocket.

This will help enable peer verification for HttpSocket later on.
This commit is contained in:
mark_story 2012-11-10 22:58:36 -05:00
parent 3e3af1f998
commit 9e725641ad
2 changed files with 48 additions and 4 deletions

View file

@ -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.
*

View file

@ -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);
}
}