Skip to content

[fs] Escape delimiter symbols. #402

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2018
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
3 changes: 2 additions & 1 deletion pkg/fs/FsConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public function receiveNoWait()
ftruncate($file, fstat($file)['size'] - strlen($frame));
rewind($file);

$rawMessage = substr(trim($frame), 1);
$rawMessage = str_replace('\|\{', '|{', $frame);
$rawMessage = substr(trim($rawMessage), 1);

if ($rawMessage) {
try {
Expand Down
4 changes: 3 additions & 1 deletion pkg/fs/FsProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function send(PsrDestination $destination, PsrMessage $message)
$message->setHeader('x-expire-at', microtime(true) + ($this->timeToLive / 1000));
}

$rawMessage = '|'.json_encode($message);
$rawMessage = json_encode($message);
$rawMessage = str_replace('|{', '\|\{', $rawMessage);
$rawMessage = '|'.$rawMessage;

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
Expand Down
27 changes: 27 additions & 0 deletions pkg/fs/Tests/Functional/FsConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,31 @@ public function testShouldThrowExceptionWhenFrameSizeNotDivideExactly()
$this->expectExceptionMessage('The frame size is "12" and it must divide exactly to 64 but it leaves a reminder "12".');
$consumer->receiveNoWait();
}

/**
* @group bug
* @group bug390
*/
public function testShouldUnEscapeDelimiterSymbolsInMessageBody()
{
$context = $this->fsContext;
$queue = $context->createQueue('fs_test_queue');
$context->purge($queue);

$message = $this->fsContext->createMessage(' |{"body":"aMessageData","properties":{"enqueue.topic_name":"user_updated"},"headers":{"content_type":"text\/plain","message_id":"90979b6c-d9ff-4b39-9938-878b83a95360","timestamp":1519899428,"reply_to":null,"correlation_id":""}}');

$this->fsContext->createProducer()->send($queue, $message);

$this->assertSame(0, strlen(file_get_contents(sys_get_temp_dir().'/fs_test_queue')) % 64);
$this->assertSame(
' |{"body":" \|\{\"body\":\"aMessageData\",\"properties\":{\"enqueue.topic_name\":\"user_updated\"},\"headers\":{\"content_type\":\"text\\\\\/plain\",\"message_id\":\"90979b6c-d9ff-4b39-9938-878b83a95360\",\"timestamp\":1519899428,\"reply_to\":null,\"correlation_id\":\"\"}}","properties":[],"headers":[]}',
file_get_contents(sys_get_temp_dir().'/fs_test_queue')
);

$consumer = $context->createConsumer($queue);

$message = $consumer->receiveNoWait();

$this->assertSame(' |{"body":"aMessageData","properties":{"enqueue.topic_name":"user_updated"},"headers":{"content_type":"text\/plain","message_id":"90979b6c-d9ff-4b39-9938-878b83a95360","timestamp":1519899428,"reply_to":null,"correlation_id":""}}', $message->getBody());
}
}