Skip to content

Commit

Permalink
Fixes for LineDecoding (#1075)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tomchristie and scr-oath authored Jul 23, 2020
1 parent bd33921 commit 20f4911
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 12 additions & 6 deletions httpx/_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,18 @@ def __init__(self) -> None:
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)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 20f4911

Please sign in to comment.