@@ -170,15 +170,12 @@ async def recv(self, size=-1, *, timeout=None, raw=True):
170170 if size == - 1 :
171171 return await self ._stream .__anext__ ()
172172
173- if len (self ._read_buf ) < size :
174- async for data in self ._stream :
175- self ._read_buf .extend (data )
176-
177- if len (self ._read_buf ) >= size :
178- break
173+ while len (self ._read_buf ) < size :
174+ self ._read_buf .extend (await self ._stream .__anext__ ())
179175
180176 data = bytes (self ._read_buf [:size ])
181177 del self ._read_buf [:size ]
178+
182179 return data
183180
184181 async def stream (self , timeout = None , raw = False ):
@@ -201,10 +198,11 @@ async def stream(self, timeout=None, raw=False):
201198
202199 if bytes_unread > 2 :
203200 data = bytes (buf [:bytes_unread - 2 ])
204- yield data
205-
206201 del buf [:bytes_unread - 2 ]
207- bytes_unread -= len (data )
202+
203+ if data :
204+ yield data
205+ bytes_unread -= len (data )
208206
209207 paused = False
210208 continue
@@ -245,11 +243,12 @@ async def stream(self, timeout=None, raw=False):
245243 raise BadRequest ('bad chunked encoding' ) from exc
246244
247245 data = bytes (buf [i + 2 :i + 2 + chunk_size ])
248- bytes_unread = chunk_size - len (data ) + 2
249-
250246 del buf [:i + 2 + chunk_size ]
251247
252- yield data
248+ if data :
249+ yield data
250+
251+ bytes_unread = chunk_size - len (data ) + 2
253252 paused = True
254253 else :
255254 if (self .content_length >
0 commit comments