(
self,
code: int,
headers: httputil.HTTPHeaders,
delegate: httputil.HTTPMessageDelegate,
)
| 592 | return start_line, headers |
| 593 | |
| 594 | def _read_body( |
| 595 | self, |
| 596 | code: int, |
| 597 | headers: httputil.HTTPHeaders, |
| 598 | delegate: httputil.HTTPMessageDelegate, |
| 599 | ) -> Optional[Awaitable[None]]: |
| 600 | if "Content-Length" in headers: |
| 601 | if "Transfer-Encoding" in headers: |
| 602 | # Response cannot contain both Content-Length and |
| 603 | # Transfer-Encoding headers. |
| 604 | # http://tools.ietf.org/html/rfc7230#section-3.3.3 |
| 605 | raise httputil.HTTPInputError( |
| 606 | "Response with both Transfer-Encoding and Content-Length" |
| 607 | ) |
| 608 | if "," in headers["Content-Length"]: |
| 609 | # Proxies sometimes cause Content-Length headers to get |
| 610 | # duplicated. If all the values are identical then we can |
| 611 | # use them but if they differ it's an error. |
| 612 | pieces = re.split(r",\s*", headers["Content-Length"]) |
| 613 | if any(i != pieces[0] for i in pieces): |
| 614 | raise httputil.HTTPInputError( |
| 615 | "Multiple unequal Content-Lengths: %r" |
| 616 | % headers["Content-Length"] |
| 617 | ) |
| 618 | headers["Content-Length"] = pieces[0] |
| 619 | |
| 620 | try: |
| 621 | content_length = int(headers["Content-Length"]) # type: Optional[int] |
| 622 | except ValueError: |
| 623 | # Handles non-integer Content-Length value. |
| 624 | raise httputil.HTTPInputError( |
| 625 | "Only integer Content-Length is allowed: %s" |
| 626 | % headers["Content-Length"] |
| 627 | ) |
| 628 | |
| 629 | if cast(int, content_length) > self._max_body_size: |
| 630 | raise httputil.HTTPInputError("Content-Length too long") |
| 631 | else: |
| 632 | content_length = None |
| 633 | |
| 634 | if code == 204: |
| 635 | # This response code is not allowed to have a non-empty body, |
| 636 | # and has an implicit length of zero instead of read-until-close. |
| 637 | # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3 |
| 638 | if "Transfer-Encoding" in headers or content_length not in (None, 0): |
| 639 | raise httputil.HTTPInputError( |
| 640 | "Response with code %d should not have body" % code |
| 641 | ) |
| 642 | content_length = 0 |
| 643 | |
| 644 | if content_length is not None: |
| 645 | return self._read_fixed_body(content_length, delegate) |
| 646 | if headers.get("Transfer-Encoding", "").lower() == "chunked": |
| 647 | return self._read_chunked_body(delegate) |
| 648 | if self.is_client: |
| 649 | return self._read_body_until_close(delegate) |
| 650 | return None |
| 651 |
no test coverage detected