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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "",
"description": "Fixed an issue in NonSeekableStreamDecodingEventStreamIterator where a message checksum returned across multiple partial reads caused a checksum mismatch."
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ protected function parseEvent(): array
)
);
$calculatedCrc = hash_final($this->hashContext, true);
$messageCrc = $this->stream->read(4);
$messageCrc = '';
while (
strlen($messageCrc) < self::BYTES_TRAILING
&& !$this->stream->eof()
) {
$chunk = $this->stream->read(
self::BYTES_TRAILING - strlen($messageCrc)
);
if ($chunk === '') {
break;
}

$messageCrc .= $chunk;
}

if ($calculatedCrc !== $messageCrc) {
throw new ParserException('Message checksum mismatch.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ public function testReadAndHashBytesHandlesPartialReads(
);
}

#[DataProvider('readAndHashBytesHandlesPartialReadsProvider')]
public function testParseEventHandlesPartialChecksumReads(
string $eventName,
array $expected
): void
{
$eventPath = self::EVENT_STREAMS_DIR . "events/$eventName";
$eventStream = Utils::streamFor(
base64_decode(
file_get_contents($eventPath)
)
);
$partialReadStream = $this->createMock(StreamInterface::class);
$partialReadStream->method('isSeekable')->willReturn(false);
$partialReadStream->method('isReadable')->willReturn(true);
$partialReadStream->method('read')
->willReturnCallback(function ($length) use ($eventStream) {
return $eventStream->read(min($length, 1));
});
$iterator = new NonSeekableStreamDecodingEventStreamIterator(
$partialReadStream
);
$iterator->rewind();
$event = $iterator->current();
$this->assertEquals(
$expected['headers'],
$event['headers']
);
$this->assertEquals(
$expected['payload'],
json_decode($event['payload']->getContents(), true)
);
}

public static function readAndHashBytesHandlesPartialReadsProvider(): \Generator
{
$cases = json_decode(
Expand Down
Loading