From 932ca7415c3b3f326dcae5dd4ba8d3931a288d88 Mon Sep 17 00:00:00 2001 From: Gaurav Raheja Date: Fri, 15 Oct 2021 10:09:03 +0530 Subject: [PATCH 1/7] Do not append missing XML declaration in formatted responses --- httpie/output/formatters/xml.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 3d63fbd574..71ba5789df 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -51,9 +51,14 @@ def format_body(self, body: str, mime: str): except DefusedXmlException: pass # Unsafe XML, ignore. else: - body = pretty_xml(parsed_body, + pretty_body = pretty_xml(parsed_body, encoding=parsed_body.encoding, indent=self.format_options['xml']['indent'], standalone=parsed_body.standalone) - - return body + first_tag_closing = body.find('>') + if first_tag_closing != -1 : + first_tag = body[:first_tag_closing+1] + document_begins_at = pretty_body.find(first_tag) + if document_begins_at != -1: + pretty_body = pretty_body[document_begins_at:] + return pretty_body From d44a62302d7e917a0b690dae98b0e2f33c3ede18 Mon Sep 17 00:00:00 2001 From: Gaurav Raheja Date: Fri, 15 Oct 2021 11:45:17 +0530 Subject: [PATCH 2/7] Fixed UnboundLocalError --- httpie/output/formatters/xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 71ba5789df..1d22217f60 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -43,7 +43,7 @@ def format_body(self, body: str, mime: str): from xml.parsers.expat import ExpatError from defusedxml.common import DefusedXmlException - + pretty_body = body try: parsed_body = parse_xml(body) except ExpatError: From 05dd7800cbed2ed27ac41fda3024a75f2d1855ee Mon Sep 17 00:00:00 2001 From: Gaurav Raheja Date: Fri, 15 Oct 2021 22:05:12 +0530 Subject: [PATCH 3/7] corrected fixture files and simpliy logic --- httpie/output/formatters/xml.py | 16 ++++++---------- .../xmldata/valid/simple-ns_formatted.xml | 1 - .../fixtures/xmldata/valid/simple_formatted.xml | 1 - 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 1d22217f60..6f35990ff8 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -43,22 +43,18 @@ def format_body(self, body: str, mime: str): from xml.parsers.expat import ExpatError from defusedxml.common import DefusedXmlException - pretty_body = body try: parsed_body = parse_xml(body) except ExpatError: pass # Invalid XML, ignore. except DefusedXmlException: pass # Unsafe XML, ignore. - else: - pretty_body = pretty_xml(parsed_body, + else: + has_declaration = body.startswith('') - if first_tag_closing != -1 : - first_tag = body[:first_tag_closing+1] - document_begins_at = pretty_body.find(first_tag) - if document_begins_at != -1: - pretty_body = pretty_body[document_begins_at:] - return pretty_body + if not has_declaration: + body = body[body.find('\n') + 1:] + return body diff --git a/tests/fixtures/xmldata/valid/simple-ns_formatted.xml b/tests/fixtures/xmldata/valid/simple-ns_formatted.xml index 8273afca19..43b49c6203 100644 --- a/tests/fixtures/xmldata/valid/simple-ns_formatted.xml +++ b/tests/fixtures/xmldata/valid/simple-ns_formatted.xml @@ -1,4 +1,3 @@ - diff --git a/tests/fixtures/xmldata/valid/simple_formatted.xml b/tests/fixtures/xmldata/valid/simple_formatted.xml index 1638b5b6a7..9db0eba2ae 100644 --- a/tests/fixtures/xmldata/valid/simple_formatted.xml +++ b/tests/fixtures/xmldata/valid/simple_formatted.xml @@ -1,4 +1,3 @@ - text From 39c6399d55de47742077592c458c0b1385caf2c5 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Mon, 18 Oct 2021 16:09:51 +0530 Subject: [PATCH 4/7] Added Deleted new lines --- httpie/output/formatters/xml.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 6f35990ff8..08b3c3f10f 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -43,6 +43,7 @@ def format_body(self, body: str, mime: str): from xml.parsers.expat import ExpatError from defusedxml.common import DefusedXmlException + try: parsed_body = parse_xml(body) except ExpatError: @@ -57,4 +58,5 @@ def format_body(self, body: str, mime: str): standalone=parsed_body.standalone) if not has_declaration: body = body[body.find('\n') + 1:] + return body From 6e32d11b525e16752057dd088628ae282b1a1745 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Mon, 18 Oct 2021 20:58:40 +0530 Subject: [PATCH 5/7] Update httpie/output/formatters/xml.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mickaƫl Schoentgen --- httpie/output/formatters/xml.py | 1 - 1 file changed, 1 deletion(-) diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 08b3c3f10f..f9c8ee41c1 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -43,7 +43,6 @@ def format_body(self, body: str, mime: str): from xml.parsers.expat import ExpatError from defusedxml.common import DefusedXmlException - try: parsed_body = parse_xml(body) except ExpatError: From 45382db5ccd7ae0c36c905e43f5155754af5decc Mon Sep 17 00:00:00 2001 From: Gaurav Raheja Date: Sat, 23 Oct 2021 23:26:07 +0530 Subject: [PATCH 6/7] Made Prolog same as document if present --- httpie/output/formatters/xml.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index f9c8ee41c1..59b6efa2ad 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -20,13 +20,15 @@ def pretty_xml(document: 'Document', standalone: Optional[bool] = None) -> str: """Render the given :class:`~xml.dom.minidom.Document` `document` into a prettified string.""" kwargs = { - 'encoding': encoding or UTF8, 'indent': ' ' * indent, } + if encoding: + kwargs['encoding'] = encoding if standalone is not None and sys.version_info >= (3, 9): kwargs['standalone'] = standalone - body = document.toprettyxml(**kwargs).decode(kwargs['encoding']) - + body = document.toprettyxml(**kwargs) + if type(body) == bytes: + body = body.decode(encoding or UTF8) # Remove blank lines automatically added by `toprettyxml()`. return '\n'.join(line for line in body.splitlines() if line.strip()) @@ -49,13 +51,14 @@ def format_body(self, body: str, mime: str): pass # Invalid XML, ignore. except DefusedXmlException: pass # Unsafe XML, ignore. - else: - has_declaration = body.startswith('") + 2] body = pretty_xml(parsed_body, encoding=parsed_body.encoding, indent=self.format_options['xml']['indent'], standalone=parsed_body.standalone) - if not has_declaration: - body = body[body.find('\n') + 1:] - + if original_declaration: + body = f"{original_declaration}\n" + body[body.find('\n') + 1:] return body From 73038e0a29a281cd79ee1a4925bc63fcea53db3e Mon Sep 17 00:00:00 2001 From: Gaurav Raheja Date: Mon, 25 Oct 2021 22:53:06 +0530 Subject: [PATCH 7/7] Added Fixture file with simple prolog --- httpie/output/formatters/xml.py | 6 +++--- .../xmldata/valid/simple-without-encoding_formatted.xml | 4 ++++ .../fixtures/xmldata/valid/simple-without-encoding_raw.xml | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/xmldata/valid/simple-without-encoding_formatted.xml create mode 100644 tests/fixtures/xmldata/valid/simple-without-encoding_raw.xml diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 59b6efa2ad..03c81f0637 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -54,11 +54,11 @@ def format_body(self, body: str, mime: str): else: original_declaration = None if body.startswith('") + 2] + original_declaration = body[:body.find("?>") + 2] + "\n" body = pretty_xml(parsed_body, encoding=parsed_body.encoding, indent=self.format_options['xml']['indent'], standalone=parsed_body.standalone) - if original_declaration: - body = f"{original_declaration}\n" + body[body.find('\n') + 1:] + body = f"{original_declaration}" + body[body.find('\n') + 1:] + return body diff --git a/tests/fixtures/xmldata/valid/simple-without-encoding_formatted.xml b/tests/fixtures/xmldata/valid/simple-without-encoding_formatted.xml new file mode 100644 index 0000000000..ccf5782daa --- /dev/null +++ b/tests/fixtures/xmldata/valid/simple-without-encoding_formatted.xml @@ -0,0 +1,4 @@ + + + testValue + diff --git a/tests/fixtures/xmldata/valid/simple-without-encoding_raw.xml b/tests/fixtures/xmldata/valid/simple-without-encoding_raw.xml new file mode 100644 index 0000000000..d2f17c5108 --- /dev/null +++ b/tests/fixtures/xmldata/valid/simple-without-encoding_raw.xml @@ -0,0 +1,3 @@ + +testValue +