Skip to content
Permalink
Browse files

Modify CakeSocket and HttpSocket so that the "protocol" parameter can…

… be used to specify which protocol to use for creating sockets. These are protcols in the php "[a-z]://" wrapper sense. I also modified the test for these two files respectively to accomodate these new changes.

Unrelated to this bug, I added a "head" function inside of HttpSocket to go along with the GET/POST/PUT/DELETE/PATCH combination that's already present. Came in handy for me for deciding if I wanted to hit a resource with HttpSocket or not.
  • Loading branch information...
MelvinRoss committed Jun 12, 2014
1 parent d8b4c39 commit 86923e3593f321e4f73d3659cc41be7de4913d7c
9 lib/Cake/Network/CakeSocket.php 100644 → 100755
@@ -116,9 +116,7 @@ class CakeSocket {
*/
public function __construct($config = array()) {
$this->config = array_merge($this->_baseConfig, $config);
if (!is_numeric($this->config['protocol'])) {
$this->config['protocol'] = getprotobyname($this->config['protocol']);
}
//if(isset())
}
/**
@@ -133,8 +131,8 @@ public function connect() {
}
$scheme = null;
if (isset($this->config['request']['uri']) && $this->config['request']['uri']['scheme'] === 'https') {
$scheme = 'ssl://';
if (!empty($this->config['protocol']) && strpos($this->config['host'], '://') === false) {
$scheme = $this->config['protocol'].'://';
}
if (!empty($this->config['context'])) {
@@ -387,3 +385,4 @@ public function enableCrypto($type, $clientOrServer = 'client', $enable = true)
}
}
@@ -294,6 +294,7 @@ public function request($request = array()) {
if (isset($host)) {
$this->config['host'] = $host;
}
$this->_setProxy();
$this->request['proxy'] = $this->_proxy;
@@ -340,6 +341,9 @@ public function request($request = array()) {
if (!empty($this->request['body']) && !isset($this->request['header']['Content-Length'])) {
$this->request['header']['Content-Length'] = strlen($this->request['body']);
}
if(isset($this->request['uri']['scheme']) && $this->request['uri']['scheme'] === 'https'){
$this->config['protocol'] = 'ssl';
}
$connectionType = null;
if (isset($this->request['header']['Connection'])) {
@@ -520,6 +524,21 @@ public function delete($uri = null, $data = array(), $request = array()) {
return $this->request($request);
}
/**
* Issues a HEADER request to the specified URI, query, and request.
*
* @param string|array $uri URI to request (see {@link _parseUri()})
* @param array $data Array of request body data keys and values.
* @param array $request An indexed array with indexes such as 'method' or uri
* @return mixed Result of request
*/
public function head($uri = null, $data = array(), $request = array()) {
$request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri, 'body' => $data), $request);
return $this->request($request);
}
/**
* Normalizes URLs into a $uriTemplate. If no template is provided
* a default one will be used. Will generate the URL using the
@@ -1030,3 +1049,4 @@ public function reset($full = true) {
}
}
@@ -56,7 +56,7 @@ public function testConstruct() {
$this->assertSame($config, array(
'persistent' => false,
'host' => 'localhost',
'protocol' => getprotobyname('tcp'),
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
));
@@ -71,7 +71,7 @@ public function testConstruct() {
$config['host'] = 'www.cakephp.org';
$config['port'] = 23;
$config['protocol'] = 17;
$config['protocol'] = 'udp';
$this->assertSame($this->Socket->config, $config);
}
@@ -217,7 +217,6 @@ public function testConstruct() {
$this->Socket->expects($this->never())->method('connect');
$this->Socket->__construct(array('host' => 'foo-bar'));
$baseConfig['host'] = 'foo-bar';
$baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
$this->assertEquals($this->Socket->config, $baseConfig);
$this->Socket->reset();
@@ -226,7 +225,6 @@ public function testConstruct() {
$baseConfig['host'] = $baseConfig['request']['uri']['host'] = 'www.cakephp.org';
$baseConfig['port'] = $baseConfig['request']['uri']['port'] = 23;
$baseConfig['request']['uri']['scheme'] = 'http';
$baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
$this->assertEquals($this->Socket->config, $baseConfig);
$this->Socket->reset();
@@ -495,6 +493,9 @@ public function testRequest() {
)
)
),
'reset9' => array(
'config.protocol' => 'ssl'
),
array(
'request' => array(
'method' => 'POST',

0 comments on commit 86923e3

Please sign in to comment.
You can’t perform that action at this time.