diff --git a/cake/libs/socket.php b/cake/libs/socket.php index fb50940d0..c5c945aa0 100644 --- a/cake/libs/socket.php +++ b/cake/libs/socket.php @@ -120,7 +120,11 @@ class CakeSocket extends Object { $this->setLastError($errStr, $errNum); } - return $this->connected = is_resource($this->connection); + $this->connected = is_resource($this->connection); + if ($this->connected) { + stream_set_timeout($this->connection, $this->config['timeout']); + } + return $this->connected; } /** @@ -218,7 +222,13 @@ class CakeSocket extends Object { } if (!feof($this->connection)) { - return fread($this->connection, $length); + $buffer = fread($this->connection, $length); + $info = stream_get_meta_data($this->connection); + if ($info['timed_out']) { + $this->setLastError(E_WARNING, __('Connection timed out', true)); + return false; + } + return $buffer; } else { return false; } diff --git a/cake/tests/cases/libs/socket.test.php b/cake/tests/cases/libs/socket.test.php index 31ce57873..931f2b531 100644 --- a/cake/tests/cases/libs/socket.test.php +++ b/cake/tests/cases/libs/socket.test.php @@ -144,6 +144,18 @@ class SocketTest extends CakeTestCase { $this->Socket = new CakeSocket(array('timeout' => 5)); $this->Socket->connect(); $this->assertEqual($this->Socket->read(26), null); + + $config = array('host' => 'www.cakephp.org', 'timeout' => 1); + $this->Socket = new CakeSocket($config); + $this->assertTrue($this->Socket->connect()); + $this->assertFalse($this->Socket->read(1024 * 1024)); + $this->assertEqual($this->Socket->lastError(), '2: ' . __('Connection timed out', true)); + + $config = array('host' => 'www.cakephp.org', 'timeout' => 30); + $this->Socket = new CakeSocket($config); + $this->assertTrue($this->Socket->connect()); + $this->assertEqual($this->Socket->read(26), null); + $this->assertEqual($this->Socket->lastError(), null); } /** * testLastError method