@@ -248,21 +248,38 @@ def feed_data(self, data: bytes, size: int=0) -> None:
248
248
249
249
def begin_http_chunk_receiving (self ) -> None :
250
250
if self ._http_chunk_splits is None :
251
+ if self .total_bytes :
252
+ raise RuntimeError ("Called begin_http_chunk_receiving when"
253
+ "some data was already fed" )
251
254
self ._http_chunk_splits = []
252
255
253
256
def end_http_chunk_receiving (self ) -> None :
254
257
if self ._http_chunk_splits is None :
255
258
raise RuntimeError ("Called end_chunk_receiving without calling "
256
259
"begin_chunk_receiving first" )
257
- if not self ._http_chunk_splits or \
258
- self ._http_chunk_splits [- 1 ] != self .total_bytes :
259
- self ._http_chunk_splits .append (self .total_bytes )
260
260
261
- # wake up readchunk when end of http chunk received
262
- waiter = self ._waiter
263
- if waiter is not None :
264
- self ._waiter = None
265
- set_result (waiter , False )
261
+ # self._http_chunk_splits contains logical byte offsets from start of
262
+ # the body transfer. Each offset is the offset of the end of a chunk.
263
+ # "Logical" means bytes, accessible for a user.
264
+ # If no chunks containig logical data were received, current position
265
+ # is difinitely zero.
266
+ pos = self ._http_chunk_splits [- 1 ] if self ._http_chunk_splits else 0
267
+
268
+ if self .total_bytes == pos :
269
+ # We should not add empty chunks here. So we check for that.
270
+ # Note, when chunked + gzip is used, we can receive a chunk
271
+ # of compressed data, but that data may not be enough for gzip FSM
272
+ # to yield any uncompressed data. That's why current position may
273
+ # not change after receiving a chunk.
274
+ return
275
+
276
+ self ._http_chunk_splits .append (self .total_bytes )
277
+
278
+ # wake up readchunk when end of http chunk received
279
+ waiter = self ._waiter
280
+ if waiter is not None :
281
+ self ._waiter = None
282
+ set_result (waiter , False )
266
283
267
284
async def _wait (self , func_name : str ) -> None :
268
285
# StreamReader uses a future to link the protocol feed_data() method
0 commit comments