Skip to content

Commit

Permalink
Merge pull request #97 from isolovey/optimize_multipart
Browse files Browse the repository at this point in the history
Optimize multipart message decoding
  • Loading branch information
CPBridge authored Oct 19, 2024
2 parents 10a2538 + f0b58c1 commit aad72d0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/dicomweb_client/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def _decode_multipart_message(

marker = b''.join((b'--', boundary))
delimiter = b''.join((b'\r\n', marker))
data = b''
data = bytearray()
j = 0
with response:
logger.debug('decode message content')
Expand All @@ -685,16 +685,24 @@ def _decode_multipart_message(
if stream:
logger.debug(f'decode message content chunk #{i}')
data += chunk
while delimiter in data:

prev_part_index = 0
while True:
delimiter_index = data.find(delimiter, prev_part_index)
if delimiter_index < 0:
break
logger.debug(f'decode message part #{j}')
part, data = data.split(delimiter, maxsplit=1)
content = self._extract_part_content(part)
content = self._extract_part_content(
data[prev_part_index:delimiter_index]
)
prev_part_index = delimiter_index + len(delimiter)
j += 1
if content is not None:
logger.debug(
f'extracted {len(content)} bytes from part #{j}'
)
yield content
data = data[prev_part_index:]

content = self._extract_part_content(data)
if content is not None:
Expand Down

0 comments on commit aad72d0

Please sign in to comment.