@@ -260,12 +260,9 @@ def __init__(
260
260
boundary : bytes ,
261
261
headers : "CIMultiDictProxy[str]" ,
262
262
content : StreamReader ,
263
- * ,
264
- _newline : bytes = b"\r \n " ,
265
263
) -> None :
266
264
self .headers = headers
267
265
self ._boundary = boundary
268
- self ._newline = _newline
269
266
self ._content = content
270
267
self ._at_eof = False
271
268
length = self .headers .get (CONTENT_LENGTH , None )
@@ -348,9 +345,9 @@ async def read_chunk(self, size: int = chunk_size) -> bytes:
348
345
if self ._read_bytes == self ._length :
349
346
self ._at_eof = True
350
347
if self ._at_eof :
351
- newline = await self ._content .readline ()
348
+ clrf = await self ._content .readline ()
352
349
assert (
353
- newline == self . _newline
350
+ b" \r \n " == clrf
354
351
), "reader did not read all the data or it is malformed"
355
352
return chunk
356
353
@@ -377,15 +374,11 @@ async def _read_chunk_from_stream(self, size: int) -> bytes:
377
374
assert self ._content_eof < 3 , "Reading after EOF"
378
375
assert self ._prev_chunk is not None
379
376
window = self ._prev_chunk + chunk
380
-
381
- intermeditate_boundary = self ._newline + self ._boundary
382
-
377
+ sub = b"\r \n " + self ._boundary
383
378
if first_chunk :
384
- pos = 0
379
+ idx = window . find ( sub )
385
380
else :
386
- pos = max (0 , len (self ._prev_chunk ) - len (intermeditate_boundary ))
387
-
388
- idx = window .find (intermeditate_boundary , pos )
381
+ idx = window .find (sub , max (0 , len (self ._prev_chunk ) - len (sub )))
389
382
if idx >= 0 :
390
383
# pushing boundary back to content
391
384
with warnings .catch_warnings ():
@@ -396,7 +389,6 @@ async def _read_chunk_from_stream(self, size: int) -> bytes:
396
389
chunk = window [len (self ._prev_chunk ) : idx ]
397
390
if not chunk :
398
391
self ._at_eof = True
399
-
400
392
result = self ._prev_chunk
401
393
self ._prev_chunk = chunk
402
394
return result
@@ -425,8 +417,7 @@ async def readline(self) -> bytes:
425
417
else :
426
418
next_line = await self ._content .readline ()
427
419
if next_line .startswith (self ._boundary ):
428
- # strip newline but only once
429
- line = line [: - len (self ._newline )]
420
+ line = line [:- 2 ] # strip CRLF but only once
430
421
self ._unread .append (next_line )
431
422
432
423
return line
@@ -578,12 +569,9 @@ def __init__(
578
569
self ,
579
570
headers : Mapping [str , str ],
580
571
content : StreamReader ,
581
- * ,
582
- _newline : bytes = b"\r \n " ,
583
572
) -> None :
584
573
self .headers = headers
585
574
self ._boundary = ("--" + self ._get_boundary ()).encode ()
586
- self ._newline = _newline
587
575
self ._content = content
588
576
self ._last_part : Optional [Union ["MultipartReader" , BodyPartReader ]] = None
589
577
self ._at_eof = False
@@ -670,13 +658,9 @@ def _get_part_reader(
670
658
if mimetype .type == "multipart" :
671
659
if self .multipart_reader_cls is None :
672
660
return type (self )(headers , self ._content )
673
- return self .multipart_reader_cls (
674
- headers , self ._content , _newline = self ._newline
675
- )
661
+ return self .multipart_reader_cls (headers , self ._content )
676
662
else :
677
- return self .part_reader_cls (
678
- self ._boundary , headers , self ._content , _newline = self ._newline
679
- )
663
+ return self .part_reader_cls (self ._boundary , headers , self ._content )
680
664
681
665
def _get_boundary (self ) -> str :
682
666
mimetype = parse_mimetype (self .headers [CONTENT_TYPE ])
@@ -703,23 +687,11 @@ async def _read_until_first_boundary(self) -> None:
703
687
while True :
704
688
chunk = await self ._readline ()
705
689
if chunk == b"" :
706
- raise ValueError (
707
- "Could not find starting boundary %r" % (self ._boundary )
708
- )
709
- newline = None
710
- end_boundary = self ._boundary + b"--"
711
- if chunk .startswith (end_boundary ):
712
- _ , newline = chunk .split (end_boundary , 1 )
713
- elif chunk .startswith (self ._boundary ):
714
- _ , newline = chunk .split (self ._boundary , 1 )
715
- if newline is not None :
716
- assert newline in (b"\r \n " , b"\n " ), (newline , chunk , self ._boundary )
717
- self ._newline = newline
718
-
690
+ raise ValueError (f"Could not find starting boundary { self ._boundary !r} " )
719
691
chunk = chunk .rstrip ()
720
692
if chunk == self ._boundary :
721
693
return
722
- elif chunk == end_boundary :
694
+ elif chunk == self . _boundary + b"--" :
723
695
self ._at_eof = True
724
696
return
725
697
0 commit comments