Similar to :meth:`HTTPResponse.read`, but with an additional parameter: ``decode_content``. :param amt: How much of the content to read. If specified, caching is skipped because it doesn't make sense to cache partial content as the full r
(
self, amt: int | None = None, decode_content: bool | None = None
)
| 1376 | return returned_chunk # type: ignore[no-any-return] |
| 1377 | |
| 1378 | def read_chunked( |
| 1379 | self, amt: int | None = None, decode_content: bool | None = None |
| 1380 | ) -> typing.Generator[bytes]: |
| 1381 | """ |
| 1382 | Similar to :meth:`HTTPResponse.read`, but with an additional |
| 1383 | parameter: ``decode_content``. |
| 1384 | |
| 1385 | :param amt: |
| 1386 | How much of the content to read. If specified, caching is skipped |
| 1387 | because it doesn't make sense to cache partial content as the full |
| 1388 | response. |
| 1389 | |
| 1390 | :param decode_content: |
| 1391 | If True, will attempt to decode the body based on the |
| 1392 | 'content-encoding' header. |
| 1393 | """ |
| 1394 | self._init_decoder() |
| 1395 | # FIXME: Rewrite this method and make it a class with a better structured logic. |
| 1396 | if not self.chunked: |
| 1397 | raise ResponseNotChunked( |
| 1398 | "Response is not chunked. " |
| 1399 | "Header 'transfer-encoding: chunked' is missing." |
| 1400 | ) |
| 1401 | if not self.supports_chunked_reads(): |
| 1402 | raise BodyNotHttplibCompatible( |
| 1403 | "Body should be http.client.HTTPResponse like. " |
| 1404 | "It should have have an fp attribute which returns raw chunks." |
| 1405 | ) |
| 1406 | |
| 1407 | with self._error_catcher(): |
| 1408 | # Don't bother reading the body of a HEAD request. |
| 1409 | if self._original_response and is_response_to_head(self._original_response): |
| 1410 | self._original_response.close() |
| 1411 | return None |
| 1412 | |
| 1413 | # If a response is already read and closed |
| 1414 | # then return immediately. |
| 1415 | if self._fp.fp is None: # type: ignore[union-attr] |
| 1416 | return None |
| 1417 | |
| 1418 | if amt == 0: |
| 1419 | return |
| 1420 | elif amt and amt < 0: |
| 1421 | # Negative numbers and `None` should be treated the same, |
| 1422 | # but httplib handles only `None` correctly. |
| 1423 | amt = None |
| 1424 | |
| 1425 | while True: |
| 1426 | # First, check if any data is left in the decoder's buffer. |
| 1427 | if self._decoder and self._decoder.has_unconsumed_tail: |
| 1428 | chunk = b"" |
| 1429 | else: |
| 1430 | self._update_chunk_length() |
| 1431 | self._uncached_read_occurred = True |
| 1432 | if self.chunk_left == 0: |
| 1433 | break |
| 1434 | chunk = self._handle_chunk(amt) |
| 1435 | decoded = self._decode( |