Skip to content

Commit

Permalink
Merge pull request #53 from clue-labs/parse-newlines
Browse files Browse the repository at this point in the history
Support parsing messages with multiple newlines between messages
  • Loading branch information
clue authored Oct 30, 2019
2 parents ddba267 + 39b25a9 commit 8b367b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ public function push($chunk)
$message = substr($this->buffer, 0, $pos);
$this->buffer = (string)substr($this->buffer, $pos + self::LEOM);

$messages []= $this->parseMessage($message);
$parsed = $this->parseMessage($message);
if ($parsed->getFields()) {
$messages []= $parsed;
}
}

return $messages;
}

private function parseMessage($message)
{
$lines = explode(self::EOL, $message);
$lines = array_filter(explode(self::EOL, $message));
$last = count($lines) - 1;
$fields = array();

Expand Down
15 changes: 15 additions & 0 deletions tests/Protocol/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,19 @@ public function testParsingMissingSpaceEmptyValue()
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
$this->assertEquals('', $first->getFieldValue('Response'));
}

public function testParsingExcessiveNewlines()
{
$parser = new Parser();
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));

$ret = $parser->push("First: 1\r\n\r\nSecond: 2\r\n\r\n\r\nThird: 3\r\n\r\n\r\n\r\nFourth: 4\r\n\r\n");
$this->assertCount(4, $ret);

$last = $ret[count($ret) - 1];
/* @var $last \Clue\React\Ami\Protocol\Response */

$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $last);
$this->assertEquals('4', $last->getFieldValue('Fourth'));
}
}

0 comments on commit 8b367b2

Please sign in to comment.