Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions httpie/output/formatters/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ def parse_declaration(raw_body: str) -> Optional[str]:
body = raw_body.strip()
# XMLDecl ::= '<?xml' DECL_CONTENT '?>'
if body.startswith(XML_DECLARATION_OPEN):
end = body.find(XML_DECLARATION_CLOSE)
if end != -1:
return body[:end + len(XML_DECLARATION_CLOSE)]
# The declaration target is exactly ``xml``; a processing instruction
# such as ``<?xml-stylesheet?>`` or ``<?xml-model?>`` merely starts with
# it and must not be treated as the declaration (which would duplicate
# the instruction in the prettified output).
rest = body[len(XML_DECLARATION_OPEN):]
if rest[:1] in ('', ' ', '\t', '\r', '\n', '?'):
end = body.find(XML_DECLARATION_CLOSE)
if end != -1:
return body[:end + len(XML_DECLARATION_CLOSE)]


def pretty_xml(document: 'Document',
Expand Down
13 changes: 13 additions & 0 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,16 @@ def test_invalid_xml(file):
# No formatting done, data is simply printed as-is.
r = http(DUMMY_URL)
assert xml_data in r


@pytest.mark.parametrize('target', ['xml-stylesheet', 'xml-model'])
def test_xml_leading_processing_instruction_not_duplicated(target):
"""A leading ``<?xml-...?>`` processing instruction (whose target merely
begins with ``xml``) must not be misdetected as the XML declaration and
duplicated in the prettified output.
"""
from httpie.output.formatters.xml import XMLFormatter
formatter = XMLFormatter(format_options={'xml': {'format': True, 'indent': 2}})
body = f'<?{target} href="x"?><root><item>x</item></root>'
formatted = formatter.format_body(body, 'application/xml')
assert formatted.count(f'<?{target}') == 1
Loading