fix: message streaming

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-29 15:42:29 -04:00
parent 5b27535723
commit 826940087d
4 changed files with 36 additions and 18 deletions
+10 -10
View File
@@ -8,19 +8,21 @@ use DateTimeInterface;
final class MessageParser
{
public static function isFetchMessage(string $payload): bool
public static function isFetchMessage(string $raw): bool
{
return str_contains(strtoupper($payload), 'FETCH (');
return preg_match('/^\*\s+\d+\s+FETCH\s+\(/i', $raw) === 1;
}
public static function parse(string $raw): Message
{
if (!preg_match('/^\*\s+(\d+)\s+FETCH\s+\((.*)\)$/is', $raw, $matches)) {
if (preg_match('/^\*\s+(\d+)\s+FETCH\s+\(/iA', $raw, $matches) !== 1
|| !str_ends_with($raw, ')')) {
throw new ImapException('Unable to parse FETCH response: ' . $raw);
}
$sequence = (int) $matches[1];
$attributes = self::parseAttributes($matches[2]);
$offset = strlen($matches[0]);
$attributes = self::parseAttributes($raw, $offset, strlen($raw) - 1);
$uid = self::toInt($attributes['UID'] ?? null, 'FETCH response is missing UID: ' . $raw);
$envelope = is_array($attributes['ENVELOPE'] ?? null) ? $attributes['ENVELOPE'] : null;
$bodyStructure = isset($attributes['BODYSTRUCTURE']) ? self::parseBodyPart($attributes['BODYSTRUCTURE'], '') : null;
@@ -52,15 +54,13 @@ final class MessageParser
/**
* @return array<string, mixed>
*/
private static function parseAttributes(string $payload): array
private static function parseAttributes(string $payload, int &$offset, int $end): array
{
$attributes = [];
$offset = 0;
$length = strlen($payload);
while ($offset < $length) {
while ($offset < $end) {
self::skipWhitespace($payload, $offset);
if ($offset >= $length) {
if ($offset >= $end) {
break;
}
@@ -784,4 +784,4 @@ final class MessageParser
$value,
)));
}
}
}