diff --git a/src/ManageSieve/Client.php b/src/ManageSieve/Client.php index f004824..fac8d73 100644 --- a/src/ManageSieve/Client.php +++ b/src/ManageSieve/Client.php @@ -76,6 +76,9 @@ private function initExpressions() { private function getSingleLine() { $pos = strpos($this->readBuffer, "\r\n"); + if ($pos === false) { + throw new \RuntimeException("Incomplete line in buffer, waiting for more data."); + } $return = substr($this->readBuffer, 0, $pos); return [$return, $pos]; } @@ -103,10 +106,16 @@ private function readLine() { try { $nval = \fread($this->sock, $this->readSize); + $meta = stream_get_meta_data($this->sock); + if ($meta['timed_out']) { + throw new SocketException("Connection timed out while reading from the server."); + } if ($nval === false || $nval === "") { break; } $this->readBuffer .= $nval; + } catch (SocketException $e) { + throw $e; } catch (\Exception $e) { throw new SocketException("Failed to read data from the server."); } @@ -656,6 +665,7 @@ public function connect($username, $password, $tls=false, $authz_id="", $auth_me throw new SocketException("Socket creation failed: " . $errorstr); } + stream_set_timeout($this->sock, $this->readTimeout); $this->connected = true; self::$connectionPool[$connectionKey] = $this->sock; if (!$this->getCapabilitiesFromServer()) {