-
Notifications
You must be signed in to change notification settings - Fork 66
Added ability to use LF, not only CRLF delimiter for response Headers and Body #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fda65a2
d3b2ef2
b2f70e9
ce26dc7
af7b481
341f632
74693fc
489de4d
ef27d89
a94d728
8aa9e7f
1aefea0
f688615
ca534f7
a1197d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import re | ||
|
||
import pytest | ||
|
||
from .._receivebuffer import ReceiveBuffer | ||
|
||
|
||
|
@@ -12,15 +16,13 @@ def test_receivebuffer(): | |
assert len(b) == 3 | ||
assert bytes(b) == b"123" | ||
|
||
b.compress() | ||
assert bytes(b) == b"123" | ||
|
||
assert b.maybe_extract_at_most(2) == b"12" | ||
assert b | ||
assert len(b) == 1 | ||
assert bytes(b) == b"3" | ||
|
||
b.compress() | ||
assert bytes(b) == b"3" | ||
|
||
assert b.maybe_extract_at_most(10) == b"3" | ||
|
@@ -33,32 +35,35 @@ def test_receivebuffer(): | |
# maybe_extract_until_next | ||
################################################################ | ||
|
||
b += b"12345a6789aa" | ||
b += b"123\n456\r\n789\r\n" | ||
|
||
assert b.maybe_extract_until_next(b"a") == b"12345a" | ||
assert bytes(b) == b"6789aa" | ||
assert b.maybe_extract_next_line() == b"123\n456\r\n" | ||
assert bytes(b) == b"789\r\n" | ||
|
||
assert b.maybe_extract_until_next(b"aaa") is None | ||
assert bytes(b) == b"6789aa" | ||
assert b.maybe_extract_next_line() == b"789\r\n" | ||
assert bytes(b) == b"" | ||
|
||
b += b"a12" | ||
assert b.maybe_extract_until_next(b"aaa") == b"6789aaa" | ||
assert bytes(b) == b"12" | ||
b += b"12\r" | ||
assert b.maybe_extract_next_line() is None | ||
assert bytes(b) == b"12\r" | ||
|
||
# check repeated searches for the same needle, triggering the | ||
# pickup-where-we-left-off logic | ||
b += b"345" | ||
assert b.maybe_extract_until_next(b"aaa") is None | ||
b += b"345\n\r" | ||
assert b.maybe_extract_next_line() is None | ||
assert bytes(b) == b"12\r345\n\r" | ||
|
||
b += b"6789aaa123" | ||
assert b.maybe_extract_until_next(b"aaa") == b"123456789aaa" | ||
assert bytes(b) == b"123" | ||
# here we stopped at the middle of b"\r\n" delimiter | ||
|
||
b += b"\n6789aaa123\r\n" | ||
assert b.maybe_extract_next_line() == b"12\r345\n\r\n" | ||
assert b.maybe_extract_next_line() == b"6789aaa123\r\n" | ||
assert b.maybe_extract_next_line() is None | ||
assert bytes(b) == b"" | ||
|
||
################################################################ | ||
# maybe_extract_lines | ||
################################################################ | ||
|
||
b += b"\r\na: b\r\nfoo:bar\r\n\r\ntrailing" | ||
b += b"123\r\na: b\r\nfoo:bar\r\n\r\ntrailing" | ||
lines = b.maybe_extract_lines() | ||
assert lines == [b"123", b"a: b", b"foo:bar"] | ||
assert bytes(b) == b"trailing" | ||
|
@@ -76,3 +81,54 @@ def test_receivebuffer(): | |
b += b"\r\ntrailing" | ||
assert b.maybe_extract_lines() == [] | ||
assert bytes(b) == b"trailing" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data", | ||
[ | ||
pytest.param( | ||
( | ||
b"HTTP/1.1 200 OK\r\n", | ||
b"Content-type: text/plain\r\n", | ||
b"Connection: close\r\n", | ||
b"\r\n", | ||
b"Some body", | ||
), | ||
id="with_crlf_delimiter", | ||
), | ||
pytest.param( | ||
( | ||
b"HTTP/1.1 200 OK\n", | ||
b"Content-type: text/plain\n", | ||
b"Connection: close\n", | ||
b"\n", | ||
b"Some body", | ||
), | ||
id="with_lf_only_delimiter", | ||
), | ||
pytest.param( | ||
( | ||
b"HTTP/1.1 200 OK\n", | ||
b"Content-type: text/plain\r\n", | ||
b"Connection: close\n", | ||
b"\n", | ||
b"Some body", | ||
), | ||
id="with_mixed_crlf_and_lf", | ||
), | ||
], | ||
) | ||
def test_receivebuffer_for_invalid_delimiter(data): | ||
b = ReceiveBuffer() | ||
|
||
for line in data: | ||
b += line | ||
|
||
lines = b.maybe_extract_lines() | ||
|
||
assert lines == [ | ||
b"HTTP/1.1 200 OK", | ||
b"Content-type: text/plain", | ||
b"Connection: close", | ||
] | ||
assert bytes(b) == b"Some body" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a few similar tests to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added similar tests to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Added support for servers with broken line endings. | ||
|
||
After this change h11 accepts both ``\r\n`` and ``\n`` as a headers delimiter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
maybe_extract_next_line
, we store the raw buffer length inself._next_line_search
and then do the subtraction when we use it. Inmaybe_extract_lines
, we store the "pre-subtracted" value, so we can use it directly. This inconsistency is kind of confusing :-). We should switch one of them so they match.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As soon as I rewrote both methods using
_extract(...)
method, this issue resolved (both methods works with offsets)