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
14 changes: 7 additions & 7 deletions src/Connection/ImapTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function nextToken(): ?Token
$this->skipWhitespace();

// Retry if whitespace scanning stopped after an empty read.
if (! isset($this->buffer[$this->position])) {
$this->ensureBuffer(1);
}
$this->ensureBuffer(1);

$char = $this->currentChar();

Expand Down Expand Up @@ -142,9 +140,7 @@ public function nextToken(): ?Token
protected function skipWhitespace(): void
{
while (true) {
if (! isset($this->buffer[$this->position])) {
$this->ensureBuffer(1);
}
$this->ensureBuffer(1);

$length = strspn($this->buffer, " \t", $this->position);

Expand Down Expand Up @@ -430,7 +426,11 @@ protected function readEmailAddress(): ?EmailAddress
*/
protected function ensureBuffer(int $length): void
{
// If we have enough data in the buffer, return early.
if (isset($this->buffer[$this->position + $length - 1])) {
return;
}

// Read until the requested number of bytes is available.
while ((strlen($this->buffer) - $this->position) < $length) {
$data = $this->stream->fgets();

Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Connection/ImapTokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,48 @@ public function fgets(): string|false
expect($tokenizer->nextToken())->toEqual(new Atom('ATOM'));
expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class);
});

test('tokenizer reads buffered values followed by another token', function (string $input, Token $expected, int $chunkSize) {
$stream = new FakeStream;
$stream->open();
$stream->feedRaw(str_split($input.' NEXT', $chunkSize));

$tokenizer = new ImapTokenizer($stream);

expect($tokenizer->nextToken())->toEqual($expected);
expect($tokenizer->nextToken())->toEqual(new Atom('NEXT'));
expect($tokenizer->nextToken())->toBeNull();
})->with([
['"hello \\"world\\""', new QuotedString('hello "world"')],
['<user@example.com>', new EmailAddress('user@example.com')],
['example', new Atom('example')],
["{5}\r\nhello", new Literal('hello')],
])->with(range(1, 24));

test('tokenizer preserves reader errors after an empty stream read', function (array $chunks, string $message) {
$stream = new FakeStream;
$stream->open();
$stream->feedRaw($chunks);

$tokenizer = new ImapTokenizer($stream);

expect(fn () => $tokenizer->nextToken())
->toThrow(ImapParserException::class, $message);
})->with([
[['"hello', '', '"'], 'Unterminated quoted string at buffer offset 0. Buffer: ""'],
[['"hello\\', '', '"'], 'Unterminated escape sequence in quoted string'],
[['<hello', '', '>'], 'Unterminated email address, expected ">"'],
[['{12', '', "}\r\n"], 'Unterminated literal specifier'],
]);

test('tokenizer finishes an atom at an empty stream read', function () {
$stream = new FakeStream;
$stream->open();
$stream->feedRaw(['hello', '', 'world ']);

$tokenizer = new ImapTokenizer($stream);

expect($tokenizer->nextToken())->toEqual(new Atom('hello'));
expect($tokenizer->nextToken())->toEqual(new Atom('world'));
expect($tokenizer->nextToken())->toBeNull();
});
Loading