Skip to content

Commit 4ebbc37

Browse files
committed
Support parsing messages with missing space after colon
1 parent 7604d5f commit 4ebbc37

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/Protocol/Parser.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,13 @@ private function parseMessage($message)
5050
$key = Response::FIELD_COMMAND_OUTPUT;
5151
$value = $line;
5252
} else {
53-
$pos = strpos($line, ': ');
53+
$pos = strpos($line, ':');
5454
if ($pos === false) {
55-
if (substr($line, -1, 1) == ':') {
56-
$key = substr($line, 0, -1);
57-
$value = "";
58-
} else {
59-
throw new \UnexpectedValueException('Parse error, no colon in line "' . $line . '" found');
60-
}
61-
} else {
62-
$value = substr($line, $pos + 2);
63-
$key = substr($line, 0, $pos);
55+
throw new \UnexpectedValueException('Parse error, no colon in line "' . $line . '" found');
6456
}
57+
58+
$value = (string)substr($line, $pos + (isset($line[$pos + 1]) && $line[$pos + 1] === ' ' ? 2 : 1));
59+
$key = substr($line, 0, $pos);
6560
}
6661

6762
if (isset($fields[$key])) {

tests/Protocol/ParserTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,33 @@ public function testParsingInvalidResponseFails()
124124
$parser->push("invalid response\r\n\r\n");
125125
}
126126

127-
/**
128-
* @expectedException UnexpectedValueException
129-
*/
130-
public function testParsingInvalidResponseNoSpaceAfterColonFails()
127+
public function testParsingMissingSpaceWithValue()
131128
{
132129
$parser = new Parser();
133130
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
134131

135-
$parser->push("Response:NoSpace\r\n\r\n");
132+
$ret = $parser->push("Response:NoSpace\r\n\r\n");
133+
$this->assertCount(1, $ret);
134+
135+
$first = reset($ret);
136+
/* @var $first Clue\React\Ami\Protocol\Response */
137+
138+
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
139+
$this->assertEquals('NoSpace', $first->getFieldValue('Response'));
136140
}
137141

138-
public function testParsingEmptyValue()
142+
public function testParsingMissingSpaceEmptyValue()
139143
{
140144
$parser = new Parser();
141145
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
142146

143-
$parser->push("Response:\r\n\r\n");
147+
$ret = $parser->push("Response:\r\n\r\n");
148+
$this->assertCount(1, $ret);
149+
150+
$first = reset($ret);
151+
/* @var $first Clue\React\Ami\Protocol\Response */
152+
153+
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
154+
$this->assertEquals('', $first->getFieldValue('Response'));
144155
}
145156
}

0 commit comments

Comments
 (0)