Skip to content

XMLSyntaxError when server response starts with whitespace before XML declaration #30

@kurujega

Description

@kurujega

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

  1. Connect to a BroadWorks server that sends whitespace-prefixed responses.
  2. Call api.command("UserGetRegistrationListRequest", user_id="some_id").
  3. Observe the XMLSyntaxError.

Environment

  • broadworks_ocip version: 3.0.0
  • Python version: 3.9
  • Server: BroadWorks OCI-P

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions