Description
The decode_xml method in api.py fails with XMLSyntaxError: XML declaration allowed only at the start of the document when the server prepends whitespace (e.g., newlines) before the <?xml ...?> declaration. This happens with some BroadWorks servers.
Root Cause
lxml.etree.fromstring strictly requires the XML declaration at position 0. Servers may send responses starting with whitespace, causing the error:
lxml.etree.XMLSyntaxError: XML declaration allowed only at the start of the document, line 2, column 6
Example problematic response (starts with newline before XML):
\n<?xml version="1.0" encoding="ISO-8859-1"?><BroadsoftDocument xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" protocol="OCI"><sessionId xmlns="">session-id-here</sessionId><command xmlns="" xsi:type="SomeResponseType">...</command></BroadsoftDocument>\n
Proposed Fix
Strip leading whitespace in decode_xml before parsing:
# In api.py, decode_xml method
if isinstance(xml, (bytes, bytearray)):
xml = xml.lstrip()
elif isinstance(xml, str):
xml = xml.lstrip()
Steps to Reproduce
- Connect to a BroadWorks server that sends whitespace-prefixed responses.
- Call
api.command("UserGetRegistrationListRequest", user_id="some_id").
- Observe the
XMLSyntaxError.
Environment
- broadworks_ocip version: 3.0.0
- Python version: 3.9
- Server: BroadWorks OCI-P
Description
The
decode_xmlmethod inapi.pyfails withXMLSyntaxError: XML declaration allowed only at the start of the documentwhen the server prepends whitespace (e.g., newlines) before the<?xml ...?>declaration. This happens with some BroadWorks servers.Root Cause
lxml.etree.fromstringstrictly requires the XML declaration at position 0. Servers may send responses starting with whitespace, causing the error:Example problematic response (starts with newline before XML):
Proposed Fix
Strip leading whitespace in
decode_xmlbefore parsing:Steps to Reproduce
api.command("UserGetRegistrationListRequest", user_id="some_id").XMLSyntaxError.Environment