updating HttpSocket with auth, closes #3838

removing type casting, fixes #3827
adding socket.group to tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6334 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-01-08 03:50:19 +00:00
parent 348df0fca2
commit d45d7a8faf
4 changed files with 72 additions and 25 deletions

View file

@ -24,7 +24,7 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
uses('socket', 'set'); App::import('Core', array('Socket', 'Set'));
/** /**
* Cake network socket connection class. * Cake network socket connection class.
@ -177,6 +177,7 @@ class HttpSocket extends CakeSocket {
$request['uri'] = null; $request['uri'] = null;
} }
$uri = $this->parseUri($request['uri']); $uri = $this->parseUri($request['uri']);
if (!isset($uri['host'])) { if (!isset($uri['host'])) {
$host = $this->config['host']; $host = $this->config['host'];
} }
@ -188,13 +189,14 @@ class HttpSocket extends CakeSocket {
$request['uri'] = $this->url($request['uri']); $request['uri'] = $this->url($request['uri']);
$request['uri'] = $this->parseUri($request['uri'], true); $request['uri'] = $this->parseUri($request['uri'], true);
$this->request = Set::merge($this->request, $this->config['request'], $request); $this->request = Set::merge($this->request, $this->config['request'], $request);
$this->configUri($this->request['uri']); $this->configUri($this->request['uri']);
if (isset($host)) { if (isset($host)) {
$this->config['host'] = $host; $this->config['host'] = $host;
} }
$cookies = null; $cookies = null;
if (is_array($this->request['header'])) { if (is_array($this->request['header'])) {
$this->request['header'] = $this->parseHeader($this->request['header']); $this->request['header'] = $this->parseHeader($this->request['header']);
if (!empty($this->request['cookies'])) { if (!empty($this->request['cookies'])) {
@ -203,6 +205,13 @@ class HttpSocket extends CakeSocket {
$this->request['header'] = array_merge(array('Host' => $this->request['uri']['host']), $this->request['header']); $this->request['header'] = array_merge(array('Host' => $this->request['uri']['host']), $this->request['header']);
} }
if (isset($this->request['auth']['user']) && isset($this->request['auth']['pass'])) {
$this->request['header']['Authorization'] = $this->request['auth']['method'] ." ". base64_encode($this->request['auth']['user'] .":".$this->request['auth']['pass']);
}
if (isset($this->request['uri']['user']) && isset($this->request['uri']['pass'])) {
$this->request['header']['Authorization'] = $this->request['auth']['method'] ." ". base64_encode($this->request['uri']['user'] .":".$this->request['uri']['pass']);
}
if (is_array($this->request['body'])) { if (is_array($this->request['body'])) {
$this->request['body'] = $this->httpSerialize($this->request['body']); $this->request['body'] = $this->httpSerialize($this->request['body']);
} }
@ -251,6 +260,7 @@ class HttpSocket extends CakeSocket {
if (!empty($this->response['cookies'])) { if (!empty($this->response['cookies'])) {
$this->config['request']['cookies'] = array_merge($this->config['request']['cookies'], $this->response['cookies']); $this->config['request']['cookies'] = array_merge($this->config['request']['cookies'], $this->response['cookies']);
} }
return $this->response['body']; return $this->response['body'];
} }
@ -333,7 +343,7 @@ class HttpSocket extends CakeSocket {
} }
if (is_string($url)) { if (is_string($url)) {
if ($url{0} == '/') { if ($url{0} == '/') {
$url = $this->config['request']['uri']['host'].':'.$this->config['request']['uri']['port'].$url; $url = $this->config['request']['uri']['host'].':'.$this->config['request']['uri']['port'] . $url;
} }
if (!preg_match('/^.+:\/\/|\*|^\//', $url)) { if (!preg_match('/^.+:\/\/|\*|^\//', $url)) {
$url = $this->config['request']['uri']['scheme'].'://'.$url; $url = $this->config['request']['uri']['scheme'].'://'.$url;
@ -344,6 +354,7 @@ class HttpSocket extends CakeSocket {
$base = array_merge($this->config['request']['uri'], array('scheme' => array('http', 'https'), 'port' => array(80, 443))); $base = array_merge($this->config['request']['uri'], array('scheme' => array('http', 'https'), 'port' => array(80, 443)));
$url = $this->parseUri($url, $base); $url = $this->parseUri($url, $base);
if (empty($url)) { if (empty($url)) {
$url = $this->config['request']['uri']; $url = $this->config['request']['uri'];
} }
@ -369,17 +380,19 @@ class HttpSocket extends CakeSocket {
} }
static $responseTemplate; static $responseTemplate;
if (empty($responseTemplate)) { if (empty($responseTemplate)) {
$classVars = get_class_vars(__CLASS__); $classVars = get_class_vars(__CLASS__);
$responseTemplate = $classVars['response']; $responseTemplate = $classVars['response'];
} }
$response = $responseTemplate; $response = $responseTemplate;
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) { if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
return false; return false;
} }
list(, $response['raw']['status-line'], $response['raw']['header']) = $match; list($null, $response['raw']['status-line'], $response['raw']['header']) = $match;
$response['raw']['response'] = $message; $response['raw']['response'] = $message;
$response['raw']['body'] = substr($message, strlen($match[0])); $response['raw']['body'] = substr($message, strlen($match[0]));
@ -392,6 +405,7 @@ class HttpSocket extends CakeSocket {
$response['header'] = $this->parseHeader($response['raw']['header']); $response['header'] = $this->parseHeader($response['raw']['header']);
$decoded = $this->decodeBody($response['raw']['body'], @$response['header']['Transfer-Encoding']); $decoded = $this->decodeBody($response['raw']['body'], @$response['header']['Transfer-Encoding']);
$response['body'] = $decoded['body']; $response['body'] = $decoded['body'];
if (!empty($decoded['header'])) { if (!empty($decoded['header'])) {
$response['header'] = $this->parseHeader($this->buildHeader($response['header']).$this->buildHeader($decoded['header'])); $response['header'] = $this->parseHeader($this->buildHeader($response['header']).$this->buildHeader($decoded['header']));
} }
@ -640,6 +654,7 @@ class HttpSocket extends CakeSocket {
if (is_string($query) && !empty($query)) { if (is_string($query) && !empty($query)) {
$query = preg_replace('/^\?/', '', $query); $query = preg_replace('/^\?/', '', $query);
$items = explode('&', $query); $items = explode('&', $query);
foreach ($items as $item) { foreach ($items as $item) {
if (strpos($item, '=') !== false) { if (strpos($item, '=') !== false) {
list($key, $value) = explode('=', $item); list($key, $value) = explode('=', $item);
@ -651,12 +666,6 @@ class HttpSocket extends CakeSocket {
$key = urldecode($key); $key = urldecode($key);
$value = urldecode($value); $value = urldecode($value);
if ($value === '') {
$value = null;
} elseif (ctype_digit($value) == true) {
$value = (int)$value;
}
if (preg_match_all('/\[([^\[\]]*)\]/iUs', $key, $matches)) { if (preg_match_all('/\[([^\[\]]*)\]/iUs', $key, $matches)) {
$subKeys = $matches[1]; $subKeys = $matches[1];
$rootKey = substr($key, 0, strpos($key, '[')); $rootKey = substr($key, 0, strpos($key, '['));

View file

@ -24,7 +24,7 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
uses('validation'); App::import('Core', 'Validation');
/** /**
* Cake network socket connection class. * Cake network socket connection class.
@ -69,7 +69,6 @@ class CakeSocket extends Object {
* @access public * @access public
*/ */
var $connection = null; var $connection = null;
/** /**
* This boolean contains the current state of the CakeSocket class * This boolean contains the current state of the CakeSocket class
* *
@ -77,7 +76,6 @@ class CakeSocket extends Object {
* @access public * @access public
*/ */
var $connected = false; var $connected = false;
/** /**
* This variable contains an array with the last error number (num) and string (str) * This variable contains an array with the last error number (num) and string (str)
* *
@ -85,7 +83,6 @@ class CakeSocket extends Object {
* @access public * @access public
*/ */
var $error = array(); var $error = array();
/** /**
* Constructor. * Constructor.
* *
@ -94,11 +91,7 @@ class CakeSocket extends Object {
function __construct($config = array()) { function __construct($config = array()) {
parent::__construct(); parent::__construct();
$classVars = get_class_vars(__CLASS__); $this->config = array_merge($this->_baseConfig, $config);
$baseConfig = $classVars['_baseConfig'];
$this->config = array_merge($baseConfig, $config);
if (!is_numeric($this->config['protocol'])) { if (!is_numeric($this->config['protocol'])) {
$this->config['protocol'] = getprotobyname($this->config['protocol']); $this->config['protocol'] = getprotobyname($this->config['protocol']);
} }

View file

@ -26,7 +26,7 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
uses('http_socket'); App::import('Core', 'HttpSocket');
/** /**
* Short description for class. * Short description for class.
* *
@ -796,7 +796,7 @@ class HttpSocketTest extends UnitTestCase {
'user' => null, 'user' => null,
'pass' => null, 'pass' => null,
'path' => '/query', 'path' => '/query',
'query' => array('foo' => null), 'query' => array('foo' => ""),
'fragment' => null 'fragment' => null
)); ));
@ -912,10 +912,10 @@ class HttpSocketTest extends UnitTestCase {
$this->assertIdentical($query, array('framework' => 'cakephp')); $this->assertIdentical($query, array('framework' => 'cakephp'));
$query = $this->Socket->parseQuery('a&b&c'); $query = $this->Socket->parseQuery('a&b&c');
$this->assertIdentical($query, array('a' => null, 'b' => null, 'c' => null)); $this->assertIdentical($query, array('a' => '', 'b' => '', 'c' => ''));
$query = $this->Socket->parseQuery('value=12345'); $query = $this->Socket->parseQuery('value=12345');
$this->assertIdentical($query, array('value' => 12345)); $this->assertIdentical($query, array('value' => '12345'));
$query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake'); $query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
$this->assertIdentical($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake'))); $this->assertIdentical($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
@ -989,9 +989,9 @@ class HttpSocketTest extends UnitTestCase {
'ball' 'ball'
) )
), ),
'count' => 2 'count' => '2'
), ),
'empty' => null 'empty' => ''
); );
$this->assertIdentical($query, $expectedQuery); $this->assertIdentical($query, $expectedQuery);
} }

View file

@ -0,0 +1,45 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.groups
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/** Socket and HttpSocket tests
*
* This test group will run socket class tests (socket, http_socket).
*
* @package cake.tests
* @subpackage cake.tests.groups
*/
class SocketGroupTest extends GroupTest {
var $label = 'Socket and HttpSocket tests';
function SocketGroupTest() {
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'socket');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'http_socket');
}
}
?>