Imap_Client
Imap_Client copied to clipboard
Add read_timeout
If you download an email with a large attachment (30M, for example), and disconnect your local network interface mid way through https://github.com/bytestream/Imap_Client/blob/ac98de92168777fb8bf0bacf3129fc178523defe/lib/Horde/Imap/Client/Socket/Connection/Socket.php#L186 then Horde hangs indefinitely. feof hangs indefinitely when the stream is in blocking mode and the client disconnects - https://github.com/php/php-src/issues/10495. Temporarily making it non-blocking, with a read timeout, resolves the issue.
Similarly _sendCmdChunk can loop indefinitely on a read/timeout error.
- Added read_timeout parameter to the constructor of Horde\Imap\Client.
- Added SERVER_READTIMEOUT exception code and message in case of timeout error during reading from socket stream.
- Changed _sendCmdChunk() method so that it can handle timeouts while waiting for server response (eof).
- In Socket::read(), added handling of read timeout, which is thrown if no data received within specified period (default 120 seconds) or when eof reached before all literal bytes were sent by server side; also changed default value for 'timeout' option to 30 seconds instead of 0 - this means that we will wait forever until some data arrives on a socket stream unless explicitly set otherwise via options array passed into client's constructor; finally, made sure that blocking mode was restored after finishing with non-blocking reads from a socket stream as per PHP documentation recommendation: https://www.php-figures/streams#warning-about-nonblocking--whence---offset .