Merge pull request #3706 from MelvinRoss/httpsocketheader

Add support for specifying protocol in Cakesocket/HttpSocket.  Add HEAD function to HttpSocket
This commit is contained in:
Mark Story 2014-07-20 21:58:37 -04:00
commit 7ef7ce2dbb
4 changed files with 90 additions and 9 deletions

View file

@ -116,9 +116,6 @@ 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']);
}
}
/**
@ -133,8 +130,8 @@ class CakeSocket {
}
$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 +384,4 @@ class CakeSocket {
}
}

View file

@ -294,6 +294,7 @@ class HttpSocket extends CakeSocket {
if (isset($host)) {
$this->config['host'] = $host;
}
$this->_setProxy();
$this->request['proxy'] = $this->_proxy;
@ -340,6 +341,9 @@ class HttpSocket extends CakeSocket {
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' && in_array($this->config['protocol'], array(false, 'tcp'))) {
$this->config['protocol'] = 'ssl';
}
$connectionType = null;
if (isset($this->request['header']['Connection'])) {
@ -459,6 +463,32 @@ class HttpSocket extends CakeSocket {
return $this->request($request);
}
/**
* Issues a HEAD request to the specified URI, query, and request.
*
* By definition HEAD request are identical to GET request except they return no response body. This means that all
* information and examples relevant to GET also applys to HEAD.
*
* @param string|array $uri URI to request. Either a string uri, or a uri array, see HttpSocket::_parseUri()
* @param array $query Querystring parameters to append to URI
* @param array $request An indexed array with indexes such as 'method' or uri
* @return mixed Result of request, either false on failure or the response to the request.
*/
public function head($uri = null, $query = array(), $request = array()) {
if (!empty($query)) {
$uri = $this->_parseUri($uri, $this->config['request']['uri']);
if (isset($uri['query'])) {
$uri['query'] = array_merge($uri['query'], $query);
} else {
$uri['query'] = $query;
}
$uri = $this->_buildUri($uri);
}
$request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri), $request);
return $this->request($request);
}
/**
* Issues a POST request to the specified URI, query, and request.
*
@ -1030,3 +1060,4 @@ class HttpSocket extends CakeSocket {
}
}

View file

@ -56,7 +56,7 @@ class CakeSocketTest extends CakeTestCase {
$this->assertSame($config, array(
'persistent' => false,
'host' => 'localhost',
'protocol' => getprotobyname('tcp'),
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
));
@ -71,7 +71,7 @@ class CakeSocketTest extends CakeTestCase {
$config['host'] = 'www.cakephp.org';
$config['port'] = 23;
$config['protocol'] = 17;
$config['protocol'] = 'udp';
$this->assertSame($this->Socket->config, $config);
}

View file

@ -217,7 +217,6 @@ class HttpSocketTest extends CakeTestCase {
$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 @@ class HttpSocketTest extends CakeTestCase {
$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 @@ class HttpSocketTest extends CakeTestCase {
)
)
),
'reset10' => array(
'config.protocol' => 'ssl'
),
array(
'request' => array(
'method' => 'POST',
@ -523,6 +524,9 @@ class HttpSocketTest extends CakeTestCase {
)
)
),
'reset11' => array(
'config.protocol' => 'ssl'
),
array(
'request' => array(
'method' => 'POST',
@ -1045,6 +1049,54 @@ class HttpSocketTest extends CakeTestCase {
));
}
/**
* testHead method
*
* @return void
*/
public function testHead() {
$this->RequestSocket->reset();
$this->RequestSocket->expects($this->at(0))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/'));
$this->RequestSocket->expects($this->at(1))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar'));
$this->RequestSocket->expects($this->at(2))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar'));
$this->RequestSocket->expects($this->at(3))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
$this->RequestSocket->expects($this->at(4))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
$this->RequestSocket->expects($this->at(5))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'https://secure.example.com/test.php?one=two'));
$this->RequestSocket->expects($this->at(6))
->method('request')
->with(array('method' => 'HEAD', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456'));
$this->RequestSocket->head('http://www.google.com/');
$this->RequestSocket->head('http://www.google.com/', array('foo' => 'bar'));
$this->RequestSocket->head('http://www.google.com/', 'foo=bar');
$this->RequestSocket->head('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
$this->RequestSocket->head('http://www.google.com/', null, array('version' => '1.0'));
$this->RequestSocket->head('https://secure.example.com/test.php', array('one' => 'two'));
$this->RequestSocket->head('https://example.com/oauth/access', array(
'clientid' => '123',
'redirect_uri' => 'http://example.com',
'code' => 456
));
}
/**
* Test authentication
*