Skip to content

Commit c5bcc37

Browse files
committed
fixed O(N^2) complexity of decode_chunked_transfer
1 parent 062a245 commit c5bcc37

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

scrapy/utils/http.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ def decode_chunked_transfer(chunked_body):
1414
http://en.wikipedia.org/wiki/Chunked_transfer_encoding
1515
1616
"""
17-
body_parts, h, t = [], b'', chunked_body
18-
while t:
19-
h, t = t.split(b'\r\n', 1)
20-
if h == b'0':
17+
body_parts = []
18+
pos = 0
19+
while pos < len(chunked_body):
20+
separator_pos = chunked_body.find(b'\r\n', pos)
21+
if separator_pos == -1:
22+
separator_pos = len(chunked_body)
23+
24+
chunk_size = chunked_body[pos:separator_pos]
25+
if chunk_size == b'0':
2126
break
22-
size = int(h, 16)
23-
body_parts.append(t[:size])
24-
t = t[size+2:]
25-
return b''.join(body_parts)
27+
size = int(chunk_size, 16)
28+
pos = separator_pos + 2
29+
chunk_data = chunked_body[pos:pos+size]
30+
body_parts.append(chunk_data)
31+
pos += size + 2
2632

33+
return b''.join(body_parts)

0 commit comments

Comments
 (0)