diff --git a/cake/libs/http_socket.php b/cake/libs/http_socket.php index 4b1bf3ac5..52ec9d1b9 100644 --- a/cake/libs/http_socket.php +++ b/cake/libs/http_socket.php @@ -469,7 +469,7 @@ class HttpSocket extends CakeSocket { $chunkLength = null; while ($chunkLength !== 0) { - if (!preg_match("/^([0-9a-f]+) *(?:;(.+)=(.+))?\r\n/iU", $body, $match)) { + if (!preg_match("/^([0-9a-f]+)(?:;(.+)=(.+))?\r\n/iU", $body, $match)) { if (!$this->quirksMode) { trigger_error(__('HttpSocket::decodeChunkedBody - Could not parse malformed chunk. Activate quirks mode to do this.', true), E_USER_WARNING); return false; @@ -956,7 +956,7 @@ class HttpSocket extends CakeSocket { * @access public */ function reset($full = true) { - static $initalState = array() ; + static $initalState = array(); if (empty($initalState)) { $initalState = get_class_vars(__CLASS__); } @@ -966,10 +966,7 @@ class HttpSocket extends CakeSocket { $this->response = $initalState['response']; return true; } - - foreach ($initalState as $property => $value) { - $this->{$property} = $value; - } + parent::reset($initalState); return true; } } diff --git a/cake/libs/socket.php b/cake/libs/socket.php index fab69fc60..a5f861406 100644 --- a/cake/libs/socket.php +++ b/cake/libs/socket.php @@ -82,7 +82,7 @@ class CakeSocket extends Object { * @var array * @access public */ - var $error = array(); + var $lastError = array(); /** * Constructor. * @@ -116,16 +116,14 @@ class CakeSocket extends Object { $tmp = null; $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); } else { - $this->connection = fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); + $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); } if (!empty($errNum) || !empty($errStr)) { $this->setLastError($errStr, $errNum); } - $this->connected = is_resource($this->connection); - - return $this->connected; + return $this->connected = is_resource($this->connection); } /** @@ -174,8 +172,8 @@ class CakeSocket extends Object { * @access public */ function lastError() { - if (!empty($this->error)) { - return $this->error['num'].': '.$this->error['str']; + if (!empty($this->lastError)) { + return $this->lastError['num'].': '.$this->lastError['str']; } else { return null; } @@ -262,6 +260,25 @@ class CakeSocket extends Object { function __destruct() { $this->disconnect(); } +/** + * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed) + * + * @return boolean True on success + * @access public + */ + function reset($initialState = null) { + if (empty($initalState)) { + static $initalState = array(); + if (empty($initalState)) { + $initalState = get_class_vars(__CLASS__); + } + } + + foreach ($initalState as $property => $value) { + $this->{$property} = $value; + } + return true; + } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/socket.test.php b/cake/tests/cases/libs/socket.test.php index 1c04a3d9a..ccfbf5bd1 100644 --- a/cake/tests/cases/libs/socket.test.php +++ b/cake/tests/cases/libs/socket.test.php @@ -39,15 +39,59 @@ class SocketTest extends UnitTestCase { $this->Socket = new CakeSocket(); } + function testConstruct() { + $this->Socket->__construct(); + $baseConfig = $this->Socket->_baseConfig; + $this->assertIdentical($baseConfig, array( + 'persistent' => false, + 'host' => 'localhost', + 'protocol' => 'tcp', + 'port' => 80, + 'timeout' => 30 + )); + + $this->Socket->reset(); + $this->Socket->__construct(array('host' => 'foo-bar')); + $baseConfig['host'] = 'foo-bar'; + $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']); + $this->assertIdentical($this->Socket->config, $baseConfig); + + $this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp')); + $baseConfig = $this->Socket->_baseConfig; + + $baseConfig['host'] = 'www.cakephp.org'; + $baseConfig['port'] = 23; + $baseConfig['protocol'] = 17; + + $this->assertIdentical($this->Socket->config, $baseConfig); + } + function testSocketConnection() { $this->assertFalse($this->Socket->connected); $this->Socket->disconnect(); $this->assertFalse($this->Socket->connected); $this->Socket->connect(); $this->assertTrue($this->Socket->connected); + $this->Socket->connect(); + $this->assertTrue($this->Socket->connected); + + $this->Socket->disconnect(); + $config = array('persistent' => true); + $this->Socket = new CakeSocket($config); + $this->Socket->connect(); + $this->assertTrue($this->Socket->connected); } function testSocketHost() { + $this->Socket = new CakeSocket(); + $this->Socket->connect(); + $this->assertEqual($this->Socket->address(), '127.0.0.1'); + $this->assertPattern('/local/', $this->Socket->host()); + $this->assertEqual($this->Socket->lastError(), null); + $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses())); + + $this->Socket = new CakeSocket(array('host' => '127.0.0.1')); + $this->Socket->connect(); $this->assertEqual($this->Socket->address(), '127.0.0.1'); $this->assertPattern('/local/', $this->Socket->host()); $this->assertEqual($this->Socket->lastError(), null); @@ -59,6 +103,31 @@ class SocketTest extends UnitTestCase { $this->assertTrue($this->Socket->write($request)); } + function testSocketReading() { + $this->Socket = new CakeSocket(array('timeout' => 5)); + $this->Socket->connect(); + $this->assertEqual($this->Socket->read(26), null); + } + + function testLastError() { + $this->Socket = new CakeSocket(); + $this->Socket->setLastError(4, 'some error here'); + $this->assertEqual($this->Socket->lastError(), '4: some error here'); + } + + function testReset() { + $config = array( + 'persistent' => true, + 'host' => '127.0.0.1', + 'protocol' => 'udp', + 'port' => 80, + 'timeout' => 20 + ); + $anotherSocket = new CakeSocket($config); + $anotherSocket->reset(); + $this->assertEqual(array(), $anotherSocket->config); + } + function tearDown() { unset($this->Socket); }