Hi everyone,
I’m writing a PHP library for interacting with AGI and I’m having trouble parsing multiline responses from Asterisk.
Since most AGI commands create single-line responses from Asterisk, I had no problem until now. I’d simply use fgets() to grab the line and I’d check that it has at least 3 characters (for the status code). If the line was shortener, I knew that I was done reading.
The problem I’m currently facing is that I want to be able to read multi-line AGI responses. For example, the following command would return a multi-line response:
Command:
GET FULL VARIABLE "${CURL(http://domain.com/test.txt)}"
Response from Asterisk:
<PJSIP/6001-0000000a>AGI Rx << GET FULL VARIABLE "${CURL(http://domain.com/test.txt)}"
<PJSIP/6001-0000000a>AGI Tx >> 200 result=1 (<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>)
The thing is I don’t know up until where I should read data:
- I cannot stop reading at a newline character (since the response may contain newline chars)
- I cannot stop reading at EOF (because apparently there is no EOF char sent)
Here’s an idea I’ve had, although not optimal:
- Read the first line and check for a known pattern (
200 result=0
for example). Then, continue reading lines and append the data if the lines do NOT have the same pattern at the beginning of the string. What if this pattern is part of the CURL response for example ?
Do you see any way to detect the end of the message sent by Asterisk ? Have any of you had to deal with this kind of responses ?
Regards.
Edit: I think I might have found a way but it really feels hacky:
If after each command I could also send the following command:
<PJSIP/6001-00000011>AGI Rx << GET FULL VARIABLE "separator"
<PJSIP/6001-00000011>AGI Tx >> 200 result=1 (separator)
I could use it as an End-of-message marker and strip in from the response (since its content is predictable).
I’d still prefer to find a real solution because this involves sending and receiving more data that I fell is necessary.