diff --git a/cake/libs/http_socket.php b/cake/libs/http_socket.php index 3aa2355ee..284a8ab3c 100644 --- a/cake/libs/http_socket.php +++ b/cake/libs/http_socket.php @@ -24,7 +24,7 @@ * @lastmodified $Date$ * @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. @@ -177,6 +177,7 @@ class HttpSocket extends CakeSocket { $request['uri'] = null; } $uri = $this->parseUri($request['uri']); + if (!isset($uri['host'])) { $host = $this->config['host']; } @@ -188,13 +189,14 @@ class HttpSocket extends CakeSocket { $request['uri'] = $this->url($request['uri']); $request['uri'] = $this->parseUri($request['uri'], true); $this->request = Set::merge($this->request, $this->config['request'], $request); + $this->configUri($this->request['uri']); if (isset($host)) { $this->config['host'] = $host; } - $cookies = null; + if (is_array($this->request['header'])) { $this->request['header'] = $this->parseHeader($this->request['header']); 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']); } + 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'])) { $this->request['body'] = $this->httpSerialize($this->request['body']); } @@ -251,6 +260,7 @@ class HttpSocket extends CakeSocket { if (!empty($this->response['cookies'])) { $this->config['request']['cookies'] = array_merge($this->config['request']['cookies'], $this->response['cookies']); } + return $this->response['body']; } @@ -333,7 +343,7 @@ class HttpSocket extends CakeSocket { } if (is_string($url)) { 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)) { $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))); $url = $this->parseUri($url, $base); + if (empty($url)) { $url = $this->config['request']['uri']; } @@ -369,17 +380,19 @@ class HttpSocket extends CakeSocket { } static $responseTemplate; + if (empty($responseTemplate)) { $classVars = get_class_vars(__CLASS__); $responseTemplate = $classVars['response']; } $response = $responseTemplate; + if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) { 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']['body'] = substr($message, strlen($match[0])); @@ -392,6 +405,7 @@ class HttpSocket extends CakeSocket { $response['header'] = $this->parseHeader($response['raw']['header']); $decoded = $this->decodeBody($response['raw']['body'], @$response['header']['Transfer-Encoding']); $response['body'] = $decoded['body']; + if (!empty($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)) { $query = preg_replace('/^\?/', '', $query); $items = explode('&', $query); + foreach ($items as $item) { if (strpos($item, '=') !== false) { list($key, $value) = explode('=', $item); @@ -651,12 +666,6 @@ class HttpSocket extends CakeSocket { $key = urldecode($key); $value = urldecode($value); - if ($value === '') { - $value = null; - } elseif (ctype_digit($value) == true) { - $value = (int)$value; - } - if (preg_match_all('/\[([^\[\]]*)\]/iUs', $key, $matches)) { $subKeys = $matches[1]; $rootKey = substr($key, 0, strpos($key, '[')); diff --git a/cake/libs/socket.php b/cake/libs/socket.php index 80864e4c6..fab69fc60 100644 --- a/cake/libs/socket.php +++ b/cake/libs/socket.php @@ -24,7 +24,7 @@ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ -uses('validation'); +App::import('Core', 'Validation'); /** * Cake network socket connection class. @@ -69,7 +69,6 @@ class CakeSocket extends Object { * @access public */ var $connection = null; - /** * This boolean contains the current state of the CakeSocket class * @@ -77,7 +76,6 @@ class CakeSocket extends Object { * @access public */ var $connected = false; - /** * This variable contains an array with the last error number (num) and string (str) * @@ -85,7 +83,6 @@ class CakeSocket extends Object { * @access public */ var $error = array(); - /** * Constructor. * @@ -94,11 +91,7 @@ class CakeSocket extends Object { function __construct($config = array()) { parent::__construct(); - $classVars = get_class_vars(__CLASS__); - $baseConfig = $classVars['_baseConfig']; - - $this->config = array_merge($baseConfig, $config); - + $this->config = array_merge($this->_baseConfig, $config); if (!is_numeric($this->config['protocol'])) { $this->config['protocol'] = getprotobyname($this->config['protocol']); } diff --git a/cake/tests/cases/libs/http_socket.test.php b/cake/tests/cases/libs/http_socket.test.php index 9dba486c3..ae546ad87 100644 --- a/cake/tests/cases/libs/http_socket.test.php +++ b/cake/tests/cases/libs/http_socket.test.php @@ -26,7 +26,7 @@ * @lastmodified $Date$ * @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. * @@ -796,7 +796,7 @@ class HttpSocketTest extends UnitTestCase { 'user' => null, 'pass' => null, 'path' => '/query', - 'query' => array('foo' => null), + 'query' => array('foo' => ""), 'fragment' => null )); @@ -912,10 +912,10 @@ class HttpSocketTest extends UnitTestCase { $this->assertIdentical($query, array('framework' => 'cakephp')); $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'); - $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'); $this->assertIdentical($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake'))); @@ -989,9 +989,9 @@ class HttpSocketTest extends UnitTestCase { 'ball' ) ), - 'count' => 2 + 'count' => '2' ), - 'empty' => null + 'empty' => '' ); $this->assertIdentical($query, $expectedQuery); } diff --git a/cake/tests/groups/socket.group.php b/cake/tests/groups/socket.group.php new file mode 100644 index 000000000..cbf7cff01 --- /dev/null +++ b/cake/tests/groups/socket.group.php @@ -0,0 +1,45 @@ + + * 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'); + } +} +?> \ No newline at end of file