diff --git a/src/Connection/ImapTokenizer.php b/src/Connection/ImapTokenizer.php index 7d9c7b14..caa83eaa 100644 --- a/src/Connection/ImapTokenizer.php +++ b/src/Connection/ImapTokenizer.php @@ -51,7 +51,10 @@ public function nextToken(): ?Token { $this->skipWhitespace(); - $this->ensureBuffer(1); + // Retry if whitespace scanning stopped after an empty read. + if (! isset($this->buffer[$this->position])) { + $this->ensureBuffer(1); + } $char = $this->currentChar(); @@ -139,7 +142,9 @@ public function nextToken(): ?Token protected function skipWhitespace(): void { while (true) { - $this->ensureBuffer(1); + if (! isset($this->buffer[$this->position])) { + $this->ensureBuffer(1); + } $length = strspn($this->buffer, " \t", $this->position); diff --git a/tests/Unit/Connection/ImapTokenizerTest.php b/tests/Unit/Connection/ImapTokenizerTest.php index ec46ae89..d5f69bd3 100644 --- a/tests/Unit/Connection/ImapTokenizerTest.php +++ b/tests/Unit/Connection/ImapTokenizerTest.php @@ -612,3 +612,49 @@ public function fgets(): string|false ['<>', new EmailAddress('')], ["{0}\r\n", new Literal('')], ]); + +test('tokenizer retries a temporary empty read before returning a token', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(['', "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); + +test('tokenizer retries a temporary empty read after consuming whitespace', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw([" \t", '', "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); + +test('tokenizer resumes after consecutive empty reads', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(['', '', "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toBeNull(); + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); + +test('tokenizer reads a fragmented carriage return and line feed', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(["\r", "\n", "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +});