22
33
44class BodyStream :
5- def __init__ (self , sock , remaining = 0 , chunked = False ):
5+ def __init__ (self , sock , remaining = 0 ):
66 self ._sock = sock
7- self ._remaining = remaining
8- self ._chunked = chunked
7+ self ._chunked = remaining < 0
8+ self ._remaining = - 1 if remaining < 0 else remaining
99
1010 def read (self , n = - 1 ):
1111 if not self ._chunked :
@@ -18,42 +18,38 @@ def read(self, n=-1):
1818 if not data :
1919 raise ValueError ("Connection closed before Content-Length satisfied" )
2020 return data
21- if n >= 0 :
22- buf = bytearray (n )
23- return bytes (buf [: self .readinto (buf )])
24- buf = bytearray (256 )
25- chunks = []
21+ buf = bytearray (n if n >= 0 else 256 )
22+ result = b""
2623 while True :
2724 got = self .readinto (buf )
2825 if not got :
2926 break
30- chunks .append (bytes (buf [:got ]))
31- return b"" .join (chunks )
27+ if n >= 0 :
28+ return bytes (buf [:got ])
29+ result += bytes (buf [:got ])
30+ return result
3231
3332 def readinto (self , buf ):
34- if self ._remaining == 0 :
35- if self ._chunked :
36- l = self ._sock .readline ()
37- self ._remaining = int (l .split (b";" , 1 )[0 ], 16 )
38- if self ._remaining == 0 :
39- while True :
40- l = self ._sock .readline ()
41- if not l or l == b"\r \n " :
42- break
43- self ._chunked = False
44- return 0
45- else :
33+ if self ._remaining <= 0 :
34+ if self ._remaining == 0 :
35+ return 0
36+ l = self ._sock .readline ()
37+ self ._remaining = int (l .split (b";" , 1 )[0 ], 16 )
38+ if self ._remaining == 0 :
39+ while True :
40+ l = self ._sock .readline ()
41+ if not l or l == b"\r \n " :
42+ break
4643 return 0
4744 if len (buf ) > self ._remaining :
4845 buf = memoryview (buf )[: self ._remaining ]
4946 got = self ._sock .readinto (buf )
5047 if not got :
51- if self ._chunked :
52- raise ValueError ("Connection closed before chunk was complete" )
53- raise ValueError ("Connection closed before Content-Length satisfied" )
48+ raise ValueError ("Connection closed before body complete" )
5449 self ._remaining -= got
5550 if self ._remaining == 0 and self ._chunked :
5651 self ._sock .readline ()
52+ self ._remaining = - 1
5753 return got
5854
5955 def close (self ):
@@ -260,7 +256,7 @@ def request(
260256 return request (method , redirect , data , json , headers , stream )
261257 else :
262258 if chunked :
263- resp = Response (BodyStream (s , chunked = True ))
259+ resp = Response (BodyStream (s , - 1 ))
264260 elif remaining is not None :
265261 resp = Response (BodyStream (s , remaining ))
266262 else :
0 commit comments