| 527 | |
| 528 | @staticmethod |
| 529 | def _decode_chunked(response_body: bytes) -> Tuple[bytes, bool]: |
| 530 | chunked_data = bytearray() |
| 531 | while True: |
| 532 | length_crlf_idx = response_body.find(b"\r\n") |
| 533 | if length_crlf_idx == -1: |
| 534 | break |
| 535 | |
| 536 | hex_length = response_body[:length_crlf_idx] |
| 537 | try: |
| 538 | length = int(hex_length, 16) |
| 539 | except ValueError as e: |
| 540 | logging.error(f"Parsing chunked length failed: {e}") |
| 541 | break |
| 542 | |
| 543 | if length == 0: |
| 544 | length_crlf_idx = response_body.find(b"0\r\n\r\n") |
| 545 | if length_crlf_idx != -1: |
| 546 | return bytes(chunked_data), True |
| 547 | |
| 548 | if length + 2 > len(response_body): |
| 549 | break |
| 550 | |
| 551 | chunked_data.extend( |
| 552 | response_body[length_crlf_idx + 2 : length_crlf_idx + 2 + length] |
| 553 | ) |
| 554 | if length_crlf_idx + 2 + length + 2 > len(response_body): |
| 555 | break |
| 556 | |
| 557 | response_body = response_body[length_crlf_idx + 2 + length + 2 :] |
| 558 | return bytes(chunked_data), False |