diff --git a/python_multipart/multipart.py b/python_multipart/multipart.py index 604bd70..d387e9a 100644 --- a/python_multipart/multipart.py +++ b/python_multipart/multipart.py @@ -888,6 +888,7 @@ def _internal_write(self, data: bytes, length: int) -> int: on_field_end = _noop_event i = 0 + sep_pos = -1 while i < length: ch = data[i] @@ -982,8 +983,9 @@ def _internal_write(self, data: bytes, length: int) -> int: i = length elif state == FIELD_DATA: - # Try finding an ampersand after this position. - sep_pos = data.find(b"&", i, length) + # Reuse the separator already found while scanning the field name. + if sep_pos < i: + sep_pos = data.find(b"&", i, length) # If we found it, callback this bit as data and then go back # to expecting to find a field. @@ -992,12 +994,14 @@ def _internal_write(self, data: bytes, length: int) -> int: on_field_data(data, i, sep_pos) on_field_end() - # Note that we go to the separator, which brings us to the - # "before field" state. This allows us to properly emit - # "field_start" events only when we actually have data for - # a field of some sort. - i = sep_pos - 1 - state = BEFORE_FIELD + # Trailing or duplicate separators must not start a field. + if sep_pos + 1 < length and data[sep_pos + 1] != AMPERSAND: + on_field_start() + i = sep_pos + state = FIELD_NAME + else: + i = sep_pos - 1 + state = BEFORE_FIELD # Otherwise, emit the rest as data and finish. else: diff --git a/tests/test_multipart.py b/tests/test_multipart.py index 31b84e3..36a0ca3 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -480,6 +480,19 @@ def test_multiple_querystring(self) -> None: self.assert_fields((b"foo", b"bar"), (b"asdf", b"baz")) + def test_field_start_events_across_chunks(self) -> None: + data = b"a=1&b=&&c&=2&d=x=y&" + chunkings = [[data[:split], data[split:]] for split in range(len(data) + 1)] + chunkings.append([data[i : i + 1] for i in range(len(data))]) + for chunks in chunkings: + self.reset() + starts: list[int] = [] + self.p.set_callback("field_start", lambda: starts.append(len(self.f))) + for chunk in chunks: + self.assertEqual(self.p.write(chunk), len(chunk)) + self.assert_fields((b"a", b"1"), (b"b", b""), (b"c", b""), (b"", b"2"), (b"d", b"x=y")) + self.assertEqual(starts, [0, 1, 2, 3, 4]) + def test_streaming_simple(self) -> None: self.p.write(b"foo=bar&") self.assert_fields((b"foo", b"bar"), finalize=False) @@ -519,6 +532,11 @@ def test_too_large_field(self) -> None: self.p.write(b"a=123456") self.assert_fields((b"a", b"12345")) + def test_max_size_truncates_after_separator(self) -> None: + self.p.max_size = 4 + self.assertEqual(self.p.write(b"a=1&b=2"), 4) + self.assert_fields((b"a", b"1")) + def test_invalid_max_size(self) -> None: with self.assertRaises(ValueError): p = QuerystringParser(max_size=-100)