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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions python_multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading