Skip to content

Commit 7370645

Browse files
cdelerpgjones
authored andcommitted
Fixed PR remark
- reworked maybe_extract_until_next
1 parent 36ff1f1 commit 7370645

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

h11/_readers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ._abnf import chunk_header, header_field, request_line, status_line
2222
from ._events import *
2323
from ._state import *
24+
from ._receivebuffer import line_delimiter_regex
2425
from ._util import LocalProtocolError, RemoteProtocolError, validate
2526

2627
__all__ = ["READERS"]
@@ -153,7 +154,7 @@ def __call__(self, buf):
153154
assert self._bytes_to_discard == 0
154155
if self._bytes_in_chunk == 0:
155156
# We need to refill our chunk count
156-
chunk_header = buf.maybe_extract_until_next(b"\r?\n")
157+
chunk_header = buf.maybe_extract_until_next(line_delimiter_regex, 2)
157158
if chunk_header is None:
158159
return None
159160
matches = validate(

h11/_receivebuffer.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
# processed a whole event, which could in theory be slightly more efficient
4141
# than the internal bytearray support.)
4242

43-
default_delimiter = b"\n\r?\n"
44-
delimiter_regex = re.compile(b"\n\r?\n", re.MULTILINE)
43+
body_and_headers_delimiter_regex = re.compile(b"\n\r?\n", re.MULTILINE)
4544
line_delimiter_regex = re.compile(b"\r?\n", re.MULTILINE)
4645

4746

@@ -51,8 +50,7 @@ def __init__(self):
5150
# These are both absolute offsets into self._data:
5251
self._start = 0
5352
self._looked_at = 0
54-
self._looked_for = default_delimiter
55-
self._looked_for_regex = delimiter_regex
53+
self._looked_for_regex = body_and_headers_delimiter_regex
5654

5755
def __bool__(self):
5856
return bool(len(self))
@@ -87,19 +85,14 @@ def maybe_extract_at_most(self, count):
8785
self._start += len(out)
8886
return out
8987

90-
def maybe_extract_until_next(self, needle):
88+
def maybe_extract_until_next(self, needle_regex, max_needle_length):
9189
# Returns extracted bytes on success (advancing offset), or None on
9290
# failure
93-
if self._looked_for == needle:
94-
looked_at = max(self._start, self._looked_at - len(needle) + 1)
91+
if self._looked_for_regex == needle_regex:
92+
looked_at = max(self._start, self._looked_at - max_needle_length)
9593
else:
9694
looked_at = self._start
97-
self._looked_for = needle
98-
# Check if default delimiter to avoid expensive re.compile
99-
if needle == default_delimiter:
100-
self._looked_for_regex = delimiter_regex
101-
else:
102-
self._looked_for_regex = re.compile(needle, re.MULTILINE)
95+
self._looked_for_regex = needle_regex
10396

10497
delimiter_match = next(
10598
self._looked_for_regex.finditer(self._data, looked_at), None
@@ -136,11 +129,11 @@ def maybe_extract_lines(self):
136129
self._start += len(start_chunk)
137130
return []
138131
else:
139-
data = self.maybe_extract_until_next(default_delimiter)
132+
data = self.maybe_extract_until_next(body_and_headers_delimiter_regex, 3)
140133
if data is None:
141134
return None
142135

143-
delimiter = self._get_fields_delimiter(data, line_delimiter_regex)
144-
lines = data.rstrip(b"\r\n").split(delimiter)
136+
real_lines_delimiter = self._get_fields_delimiter(data, line_delimiter_regex)
137+
lines = data.rstrip(b"\r\n").split(real_lines_delimiter)
145138

146139
return lines

h11/tests/test_receivebuffer.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import pytest
24

35
from .._receivebuffer import ReceiveBuffer
@@ -37,23 +39,23 @@ def test_receivebuffer():
3739

3840
b += b"12345a6789aa"
3941

40-
assert b.maybe_extract_until_next(b"a") == b"12345a"
42+
assert b.maybe_extract_until_next(re.compile(b"a"), 1) == b"12345a"
4143
assert bytes(b) == b"6789aa"
4244

43-
assert b.maybe_extract_until_next(b"aaa") is None
45+
assert b.maybe_extract_until_next(re.compile(b"aaa"), 3) is None
4446
assert bytes(b) == b"6789aa"
4547

4648
b += b"a12"
47-
assert b.maybe_extract_until_next(b"aaa") == b"6789aaa"
49+
assert b.maybe_extract_until_next(re.compile(b"aaa"), 3) == b"6789aaa"
4850
assert bytes(b) == b"12"
4951

5052
# check repeated searches for the same needle, triggering the
5153
# pickup-where-we-left-off logic
5254
b += b"345"
53-
assert b.maybe_extract_until_next(b"aaa") is None
55+
assert b.maybe_extract_until_next(re.compile(b"aaa"), 3) is None
5456

5557
b += b"6789aaa123"
56-
assert b.maybe_extract_until_next(b"aaa") == b"123456789aaa"
58+
assert b.maybe_extract_until_next(re.compile(b"aaa"), 3) == b"123456789aaa"
5759
assert bytes(b) == b"123"
5860

5961
################################################################

0 commit comments

Comments
 (0)