Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Connection/ImapTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Connection/ImapTokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Loading