Support many string literals in a "line response", deduplicate code

pull/7296/head
Aleksander Machniak 4 years ago
parent f9c84e2646
commit 2965e60c1f

@ -220,6 +220,36 @@ class rcube_imap_generic
return $line;
}
/**
* Reads a line of data from the connection stream inluding all
* string continuation literals.
*
* @param int $size Buffer size
*
* @return string Line of text response
*/
protected function readFullLine($size = 1024)
{
$line = $this->readLine($size);
// include all string literels untile the real end of "line"
while (preg_match('/\{([0-9]+)\}\r\n$/', $line, $m)) {
$bytes = $m[1] + 2;
$out = '';
while (strlen($out) < $bytes) {
$out = $this->readBytes($bytes);
if ($out === null) {
break;
}
$line .= $out;
}
}
return $line;
}
/**
* Reads more data from the connection stream when provided
* data contain string literal
@ -2398,7 +2428,7 @@ class rcube_imap_generic
}
do {
$line = $this->readLine(4096);
$line = $this->readFullLine(4096);
if (!$line) {
break;
@ -2422,27 +2452,6 @@ class rcube_imap_generic
$line = substr($line, strlen($m[0]) + 2);
$ln = 0;
// get complete entry
while (preg_match('/\{([0-9]+)\}\r\n$/', $line, $m)) {
$bytes = $m[1];
$out = '';
while (strlen($out) < $bytes) {
$out = $this->readBytes($bytes);
if ($out === null) {
break;
}
$line .= $out;
}
$str = $this->readLine(4096);
if ($str === false) {
break;
}
$line .= $str;
}
// Tokenize response and assign to object properties
while (list($name, $value) = $this->tokenizeResponse($line, 2)) {
if ($name == 'UID') {
@ -3722,24 +3731,9 @@ class rcube_imap_generic
// Parse response
do {
$line = $this->readLine(4096);
$line = $this->readFullLine(4096);
if ($response !== null) {
// In filter mode we need additional code for string literals.
// We'll merge the following literal data into one chunk, so
// it can be compared with the filter below
if ($filter && preg_match('/\{([0-9]+)\}\r\n$/', $line, $match)) {
// read and append the given bytes
$bytes = $match[1];
$out = '';
while (strlen($out) < $bytes) {
$tmp = $this->readBytes($bytes);
if ($tmp === null) break;
$out .= $tmp;
}
$line .= $out;
}
if (!$filter || preg_match($filter, $line)) {
$response .= $line;
}

Loading…
Cancel
Save