Skip to content

Commit 8672979

Browse files
authored
Replace per-byte partial-boundary scan with rfind lookbehind (#300)
1 parent 8190779 commit 8672979

4 files changed

Lines changed: 55 additions & 11 deletions

File tree

‎python_multipart/multipart.py‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,18 +1361,19 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
13611361
# We matched the whole boundary string.
13621362
index = boundary_length - 1
13631363
i = i0 + boundary_length - 1
1364+
c = data[i]
13641365
else:
1365-
# No match found for whole string.
1366-
# There may be a partial boundary at the end of the
1367-
# data, which the find will not match.
1368-
# Since the length to be searched is limited to the
1369-
# boundary length, scan the tail for boundary[0] via
1370-
# bytes.find (C-level) to keep cost off the Python loop.
1371-
i = max(i, length - boundary_length)
1372-
j = data.find(boundary[:1], i, length - 1)
1373-
i = j if j >= 0 else length - 1
1374-
1375-
c = data[i]
1366+
# No whole boundary, but the tail may hold a partial one
1367+
# that completes in the next chunk. Boundary starts with
1368+
# CR, which an RFC boundary contains nowhere else, so the
1369+
# last CR in the tail is the only candidate prefix start.
1370+
k = data.rfind(boundary[:1], max(i, length - boundary_length + 1), length)
1371+
if k != -1 and boundary.startswith(data[k:length]):
1372+
index = length - k
1373+
# Carry the partial via index; the end-of-chunk flush
1374+
# emits the data before it and re-marks the lookbehind.
1375+
i = length
1376+
continue
13761377

13771378
# Now, we have a couple of cases here. If our index is before
13781379
# the end of the boundary...
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--boundary
2+
Content-Disposition: form-data; name="file"; filename="crlf.bin"
3+
Content-Type: application/octet-stream
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
--boundar
27+
28+
29+
30+
31+
32+
--bound
33+
34+
--boundary--
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
boundary: boundary
2+
expected:
3+
- name: file
4+
type: file
5+
file_name: crlf.bin
6+
content_type: 'application/octet-stream'
7+
data: !!binary |
8+
DQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KLS1ib3VuZGFyDQoNCg0KDQoNCg0KLS1ib3VuZA0K

‎tests/test_multipart.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ def test_not_aligned(self) -> None:
779779
"almost_match_boundary_without_LF",
780780
"almost_match_boundary_without_final_hyphen",
781781
"single_field_single_file",
782+
"crlf_dense_part_data",
782783
]
783784

784785
EPILOGUE_TEST_HEAD = (

0 commit comments

Comments
 (0)