FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Replace per-byte partial-boundary scan with rfind lookbehind by Kludex · Pull Request #300 · Kludex/python-multipart · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .http  (1) .py  (2) .yaml  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 12 additions & 11 deletions python_multipart/multipart.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1361,18 +1361,19 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
# We matched the whole boundary string.
index = boundary_length - 1
i = i0 + boundary_length - 1
c = data[i]
else:
# No match found for whole string.
# There may be a partial boundary at the end of the
# data, which the find will not match.
# Since the length to be searched is limited to the
# boundary length, scan the tail for boundary[0] via
# bytes.find (C-level) to keep cost off the Python loop.
i = max(i, length - boundary_length)
j = data.find(boundary[:1], i, length - 1)
i = j if j >= 0 else length - 1

c = data[i]
# No whole boundary, but the tail may hold a partial one
# that completes in the next chunk. Boundary starts with
# CR, which an RFC boundary contains nowhere else, so the
# last CR in the tail is the only candidate prefix start.
k = data.rfind(boundary[:1], max(i, length - boundary_length + 1), length)
if k != -1 and boundary.startswith(data[k:length]):
index = length - k
# Carry the partial via index; the end-of-chunk flush
# emits the data before it and re-marks the lookbehind.
i = length
continue

# Now, we have a couple of cases here. If our index is before
# the end of the boundary...
Expand Down
34 changes: 34 additions & 0 deletions tests/test_data/http/crlf_dense_part_data.http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--boundary
Content-Disposition: form-data; name="file"; filename="crlf.bin"
Content-Type: application/octet-stream






















--boundar





--bound

--boundary--
8 changes: 8 additions & 0 deletions tests/test_data/http/crlf_dense_part_data.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
boundary: boundary
expected:
- name: file
type: file
file_name: crlf.bin
content_type: 'application/octet-stream'
data: !!binary |
DQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KDQoNCg0KLS1ib3VuZGFyDQoNCg0KDQoNCg0KLS1ib3VuZA0K
1 change: 1 addition & 0 deletions tests/test_multipart.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ def test_not_aligned(self) -> None:
"almost_match_boundary_without_LF",
"almost_match_boundary_without_final_hyphen",
"single_field_single_file",
"crlf_dense_part_data",
]

EPILOGUE_TEST_HEAD = (
Expand Down
Loading

Back | FazBrowse Home | New Git URL