Skip to content

Commit

Permalink
Modify CakeSocket and HttpSocket so that the "protocol" parameter can…
Browse files Browse the repository at this point in the history
… 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 86923e3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
9 changes: 4 additions & 5 deletions lib/Cake/Network/CakeSocket.php 100644 → 100755
Expand Up @@ -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())
}

/**
Expand All @@ -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'])) {
Expand Down Expand Up @@ -387,3 +385,4 @@ public function enableCrypto($type, $clientOrServer = 'client', $enable = true)
}

}

20 changes: 20 additions & 0 deletions lib/Cake/Network/Http/HttpSocket.php 100644 → 100755
Expand Up @@ -294,6 +294,7 @@ public function request($request = array()) {
if (isset($host)) {
$this->config['host'] = $host;
}

$this->_setProxy();
$this->request['proxy'] = $this->_proxy;

Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1030,3 +1049,4 @@ public function reset($full = true) {
}

}

4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/Network/CakeSocketTest.php 100644 → 100755
Expand Up @@ -56,7 +56,7 @@ public function testConstruct() {
$this->assertSame($config, array(
'persistent' => false,
'host' => 'localhost',
'protocol' => getprotobyname('tcp'),
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
));
Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Test/Case/Network/Http/HttpSocketTest.php 100644 → 100755
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -495,6 +493,9 @@ public function testRequest() {
)
)
),
'reset9' => array(
'config.protocol' => 'ssl'
),
array(
'request' => array(
'method' => 'POST',
Expand Down

0 comments on commit 86923e3

Please sign in to comment.