From 7fd6cc52c25d4f73fc74af011d190cadff077e2f Mon Sep 17 00:00:00 2001 From: "renan.saddam" Date: Wed, 15 Jul 2009 19:17:41 +0000 Subject: [PATCH] Fixing issue with timeout when reading socket. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8233 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/socket.php | 14 ++++++++++++-- cake/tests/cases/libs/socket.test.php | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cake/libs/socket.php b/cake/libs/socket.php index fb50940d031..c5c945aa0e5 100644 --- a/cake/libs/socket.php +++ b/cake/libs/socket.php @@ -120,7 +120,11 @@ function connect() { $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 @@ function read($length = 1024) { } 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 31ce5787364..931f2b53158 100644 --- a/cake/tests/cases/libs/socket.test.php +++ b/cake/tests/cases/libs/socket.test.php @@ -144,6 +144,18 @@ function testSocketReading() { $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