]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fixes for LineDecoding (#1075)
authorTom Christie <tom@tomchristie.com>
Thu, 23 Jul 2020 08:45:35 +0000 (09:45 +0100)
committerGitHub <noreply@github.com>
Thu, 23 Jul 2020 08:45:35 +0000 (09:45 +0100)
* Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines.
This seems like a one character fix + the test.

* Update the tests.

* Undo formatting/indentation.

* Update long comment.

* Fix trailing cr line decoding

Co-authored-by: Sheridan C Rawlins <scr@verizonmedia.com>
httpx/_decoders.py
tests/test_decoders.py

index 1ea47b00048726984aff9438899e5409da7081d0..d1c60fb2679ef1ab63cd54f062544b82f17961ee 100644 (file)
@@ -233,12 +233,18 @@ class LineDecoder:
     def decode(self, text: str) -> typing.List[str]:
         lines = []
 
-        if text.startswith("\n") and self.buffer and self.buffer[-1] == "\r":
-            # Handle the case where we have an "\r\n" split across
-            # our previous input, and our new chunk.
-            lines.append(self.buffer[:-1] + "\n")
-            self.buffer = ""
-            text = text[1:]
+        if text and self.buffer and self.buffer[-1] == "\r":
+            if text.startswith("\n"):
+                # Handle the case where we have an "\r\n" split across
+                # our previous input, and our new chunk.
+                lines.append(self.buffer[:-1] + "\n")
+                self.buffer = ""
+                text = text[1:]
+            else:
+                # Handle the case where we have "\r" at the end of our
+                # previous input.
+                lines.append(self.buffer[:-1] + "\n")
+                self.buffer = ""
 
         while text:
             num_chars = len(text)
index 89c545b5a41eb732fdde2870e7b8e073d64cbdc9..6b7993109af39423fced3f485f85488906d636e8 100644 (file)
@@ -247,14 +247,13 @@ def test_line_decoder_cr():
     assert decoder.flush() == ["c\n"]
 
     # Issue #1033
-    # TODO: This seems like another bug; fix expectations and results.
     decoder = LineDecoder()
     assert decoder.decode("") == []
     assert decoder.decode("12345\r") == []
-    assert decoder.decode("foo ") == []
+    assert decoder.decode("foo ") == ["12345\n"]
     assert decoder.decode("bar ") == []
     assert decoder.decode("baz\r") == []
-    assert decoder.flush() == ["12345\rfoo bar baz\n"]
+    assert decoder.flush() == ["foo bar baz\n"]
 
 
 def test_line_decoder_crnl():